This repository has been archived by the owner on Apr 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
userscript.js
146 lines (113 loc) · 4.75 KB
/
userscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// ==UserScript==
// @name Show total duration for Laracasts series
// @namespace http://dieterholvoet.com
// @version 1.3
// @description Shows a sum of the durations of all episodes of a Laracasts series
// @author Dieter Holvoet
// @match laracasts.com/series/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
// @require https://bowercdn.net/c/jquery-observe-2.0.2/jquery-observe.js
// @grant none
// ==/UserScript==
(function() {
'use strict';
if($(".condensed-episode-list").length > 0) {
episode_page();
} else if($(".episode-list").length > 0) {
series_page();
}
/* SERIES PAGE */
function series_page() {
// Append container
var $container = $('<div id="durations" class="level-right"></div>');
$container.append('<div class="level-item"><span><strong id="time-total"></strong> Total</span></div>');
$container.appendTo(".series-banner-meta .level-right");
// Run once
series_page_run();
// Attach observer
$(".episode-list .episode-list-item").observe({ attributes: true, attributeFilter: ['class'] }, series_page_run);
}
function series_page_run() {
var seconds_total = 0,
seconds_left = 0,
is_running = $(".episode-list-item.is-complete").length > 0;
$(".episode-list > .episode-list-item").each(function() {
// Calculate
var runtime = $(this).find(".running-time").text().replace("Run Time ", ""),
seconds = unformatSeconds(runtime);
// Add total
seconds_total += seconds;
// Add remaining
if(is_running && !$(this).is(".is-complete"))
seconds_left += seconds;
});
// Append
if(!$("#time-remaining").length && is_running)
$("#durations").append('<div class="level-item"><span><strong id="time-remaining">'+formatSeconds(seconds_left)+'</strong> Remaining</span></div>');
$("#time-total").text(formatSeconds(seconds_total));
$("#time-remaining").text(formatSeconds(seconds_left));
}
/* EPISODE PAGE */
function episode_page() {
// Append container
var $container = $('<div id="durations" class="level-right" style="flex-direction: column; align-items: flex-end;"></div>');
$container.append('<div class="level-item" style="margin: 0"><span><strong id="time-total"></strong> Total</span></div>');
$container.append('<div class="level-item"><span><strong id="time-remaining"></strong> Remaining</span></div>');
$container.insertAfter(".sidebar-section > .heading:first");
// Run once
episode_page_run();
// Attach observer
$(".vjs-remaining-time-display").observe("childlist", episode_page_run);
}
function episode_page_run() {
var seconds_total = 0,
seconds_left = 0;
$(".condensed-episode-list > .condensed-episode-list-item").each(function() {
// Calculate
var runtime = $(this).find(".length").text(),
seconds = unformatSeconds(runtime);
// Add total
seconds_total += seconds;
// Add remaining
if(!$(this).is(".is-complete") && !$(this).is(".is-active"))
seconds_left += seconds;
});
// Add remaining seconds of active video
var active_passed = unformatSeconds($(".vjs-progress-holder").attr("aria-valuetext")),
active_total = unformatSeconds($(".condensed-episode-list-item.is-active .length").text().trim());
seconds_left += (active_total - active_passed);
// Append
$("#time-total").text(formatSeconds(seconds_total));
$("#time-remaining").text(formatSeconds(seconds_left));
}
/* HELPER */
function unformatSeconds(str) {
var splitted = str.split(":"),
seconds = 0;
seconds += parseInt(splitted[0]) * 60;
seconds += parseInt(splitted[1]);
return seconds;
}
function formatSeconds(total_seconds) {
var str = '',
t = {
hours: parseInt(Math.floor(total_seconds / 3600)),
minutes: parseInt(Math.floor((total_seconds % 3600) / 60)),
seconds: parseInt(Math.floor(total_seconds % 60))
};
// Add zeros
if(t.hours < 10)
t.hours = '0'+t.hours;
if(t.minutes < 10)
t.minutes = '0'+t.minutes;
if(t.seconds < 10)
t.seconds = '0'+t.seconds;
// Create string
if(t.hours > 0)
str += t.hours+':';
str += t.minutes;
if(t.seconds > 0)
str += ':'+t.seconds;
return str;
}
})();