-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
95 lines (84 loc) · 3.03 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function checkSpeed(state) {
let video = document.querySelector("video");
if (video && video.playbackRate !== state.speed) {
video.playbackRate = state.speed;
console.log("Speed Updated to: " + state.speed);
} else {
console.log("Speed to: " + state.speed);
}
}
function checkIfSpeedisNumber(speed) {
return speed != undefined && speed != null && !isNaN(speed);
}
const speedState = {
speed: null,
};
let button = document.createElement("button");
button.type = "button";
button.textContent = "Change Speed";
button.style.position = "fixed";
button.style.zIndex = "999";
button.style.display = "block";
button.style.height = "3rem";
let dialog = document.createElement("dialog");
dialog.id = "change-speed-dialog";
dialog.open = false;
dialog.style.zIndex = 999;
dialog.style.height = "6rem";
dialog.style.weight = "6rem";
dialog.style.margin = "auto";
let pTag = document.createElement("p");
let divTag = document.createElement("div");
let speedDecreaseButton = document.createElement("button");
speedDecreaseButton.style.width = "2.5rem";
speedDecreaseButton.textContent = "-";
speedDecreaseButton.addEventListener("click", () => {
let video = document.querySelector("video");
let newSpeed = video
? (video.playbackRate = video.playbackRate - 0.1)
: "x.yz";
pTag.textContent =
"Speed: " +
(typeof newSpeed === "number" ? newSpeed.toFixed(2) : newSpeed);
checkIfSpeedisNumber(speedState.speed)
? (speedState.speed = newSpeed)
: null;
});
let speedIncreaseButton = document.createElement("button");
speedIncreaseButton.textContent = "+";
speedIncreaseButton.style.width = "2.5rem";
speedIncreaseButton.addEventListener("click", () => {
let video = document.querySelector("video");
let newSpeed = video
? (video.playbackRate = video.playbackRate + 0.1)
: "x.yz";
pTag.textContent =
"Speed: " +
(typeof newSpeed === "number" ? newSpeed.toFixed(2) : newSpeed);
checkIfSpeedisNumber(speedState.speed)
? (speedState.speed = newSpeed)
: null;
});
dialog.appendChild(pTag);
dialog.appendChild(speedDecreaseButton);
dialog.appendChild(speedIncreaseButton);
button.addEventListener("click", () => {
let video = document.querySelector("video");
dialog.open = !dialog.open;
dialog.open &&
(pTag.textContent =
"Speed: " + (video ? video.playbackRate.toFixed(2) : "x.yz"));
!checkIfSpeedisNumber(speedState.speed)
? (speedState.speed = video.playbackRate)
: null;
});
document.body.insertBefore(dialog, document.body.firstChild);
document.body.insertBefore(button, document.body.firstChild);
document.addEventListener("keydown", function (event) {
if (event.shiftKey && event.code === "KeyQ") {
button.style.display =
button.style.display === "none" ? "block" : "none";
dialog.open = false;
}
});
setInterval(() => checkSpeed(speedState), 5000);