-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
55 lines (47 loc) · 1.53 KB
/
popup.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
let rangeText = document.getElementById("rangeText");
let slider = document.getElementById("playbackSlider");
let resetButton = document.getElementById("resetButton");
const lowerPlaybackLimit = 0;
const upperPlaybackLimit = 16;
//speed is float
function setSpeed(speed){
slider.value = speed;
let valueString = speed.toString();
if(valueString.indexOf(".") == -1) valueString += ".0";
rangeText.value = valueString;
updatePlaybackSpeed(speed);
}
function getPlaybackSpeed(){
chrome.tabs.query({"active" : true, "status" : "complete"}, (tabs)=>{
if(tabs.length == 0){
setTimeout(getPlaybackSpeed, 100);
return;
}
chrome.tabs.sendMessage(tabs[0].id, {"message" : "get playback speed"});
});
}
getPlaybackSpeed();
chrome.runtime.onMessage.addListener((request, sender, sendResponse)=>{
if(request.message == "current speed"){
setSpeed(request.speed);
}
});
resetButton.addEventListener("click", ()=>{
setSpeed(1.0);
});
rangeText.addEventListener("keydown", (e)=>{
if(e.key != "Enter") return;
let value = parseFloat(rangeText.value);
value = Math.round(value * 10) / 10;
if(value < lowerPlaybackLimit) value = lowerPlaybackLimit;
if(value > upperPlaybackLimit) value = upperPlaybackLimit;
setSpeed(value);
});
slider.addEventListener("input", (e)=>{
setSpeed(slider.value);
});
function updatePlaybackSpeed(speed){
chrome.tabs.getSelected(undefined, (tab)=>{
chrome.tabs.sendMessage(tab.id, {"message" : "set playback speed", "speed" : speed});
});
}