-
Notifications
You must be signed in to change notification settings - Fork 3
/
content.js
64 lines (55 loc) · 1.98 KB
/
content.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
(function () {
const speeds = [1, 1.25, 1.5, 1.75, 2, 2.5, 3];
let buttonsContainer;
function createSpeedButtons() {
if (buttonsContainer) return; // Prevent duplicate buttons
// Select the YouTube video player
const player = document.querySelector('#below');
if (!player) return;
// Create a container for the buttons
buttonsContainer = document.createElement('div');
buttonsContainer.style.position = 'absolute';
buttonsContainer.style.top = '-4px';
buttonsContainer.style.right = '0px';
buttonsContainer.style.zIndex = '9999';
buttonsContainer.style.display = 'flex';
buttonsContainer.style.flexDirection = 'row';
// Create buttons for each speed
speeds.forEach(speed => {
const button = document.createElement('button');
button.textContent = `${speed}x`;
button.style.margin = '2px';
button.style.padding = '5px';
button.style.fontSize = '14px';
button.style.backgroundColor = '#ffffffcc';
button.style.border = '1px solid #ccc';
button.style.borderRadius = '4px';
button.style.cursor = 'pointer';
button.style.width = '48px';
button.style.margin = '2px';
// Set the video playback speed when clicked
button.addEventListener('click', () => {
const video = document.querySelector('video');
if (video) {
video.playbackRate = speed;
}
});
buttonsContainer.appendChild(button);
});
// Append the container to the player
player.appendChild(buttonsContainer);
}
function checkForPlayer() {
const player = document.querySelector('.html5-video-player');
if (player && !buttonsContainer) {
createSpeedButtons();
}
}
// Observe changes in the DOM to handle navigation within YouTube
const observer = new MutationObserver(() => {
checkForPlayer();
});
observer.observe(document.body, { childList: true, subtree: true });
// Initial check when the script loads
checkForPlayer();
})();