Skip to content

Instantly share code, notes, and snippets.

@ktakeda47
Last active October 13, 2023 18:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktakeda47/400d8cbb38031da370af88e6b2ae4937 to your computer and use it in GitHub Desktop.
Save ktakeda47/400d8cbb38031da370af88e6b2ae4937 to your computer and use it in GitHub Desktop.
A user script that automatically "presses" YouTube's "skip ads" button.
// ==UserScript==
// @name AutoClickYouTubeSkipAdButton
// @namespace net.maiware.userscript
// @description user.js for click "Skip Ad" button automatically.
// @version 1.1.4
// @grant none
// @match https://www.youtube.com/*
// @license MIT
// ==/UserScript==
/*jshint esversion: 6 */
(function () {
// callback for `MutationObserver` constructor
const callback = (mutationRecords, observer) => {
const selectors = [".ytp-ad-skip-button", ".ytp-ad-skip-button-modern"];
selectors.forEach((selector) => {
const skipButtons = document.querySelectorAll(selector);
for (const skipButton of skipButtons) {
skipButton.click();
}
})
// make overlay-ads hidden
const overlayAds = document.querySelectorAll(".ytp-ad-overlay-slot");
for (const overlayAd of overlayAds) {
overlayAd.style.visibility = "hidden";
}
};
// observer instance
const observer = new MutationObserver(callback);
// observation options
const options = {
childList: true,
subtree: true,
};
// start MutationObserver#observe()
const startObservation = (targetNode) => {
if (targetNode) {
observer.observe(targetNode, options);
}
};
const target = document.documentElement;
startObservation(target);
})();
@ktakeda47
Copy link
Author

@ktakeda47
Copy link
Author

Updated the script as the class name of the <button> seems to have changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment