-
Notifications
You must be signed in to change notification settings - Fork 2
/
injectedYoutube.js
69 lines (55 loc) · 1.52 KB
/
injectedYoutube.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
let video;
const defaultSpeed = 1.0;
const lowerLimit = 0;
const higherLimit = 16;
function urlChangeEvent(){
video = undefined;
if(location.href.indexOf("/watch") == -1){
chrome.runtime.sendMessage({"message": "inactivate_icon"});
}
else{
chrome.runtime.sendMessage({"message": "activate_icon"});
let intervalHandler = setInterval(()=>{
video = document.getElementsByTagName("video")[0];
if(video !== undefined){
clearInterval(intervalHandler);
return;
}
}, 100);
}
}
{
let lastURL;
function checkURL(){
if(location.href != lastURL){
urlChangeEvent();
lastURL = location.href;
}
}
setInterval(checkURL, 1000);
checkURL();
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse)=>{
if(message.message == "set playback speed"){
let speed = message.speed;
if(speed > higherLimit || speed < lowerLimit){
console.error(`invalid speed ${speed}`);
return;
}
if(video === undefined){
console.error("Sorry, video not found!");
return;
}
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);
}
});