YTAdSkip

Skips YouTube pre-roll ads as fast as possible

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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 });
})();