-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
52 lines (47 loc) · 1.85 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
(function() {
// Function to change the playback speed to 1.5
function setDefaultSpeed() {
const video = document.querySelector('video');
if (video && video.playbackRate !== 1.5) {
console.log('Setting playback speed to 1.5');
video.playbackRate = 1.5;
}
}
// Listen for changes to the video's playback speed
function monitorSpeedChange() {
const video = document.querySelector('video');
if (video) {
video.onratechange = function(event) {
if (video.playbackRate !== 1.5) {
console.log(`User changed speed to ${video.playbackRate}, stopping automatic change`);
// Remove the listener since the user has changed the speed
video.removeEventListener('playing', setDefaultSpeedOnStart);
}
};
}
}
// Function to set the speed when a video starts playing
function setDefaultSpeedOnStart() {
setDefaultSpeed();
monitorSpeedChange();
}
// Attach to the 'playing' event to handle new videos or page changes
document.addEventListener('DOMContentLoaded', () => {
const video = document.querySelector('video');
if (video) {
video.addEventListener('playing', setDefaultSpeedOnStart);
}
});
// Handle possible Ajax navigation on YouTube (when URL changes without full page reload)
let lastUrl = location.href;
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
console.log('URL changed, setting default speed for new content');
setDefaultSpeed();
}
}).observe(document, {subtree: true, childList: true});
// Initial setup in case the video is already present
setDefaultSpeed();
})();