-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuto-Skip-Ad.user.js
106 lines (88 loc) · 2.84 KB
/
Auto-Skip-Ad.user.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
// ==UserScript==
// @name Auto Skip Ad
// @namespace http://tampermonkey.net/
// @version 0.2
// @description automatic skip youtube ads!
// @author tonmoydeb404
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @downloadURL https://github.com/tonmoydeb404/auto-skip-ad/raw/main/Auto-Skip-Ad.user.js
// @updateURL https://github.com/tonmoydeb404/auto-skip-ad/raw/main/Auto-Skip-Ad.user.js
// @grant none
// ==/UserScript==
/*
# Title : Auto Skip Ad
# Description : automatic skip youtube ads!
# Author : Tonmoy Deb ([email protected])
# Date : 17/10/2023
*/
(function () {
"use strict";
// constant values.
const AD_CLASS = "ad-showing";
const SKIP_TIME = 5000;
const SKIP_BUTTON_CLASS = "ytp-skip-ad-button";
/**
* get the video element
* @returns {Element}
*/
const getVideoElem = () => document.querySelector("video.html5-main-video");
/**
* Automatic AD handler: Mute AD, Skip AD
* @param {Element} media
*/
const adHandler = (media) => {
// movie player element
const moviePlayer = document.querySelector("#movie_player");
// check Ad is currently playing or not
if (moviePlayer.classList.contains(AD_CLASS)) {
// mute the ad
media.muted = true;
// waiting to click skip button
setTimeout(() => {
// make sure skip button is available & not disabled
const skipBtn = document.querySelector(`button.${SKIP_BUTTON_CLASS}`);
if (!skipBtn) return;
// skip the ad
skipBtn.click();
}, SKIP_TIME);
} else {
// unmute video
media.muted = false;
}
};
/**
* check url contains "youtube.com/watch" string or not
* @param {string} url
* @returns {Boolean}
*/
const urlChecker = (url) => url.includes("youtube.com/watch");
/**
* Detect page url change and update video event listener
*/
const observeUrlChange = () => {
// variable for previous url. initially it will be undefined
let prev_url = undefined;
const observer = new MutationObserver((_mutations) => {
const current_url = document.location.href;
// check previous url and check url changed or not
if (prev_url && prev_url === current_url) return;
// if url changed then update the previous url
prev_url = current_url;
if (!urlChecker(current_url)) return;
// add event listener to the video
const mainVideo = getVideoElem();
if (!mainVideo) return;
mainVideo.oncanplay = (e) => adHandler(e.target);
});
observer.observe(document.documentElement, {
childList: true,
subtree: true,
});
};
// assign observer on window load
window.onload = () => {
if (urlChecker(window.location.href)) adHandler(getVideoElem());
observeUrlChange();
};
})();