-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-script.js
132 lines (106 loc) · 3.54 KB
/
content-script.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
// skip button class
const skipBtnClass = 'ytp-ad-skip-button';
// player screen id
const playerId = 'player-theater-container';
// player tag name
const playerTagName = 'ytd-player';
// ad banner close button classname
const adBannerCloseBtnClass = 'ytp-ad-overlay-close-button';
// continue watching banner submit button
const continueWatchBtnClass = 'style-scope yt-confirm-dialog-renderer style-blue-text size-default';
// youtube URL
const youtubeUrl = 'www.youtube.com';
// interval
let pollingIntervalInstance = null;
// Counter
let skippedAds = 0;
let closedBanner = 0;
let closedContinueWatchBanner = 0;
// check if site is youtube
function isYoutubeURL() {
const currentUrl = window.location.hostname;
return currentUrl == youtubeUrl;
}
// check if any video is playing
function isVideoPlayerScreen() {
// video player screen
const player = document.getElementById(playerId);
return !!player;
}
function debounced(delay, fn) {
let timerId;
return function (...args) {
if (timerId) {
clearTimeout(timerId);
}
timerId = setTimeout(() => {
fn(...args);
timerId = null;
}, delay);
}
}
function skipAdvertisement() {
if(!!pollingIntervalInstance) {
clearInterval(pollingIntervalInstance);
}
pollingIntervalInstance = setInterval(function() {
// skip youttube ads
const skipButtons = document.getElementsByClassName(skipBtnClass);
// ad skip button exists
if (skipButtons.length > 0) {
// click on the button & skip ad
skipButtons[0].click();
// update counter
skippedAds++;
// clear old counter logs and log new counter
console.clear();
console.log('%cSkip Youtube Ads: We just skipped ad number %d for you. Cheers.', 'color: green; font-size: 15px', skippedAds);
}
// ad banner exists (overlay banner on video)
const adBannerCloseBtn = document.getElementsByClassName(adBannerCloseBtnClass);
// if ad banner exist with close btn
if(adBannerCloseBtn.length > 0) {
// click and close the banner
adBannerCloseBtn[0].click();
// update counter
closedBanner++
// clear old counter logs and log new counter
console.clear();
console.log('%cSkip Youtube Ads: We just closed banner number %d for you. Cheers.', 'color: purple; font-size: 15px', closedBanner);
}
// close continue watching banner and play video
const continueWatchBtn = document.getElementsByClassName(continueWatchBtnClass);
// if continue watching banner exist
if(continueWatchBtn.length > 0) {
// click button and close banner
continueWatchBtn[0].click();
// update counter
closedContinueWatchBanner++;
console.log('%cSkip Youtube Ads: We just closed continue watch banner number %d for you. Cheers.', 'color: blue; font-size: 15px', closedContinueWatchBanner);
}
}, 2000);
}
const skipAdHandler = debounced(1000, skipAdvertisement)
function observeNode(node) {
// observer config
const config = { attributes: true, childList: true, subtree: true };
// dom change observer
const observer = new MutationObserver(skipAdHandler);
// register the observer
observer.observe(node, config);
}
function main() {
// register observer on document
observeNode(document);
const isPlayerScreen = isVideoPlayerScreen();
if(isPlayerScreen) {
skipAdvertisement();
}
}
function messageListener(message) {
main();
}
// on window load
window.onload = main()
// background message listener
chrome.runtime.onMessage = messageListener(message);