-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
72 lines (62 loc) · 2.52 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
60
61
62
63
64
65
66
67
68
69
70
71
72
// popup.js
// Defines behaviour of popup.html.
// Get button and text elements.
let speedDownButton = document.getElementById('speedDownButton');
let currentSpeedText = document.getElementById('currentSpeedText');
let speedUpButton = document.getElementById('speedUpButton');
let currentSpeed;
// Get current default speed from local storage when popup is opened.
chrome.storage.sync.get('speed', function (data) {
// Update the text to match the current speed.
currentSpeed = data.speed;
currentSpeedText.innerText = currentSpeed + "x";
// Update the speed accordingly using Chrome's Extensions API.
chrome.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
chrome.tabs.executeScript(
tabs[0].id, {
code: 'var video = document.querySelector("video");if (video) {video.playbackRate = ' + currentSpeed + ';} else {console.log("There is no video element on the page.")};'
});
});
});
// Detect when the speedUp button is pressed.
speedUpButton.onclick = function (element) {
changeSpeed("0.25");
};
// Detect when the speedDown button is pressed.
speedDownButton.onclick = function (element) {
changeSpeed("-0.25");
};
// Listens to messages sent by content.js and
// updates the text in popup.html accordingly.
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
chrome.storage.sync.get('speed', function (data) {
currentSpeedText.innerText = data.speed + "x";
});
});
// Changes the current speed by amount speedDelta. Note that
// speedDelta can be either positive or negative.
let changeSpeed = function (speedDelta) {
// Get current default speed from local storage.
chrome.storage.sync.get('speed', function (data) {
currentSpeed = data.speed;
let newSpeed = Number(currentSpeed) + Number(speedDelta);
currentSpeedText.innerText = newSpeed + "x";
// Save new default speed to local storage.
chrome.storage.sync.set({
speed: String(newSpeed)
});
// Update the speed accordingly using Chrome's Extensions API.
chrome.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
chrome.tabs.executeScript(
tabs[0].id, {
code: 'var video = document.querySelector("video");if (video) {video.playbackRate = ' + newSpeed + ';} else {console.log("There is no video element on the page.")};'
});
});
});
}