-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskipAds.js
42 lines (34 loc) · 968 Bytes
/
skipAds.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
console.log('skipAds loaded')
function waitYtbAdSkipButton() {
return new Promise((resolve, reject) => {
const maxAttempts = 30;
const interval = 100;
let attempts = 0;
function checkElement() {
attempts++;
const adSkipButton = document.querySelector('.ytp-ad-skip-button');
if (adSkipButton) {
resolve(adSkipButton);
} else if (attempts < maxAttempts) {
setTimeout(checkElement, interval);
} else {
reject();
}
}
checkElement();
});
}
async function skipAd(video) {
video.currentTime = video.duration;
const adSkipButton = await waitYtbAdSkipButton();
if(adSkipButton) {
adSkipButton.click();
}
console.log('ad skiped');
}
function detectContainer() {
const container = document.querySelector('#container.style-scope.ytd-player');
const video = container.querySelector('video');
skipAd(video);
}
window.addEventListener('adAppeared', detectContainer);