-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjectedUdemy.js
56 lines (53 loc) · 1.58 KB
/
injectedUdemy.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
let video;
let targetVideoSpeed = 1;
function urlUpdate(){
let index = location.href.indexOf("lecture");
chrome.runtime.sendMessage({"message": "inactivate_icon"});
video = undefined;
if(index != -1){
let intervalHandler;
function findVideo(){
video = document.getElementsByTagName("video")[0];
if(video !== undefined){
clearInterval(intervalHandler);
chrome.runtime.sendMessage({"message": "activate_icon"});
video.playbackRate = targetVideoSpeed;
let videoVar = video;
video.addEventListener("play", ()=>{
videoVar.playbackRate = targetVideoSpeed;
});
}
}
intervalHandler = setInterval(findVideo, 1000);
findVideo();
}
}
{
let lastURL;
function verityURL(){
if(location.href != lastURL){
urlUpdate();
lastURL = location.href;
}
}
setInterval(verityURL, 1000);
verityURL();
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse)=>{
if(message.message == "set playback speed"){
let speed = message.speed;
if(speed > 16 || speed < 0){
console.log(`invalid speed ${speed}`);
return;
}
targetVideoSpeed = speed;
video.playbackRate = speed;
}
else if(message.message == "get playback speed"){
let intervalHandler = setInterval(()=>{
if(video === undefined) return;
chrome.runtime.sendMessage({"message" : "current speed", "speed" : video.playbackRate});
clearInterval(intervalHandler);
}, 100);
}
});