-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
382 lines (308 loc) · 10.8 KB
/
background.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
document.addEventListener("DOMContentLoaded", function () {
setupUI()
});
// check if user is in the eduscope website
async function setupUI() {
let tabs = await chrome.tabs.query({ active: true, currentWindow: true });
let pageUrl = tabs[0].url;
if (!pageUrl.includes("lecturecapture.sliit.lk")) {
document.body.classList.add("--eduscope-mod-disabled");
document.getElementById("eduscope-mod-error").innerHTML = "Only works in lecturecapture.sliit.lk";
} else {
// get saved values
let { playbackSpeed } = await chrome.storage.sync.get(['playbackSpeed']);
let { scroll } = await chrome.storage.sync.get(["scroll"]);
let { dark } = await chrome.storage.sync.get(["dark"]);
let { tweaks } = await chrome.storage.sync.get(["tweaks"]);
let { theater } = await chrome.storage.sync.get(["theater"]);
let { analytics } = await chrome.storage.sync.get(["analytics"]);
let { attention } = await chrome.storage.sync.get(["attention"]);
// set saved options
if (playbackSpeed !== undefined) {
setPlaybackSpeed(playbackSpeed, true);
}
if (scroll !== undefined) {
setScroll(scroll);
}
if (dark !== undefined) {
setDark(dark);
}
if (tweaks !== undefined) {
setTweaks(tweaks);
}
if (theater !== undefined) {
setTheaterMode(theater);
}
if (analytics !== undefined) {
setAnalyticsOption(analytics)
}
console.log("🚀 ~ setupUI ~ attention:", attention)
if (attention !== undefined) {
setAttentionLock(attention)
}
// get input elements
let speedSlider = document.getElementById("speedSlider");
let speedOptions = document.getElementsByName("speed");
let scrollOptions = document.getElementsByName("scroll");
let darkOptions = document.getElementsByName("dark");
let tweaksOptions = document.getElementsByName("tweaks");
let theaterOptions = document.getElementsByName("theater");
let analyticsOptions = document.getElementsByName("analytics");
let pipMode = document.getElementById("pip");
let attentionOptions = document.getElementsByName("attention");
// let downloadBtn = document.getElementById("download");
//add onclick handlers
//speed
speedSlider.oninput = onSpeedOptionChangeRealtime
speedSlider.onmouseup = onSpeedOptionChange
//speed buttons
Array.prototype.forEach.call(speedOptions, function (radio) {
radio.addEventListener("change", onButtonSpeedOptionChange);
});
//scroll
Array.prototype.forEach.call(scrollOptions, function (radio) {
radio.addEventListener("change", onScrollOptionChange);
});
//DarkMode
Array.prototype.forEach.call(darkOptions, function (radio) {
radio.addEventListener("change", onDarkOptionChange);
});
//UI Tweaks
Array.prototype.forEach.call(tweaksOptions, function (radio) {
radio.addEventListener("change", onTweaksOptionChange);
});
// Theater mode
Array.prototype.forEach.call(theaterOptions, function (radio) {
radio.addEventListener("change", onTheaterOptionChange);
});
// Attention Lock
Array.prototype.forEach.call(attentionOptions, function (radio) {
radio.addEventListener("change", onAttentionOptionChange);
});
// Analytics consent (Disabled for now)
// Array.prototype.forEach.call(analyticsOptions, function (radio) {
// radio.addEventListener("change", onAnalyticsOptionChange);
// });
// Analytics consent
Array.prototype.forEach.call(analyticsOptions, function (radio) {
radio.addEventListener("change", onAnalyticsOptionChange);
});
// PIP mode
pipMode.addEventListener("click", enablePip);
// download button
// downloadBtn.addEventListener("click", () => downloadVideo(pageUrl));
}
}
/*
* --------------------------
* | Option Change Handlers |
* --------------------------
*
*/
// change video playback speed change handler
async function onSpeedOptionChange() {
//set speed
setPlaybackSpeed(this.value);
// save to storage
await chrome.storage.sync.set({ "playbackSpeed": this.value })
}
async function onButtonSpeedOptionChange() {
//set speed
setPlaybackSpeed(this.value);
//
document.getElementById("speedSlider").value = this.value
onSpeedOptionChangeRealtime(this.value)
// save to storage
await chrome.storage.sync.set({ "playbackSpeed": this.value })
}
async function onSpeedOptionChangeRealtime(customValue = 0) {
let val = this.value || customValue
let suffix = val > 5 ? "x🔥" : "x"
document.getElementById("speedValue").innerHTML = `${Number(val).toFixed(2)}${suffix}`
document.getElementById("blinker").style.animationDuration = `${Number((1 / val) / 2).toFixed(2)}s`
let speedOptions = document.getElementsByName("speed")
speedOptions.forEach(elm => {
elm.checked = false
})
}
// change scroll behavior change handler
async function onScrollOptionChange() {
//set speed
setScroll(this.value);
if (this.value == 0) {
// request page refresh
document.getElementById("eduscope-mod-option-scroll-subtitle").innerHTML = "Page Refresh Required";
// TODO Find a way to remove event handlers with extensions
} else {
document.getElementById("eduscope-mod-option-scroll-subtitle").innerHTML = "";
}
// save to storage
await chrome.storage.sync.set({ "scroll": this.value })
}
async function onDarkOptionChange() {
setDark(this.value);
// save to local storage
await chrome.storage.sync.set({ "dark": this.value })
}
async function onTweaksOptionChange() {
setTweaks(this.value);
if (this.value == 0) {
// request page refresh
document.getElementById("eduscope-mod-option-tweaks-subtitle").innerHTML = "Page Refresh Required";
// TODO Find a way to remove event handlers with extensions
} else {
document.getElementById("eduscope-mod-option-tweaks-subtitle").innerHTML = "";
}
// save to local storage
await chrome.storage.sync.set({ "tweaks": this.value })
}
async function onTheaterOptionChange() {
setTheaterMode(this.value);
// save to local storage
await chrome.storage.sync.set({ "theater": this.value })
}
async function onAnalyticsOptionChange() {
// save to local storage
await chrome.storage.sync.set({ "analytics": this.value })
}
async function onAttentionOptionChange() {
setAttentionLock(this.value);
await chrome.storage.sync.set({ "attention": this.value })
}
async function downloadVideo(url) {
chrome.tabs.create({ url: "https://downloader-onboarding.vercel.app/" }, function (tab) {
});
}
/*
*
* ------------------------------
* | Injectors
* ------------------------------
*
*/
// set playback speed
async function setPlaybackSpeed(speed, setInitial = false) {
let tabs = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tabs[0].id, allFrames: true },
func: function (sp) {
videoElements = document.getElementById("eplayer_iframe")?.contentWindow?.document?.getElementsByTagName("video") || [];
Array.prototype.forEach.call(videoElements, function (elm) {
// let playPromise = elm.play()
// if (playPromise !== undefined) {
// elm.playbackRate = sp;
// }
elm.playbackRate = sp;
});
},
args: [speed]
});
if (setInitial) {
let suffix = this.value > 5 ? "x🔥" : "x"
document.getElementById("speedValue").innerHTML = `${Number(speed).toFixed(2)}${suffix}`
document.getElementById("speedSlider").value = speed
}
}
// set scrolling behaviour
async function setScroll(state) {
let tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (state == 1) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id, allFrames: true },
func: () => {
window.addEventListener('keydown', function (e) {
if (e.keyCode == 32) {
e.preventDefault();
}
});
},
});
}
document.getElementById(`scroll-${state}`).checked = true;
}
// set dark theme
async function setDark(state) {
let tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (state == 0) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id, allFrames: true },
files: ['./scripts/darkThemeRemove.js'],
});
} else {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id, allFrames: true },
files: ['./scripts/darkThemeSet.js'],
});
}
document.getElementById(`dark-${state}`).checked = true;
}
// set UI Tweaks
async function setTweaks(state) {
let tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (state == 0) {
} else {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id, allFrames: true },
files: ['./scripts/tweaksSet.js'],
});
}
document.getElementById(`tweaks-${state}`).checked = true;
}
// set Theater mode
async function setTheaterMode(state) {
let tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (state == 0) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id, allFrames: true },
files: ['./scripts/theaterRemove.js'],
});
} else {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id, allFrames: true },
files: ['./scripts/theaterSet.js'],
});
}
document.getElementById(`theater-${state}`).checked = true;
}
// set Attention Lock mode
async function setAttentionLock(state) {
console.log("🚀 ~ setAttentionLock ~ state:", state)
let tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (state == 0) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id, allFrames: true },
files: ['./scripts/attentionRemove.js'],
});
} else {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id, allFrames: true },
args: [{ attentionVideoId: state }],
func: vars => Object.assign(self, vars)
}, () => {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id, allFrames: true },
files: ['./scripts/attentionSet.js'],
});
});
}
document.getElementById(`attention-${state}`).checked = true;
}
async function enablePip() {
let tabs = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tabs[0].id, allFrames: true },
func: function () {
videoElements = document.getElementById("eplayer_iframe")?.contentWindow?.document?.getElementsByTagName("video") || [];
Array.prototype.forEach.call(videoElements, function (elm) {
if (!document.pictureInPictureElement && document.pictureInPictureEnabled) {
elm.requestPictureInPicture();
} else {
console.log("PIP not supported or already in PIP");
}
});
},
});
}
async function setAnalyticsOption(analytics) {
document.getElementById(`analytics-${analytics}`).checked = true;
}