-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
71 lines (58 loc) · 1.92 KB
/
content-script.js
File metadata and controls
71 lines (58 loc) · 1.92 KB
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
// Constants for ad-related settings
const AD_PLAYBACK_RATE = 16;
const AD_MUTE_SETTING = true;
// Function to check if an ad is playing in the video player
function isAdPlaying(videoPlayer) {
return (
videoPlayer.classList.contains("ad-showing") ||
videoPlayer.classList.contains("ad-interrupting")
);
}
// Function to adjust video playback settings during ads
function adjustAdPlayback(videoElement) {
videoElement.playbackRate = AD_PLAYBACK_RATE;
videoElement.muted = AD_MUTE_SETTING;
}
// Function to handle ad state changes
function handleAdStateChange(videoPlayer) {
const videoElement = videoPlayer.querySelector("video");
if (videoElement && isAdPlaying(videoPlayer)) {
adjustAdPlayback(videoElement);
}
}
// Function to skip ad if possible
function trySkipAd() {
const adSkipButtonContainer = document.querySelector('.ytp-ad-skip-button-container');
if (adSkipButtonContainer?.style.display !== 'none') {
const skipButton = adSkipButtonContainer.querySelector('.ytp-ad-skip-button-modern');
if (skipButton) {
skipButton.click();
}
}
}
// Function to process each mutation
function processMutation(mutation) {
if (mutation.attributeName === "style") {
trySkipAd();
}
handleAdStateChange(mutation.target);
}
// Function to initialize the ad observer
function initializeAdObserver() {
const videoPlayer = document.querySelector("#movie_player");
if (videoPlayer) {
handleAdStateChange(videoPlayer);
const observer = new MutationObserver((mutations) => {
mutations.forEach(processMutation);
});
const config = {
attributes: true,
subtree: true,
attributeFilter: ["class", 'style'],
};
observer.observe(videoPlayer, config);
} else {
setTimeout(initializeAdObserver, 50);
}
}
initializeAdObserver();