-
Notifications
You must be signed in to change notification settings - Fork 0
/
boost_teachable.js
83 lines (69 loc) · 2.93 KB
/
boost_teachable.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
function boost_teachable() {
let existing_elems = document.querySelectorAll(".custom_duration.cumulative"),
playback_speed = document.querySelector(".playback-speed").textContent.trim().replace(/x$/, '');
for (let i = 0; i < existing_elems.length; i++) {
existing_elems[i].remove();
}
function addStyle() {
let head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
head.appendChild(style);
let css = `
.custom_fading_highlight {
background-color: #9ce60ab0;
animation-name:anim_fadeout;
animation-fill-mode:forwards;
animation-duration:2s;
animation-delay:2s;
}
@keyframes anim_fadeout {
0% {background-color:#9ce60ab0;}
100% { background-color:transparent;}
}
`
style.appendChild(document.createTextNode(css));
}
addStyle()
function prettify_seconds(duration) {
let hrs = ~~(duration / 3600),
mins = ~~((duration % 3600) / 60),
secs = ~~duration % 60,
ret = "";
if (hrs > 0) {
ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
}
ret += "" + mins + ":" + (secs < 10 ? "0" : "");
ret += "" + secs;
return ret;
}
function get_hour_span(total_seconds) {
let hour_span = document
.createElement('span');
hour_span.className = "custom_duration cumulative custom_fading_highlight"
hour_span.innerText = `[${prettify_seconds(total_seconds / playback_speed)} | ${prettify_seconds(total_seconds)}]`
return hour_span;
}
let course_sections = document.querySelectorAll(".course-sidebar .row.lecture-sidebar .course-section");
for (let course_idx = 0; course_idx < course_sections.length; course_idx++) {
let course_section = course_sections[course_idx],
// video_durations = [],
total_seconds = 0,
lectures = course_section.querySelectorAll(".lecture-name");
for (let lecture_idx = 0; lecture_idx < lectures.length; lecture_idx++) {
let lecture = lectures[lecture_idx],
duration = lecture.textContent.trim().match(/\((.*?)\)$/)[1],
duration_split = duration.split(":"),
lecture_seconds = (parseInt(duration_split[0]) * 60) + parseInt(duration_split[1]);
// video_durations.push(duration);
total_seconds += lecture_seconds;
lecture.appendChild(get_hour_span(total_seconds));
}
course_section
.querySelector(".section-title")
.appendChild(get_hour_span(total_seconds));
}
}
if (document.querySelectorAll(".course-sidebar .row.lecture-sidebar .course-section").length > 0) {
console.log("[Boost Teachable Chrome Plugin] Found a teachable website. Powering it up 🚀")
boost_teachable();
}