YTAdSkip

Skips YouTube pre-roll ads as fast as possible

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         YTAdSkip
// @description  Skips YouTube pre-roll ads as fast as possible
// @version      1.2
// @author       Gerg0Vagyok
// @match        https://www.youtube.com/watch*
// @license      MIT
// @run-at       document-start
// @namespace    https://lmao.sbs
// ==/UserScript==

(function () {
	// Deletes overlays
	function DeleteOverlays() {
		document.querySelector('div.video-ads.ytp-ad-module')?.remove();
		document.querySelector("div#player-ads.style-scope.ytd-watch-flexy")?.remove()
		document.querySelector("div#panels.style-scope.ytd-watch-flexy")?.remove()
	}

	// Adds a event listener to skip ads, deletes overlays too
	function SetupAdSkip() {
		const player = document.getElementById('movie_player');
		const video = document.querySelector('video');
		if (!player || !video) return false;

		DeleteOverlays();
		video.addEventListener('durationchange', () => {
			DeleteOverlays();
			if (player.classList.contains('ad-showing') && video.duration) {
				console.log("[YTAdSkip] Skipping ad");
				video.currentTime = video.duration;

				// Pause and unpause with a slight delay to go to actual video
				setTimeout(() => player.pauseVideo(), 10);
				setTimeout(() => player.playVideo(), 20);
			}
		});

		return true;
	}

	// Wait until movie_player and the video player is in the DOM
	const observer = new MutationObserver(() => {
		if (SetupAdSkip()) observer.disconnect();
	});
	observer.observe(document, { childList: true, subtree: true });
})();