-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
191 lines (175 loc) · 5.47 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
console.log("Hello! from Video Speed Controller");
let playbackSpeed;
let speedIndicatorTimerId;
let targetVideo = document.querySelector("video");
let speedIndicatorCanvas = createCanvas();
const timerDuration = 1000;
let sites = {
"www.youtube.com": {
init: function() {
const targetNode = document.getElementById("content")
const config = {
attributes: false,
childList: true,
subtree: true,
characterData: false,
};
const observer = new MutationObserver(callback);
if (document.querySelector("video")) {
initializeEvents(targetVideo);
} else {
observer.observe(targetNode, config); // MutationObserverを開始
}
}
},
"www.netflix.com": {
init: function() {
console.log("netflix")
const targetNode = document.getElementById("appMountPoint")
const config = {
attributes: false,
childList: true,
subtree: true,
characterData: false,
};
const observer = new MutationObserver(callback);
if (document.querySelector("video")) {
initializeEvents();
} else {
observer.observe(targetNode, config); // MutationObserverを開始
}
}
}
};
let hostname = window.location.hostname;
if (sites[hostname]) {
sites[hostname].init();
} else {
try {
if (!document.querySelector("video")) {
throw new Error("video element not found");
} else {
initializeEvents();
}
} catch (e) {
console.log(e.message);
}
}
function callback(mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.type === "childList") {
mutation.addedNodes.forEach((node) => {
if (node.nodeName === "VIDEO") {
// console.log("A video node has been added:", node);
// targetVideo = node;
initializeEvents();
}
});
}
}
}
function initializeEvents(targetVideo = document.querySelector("video")) {
// 自動再生動画の再生速度を変更用
targetVideo.addEventListener("canplay", () => {
chrome.storage.local.get({ playbackSpeed: 1.0 }).then((result) => {
playbackSpeed = result.playbackSpeed;
console.log(
"Get playbackSpeed from local storage : " + result.playbackSpeed
);
setPlaybackSpeed(document.querySelector("video"), playbackSpeed, timerDuration);
});
});
window.addEventListener("keydown", (event) => {
targetVideo = document.querySelector("video");
switch (event.key) {
case "d":
increaseSpeed(targetVideo, timerDuration);
break;
case "a":
decreaseSpeed(targetVideo, timerDuration);
break;
case "s":
resetSpeed(targetVideo, timerDuration);
break;
default:
clearTimeout(speedIndicatorTimerId);
clearSpeedIndicator(speedIndicatorCanvas);
}
});
}
function createCanvas() {
const canvas = document.createElement("canvas");
canvas.id = "speedIndicatorCanvas";
canvas.style.position = "absolute";
canvas.style.zIndex = 1;
canvas.style.display = "none";
canvas.style.pointerEvents = "none";
document.body.appendChild(canvas);
return canvas;
}
function setPlaybackSpeed(video, speed, duration) {
speedIndicatorCanvas.width = video.clientWidth;
speedIndicatorCanvas.height = video.clientHeight;
var clientRect = video.getBoundingClientRect();
speedIndicatorCanvas.style.top = clientRect.top + "px";
speedIndicatorCanvas.style.left = clientRect.left + "px";
// console.log(speedIndicatorCanvas.style.top);
video.playbackRate = Number(speed.toFixed(1));
clearTimeout(speedIndicatorTimerId);
clearSpeedIndicator(speedIndicatorCanvas);
drawSpeedIndicator(speedIndicatorCanvas, speed);
speedIndicatorTimerId = setTimeout(() => {
clearSpeedIndicator(speedIndicatorCanvas);
}, duration);
chrome.storage.local
.set({ playbackSpeed: Number(speed.toFixed(1)) })
.then(() => {
console.log("playbackSpeed is set to " + Number(speed.toFixed(1)));
});
}
function increaseSpeed(video, duration) {
chrome.storage.local.get({ playbackSpeed: 1.0 }, (result) => {
playbackSpeed = result.playbackSpeed;
if (playbackSpeed + 0.1 <= 10.0) {
playbackSpeed += 0.1;
setPlaybackSpeed(video, playbackSpeed, duration);
} else {
playbackSpeed = 10.0;
setPlaybackSpeed(video, playbackSpeed, duration);
}
});
}
function decreaseSpeed(video, duration) {
chrome.storage.local.get({ playbackSpeed: 1.0 }, (result) => {
playbackSpeed = result.playbackSpeed;
if (playbackSpeed > 0.1) {
playbackSpeed -= 0.1;
setPlaybackSpeed(video, playbackSpeed, duration);
} else {
playbackSpeed = 0.1;
setPlaybackSpeed(video, playbackSpeed, duration);
}
});
}
function resetSpeed(video, duration) {
playbackSpeed = 1.0;
setPlaybackSpeed(video, playbackSpeed, duration);
}
function drawSpeedIndicator(canvas, speed) {
canvas.style.display = "block";
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "gray";
ctx.font = `${parseInt(canvas.height / 3)}px Sans-Serif`;
ctx.textBaseline = "bottom";
ctx.textAlign = "right";
ctx.maxWidth = canvas.width;
ctx.maxHeight = canvas.height;
const speedText = `${speed.toFixed(1)} x`;
ctx.fillText(speedText, canvas.width, canvas.height);
}
function clearSpeedIndicator(canvas) {
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.style.display = "none";
}