-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.js
59 lines (47 loc) · 1.57 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
56
57
58
59
function init() {
var slider = document.getElementById('playbackSpeed');
var output = document.getElementById('currentSpeed');
var resetButton = document.getElementById('resetPlaybackSpeed');
getFromStorage(null, function (savedData) {
lastSetSpeed = savedData.lastSetSpeed || 1;
setValueInner(lastSetSpeed);
});
// attach the on-change and on-event handlers
slider.oninput = setValue;
resetButton.onclick = onResetClick;
function setValue() {
setValueInner(this.value);
}
function setValueInner(val) {
output.innerHTML = val;
slider.value = val;
chrome.tabs.executeScript(null, {
code: getUpdatePlaybackRateFn(val)
});
setInStorage('lastSetSpeed', val);
}
function onResetClick() {
setValueInner(1);
}
function getFromStorage(key, callback) {
chrome.storage.sync.get(key, function(data) {
callback(data);
});
}
function setInStorage(key, data) {
// Setting
chrome.storage.sync.set(JSON.parse(`{"${key}": ${data}}`), function() {
if (chrome.runtime.lastError) {
console.error(
'Error setting ' + key + ' to ' + JSON.stringify(data) + ': ' + chrome.runtime.lastError.message
);
}
});
}
}
function getUpdatePlaybackRateFn(val) {
return `document.querySelectorAll('video').forEach((videoElement) => {
videoElement.playbackRate = ${val};
});`;
}
document.addEventListener('DOMContentLoaded', init);