Main Question: How to close the ad in Javascript?
I used to use a userscript on YouTube that I made that would click the skip ad button after the 5-second required time so I don’t violate YouTube’s terms.
Here is the Code:
// ==UserScript==
// @name By Mohtady
// @namespace http://tampermonkey.net/
// @version 2024-07-20
// @description By Mohtady!
// @author You
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
function getRandomInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
setInterval(function() {
let skipButton = document.getElementsByClassName("ytp-skip-ad-button");
if (skipButton != undefined && skipButton.length > 0) {
setTimeout(() => {
skipButton[0].click();
// after 5 sec
}, 5000);
// ad found!
}
console.log("Running");
}, getRandomInterval(1000, 3000));
})();
And when I tried to run document.getElementsByClassName("button")[0].click()
in the console it found the button (document.getElementsByClassName("button")[0]
) but when it tried to click it, it did nothing.
Literally Nothing, no errors and it did nothing I could see on the webpage and the ad was still running.
I expected it to be an easy matter but it seems not!
Did YouTube change the way it reads button clicks?
Is anyone facing the same problem?