YTAdSkip

Skips YouTube pre-roll ads as fast as possible

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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 });
})();