Greasy Fork

Greasy Fork is available in English.

YouTube Bỏ qua quảng cáo video tự động

Tự động bỏ qua quảng cáo trên YouTube

当前为 2024-12-23 提交的版本,查看 最新版本

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

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

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

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

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

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name              YouTube Bỏ qua quảng cáo video tự động
// @name:en           YouTube Auto Ad Skipper
// @name:vi           YouTube Bỏ qua quảng cáo video tự động
// @name:zh-cn        YouTube 自动跳过广告
// @name:zh-tw        YouTube 自動跳過廣告
// @name:ja           YouTube 広告自動スキップ
// @name:ko           YouTube 자동 광고 건너뛰기
// @name:es           YouTube Saltar anuncios automáticamente
// @name:ru           YouTube Автоматический пропуск рекламы
// @name:id           YouTube Lewati Iklan Otomatis
// @name:hi           YouTube स्वचालित विज्ञापन स्किपर
// @namespace         http://tampermonkey.net/
// @version           2024.12.23.2
// @description       Tự động bỏ qua quảng cáo trên YouTube
// @description:en    Automatically skip ads on YouTube videos
// @description:vi    Tự động bỏ qua quảng cáo trên YouTube
// @description:zh-cn 自动跳过 YouTube 视频广告
// @description:zh-tw 自動跳過 YouTube 影片廣告
// @description:ja    YouTube動画の広告を自動的にスキップ
// @description:ko    YouTube 동영상의 광고를 자동으로 건너뛰기
// @description:es    Salta automáticamente los anuncios en videos de YouTube
// @description:ru    Автоматически пропускает рекламу в видео на YouTube
// @description:id    Otomatis melewati iklan di video YouTube
// @description:hi    YouTube वीडियो में विज्ञापनों को स्वचालित रूप से छोड़ें
// @author            RenjiYuusei
// @license           MIT
// @icon              https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @match             https://*.youtube.com/*
// @grant            GM_addStyle
// @grant            GM_getValue
// @grant            GM_setValue
// @run-at           document-start
// @compatible chrome
// @compatible firefox
// @compatible edge
// ==/UserScript==

(function () {
	'use strict';

	const CONFIG = {
		allowedReloadPage: true,
		dontReloadWhileBusy: true,
		maxScrollThreshold: 200,
		adSkipDelay: 1000,
		maxPlaybackRate: 16,
	};

	class YouTubeAdSkipper {
		constructor() {
			this.video = null;
			this.currentVideoTime = 0;
			this.isTabBlurred = false;
			this.skipAttempts = 0;
			this.maxSkipAttempts = 5;
			this.init();
		}

		init() {
			this.setupEventListeners();
			this.setupMutationObserver();
			this.addCSSHideAds();
			this.skipAd();
		}

		setupEventListeners() {
			window.addEventListener('blur', () => (this.isTabBlurred = true));
			window.addEventListener('focus', () => (this.isTabBlurred = false));

			document.addEventListener(
				'timeupdate',
				e => {
					if (e.target.matches('video.html5-main-video')) {
						this.currentVideoTime = e.target.currentTime;
					}
				},
				true
			);
		}

		setupMutationObserver() {
			const observer = new MutationObserver(() => this.skipAd());
			observer.observe(document.body, {
				attributes: true,
				attributeFilter: ['class', 'src'],
				childList: true,
				subtree: true,
			});
		}

		async skipAd() {
			if (window.location.pathname.startsWith('/shorts/')) return;

			const player = document.querySelector('#movie_player');
			if (!player) return;

			const hasAd = player.classList.contains('ad-showing');
			this.video = player.querySelector('video.html5-main-video');

			if (hasAd && this.video) {
				await this.handleVideoAd();
			}

			this.removeAdBlockerWarnings();
			this.removeShortVideoAds();
		}

		async handleVideoAd() {
			const skipButton = document.querySelector(`
                .ytp-skip-ad-button,
                .ytp-ad-skip-button,
                .ytp-ad-skip-button-modern,
                .ytp-ad-survey-answer-button
            `);

			if (skipButton) {
				skipButton.click();
				skipButton.remove();
			} else if (this.video.src) {
				this.video.currentTime = 9999;
				this.video.playbackRate = CONFIG.maxPlaybackRate;
			}

			this.video.muted = true;
			this.video.volume = 0;

			if (this.skipAttempts < this.maxSkipAttempts) {
				this.skipAttempts++;
				await new Promise(resolve => setTimeout(resolve, CONFIG.adSkipDelay));
				this.skipAd();
			}
		}

		removeAdBlockerWarnings() {
			const warnings = ['tp-yt-paper-dialog:has(#feedback.ytd-enforcement-message-view-model)', '.yt-playability-error-supported-renderers:has(.ytd-enforcement-message-view-model)'];

			warnings.forEach(selector => {
				const warning = document.querySelector(selector);
				if (warning) {
					if (selector.includes('playability-error') && this.checkCanReloadPage()) {
						this.reloadPage();
					}
					warning.remove();
				}
			});
		}

		removeShortVideoAds() {
			document.querySelectorAll('ytd-reel-video-renderer:has(.ytd-ad-slot-renderer)').forEach(ad => ad.remove());
		}

		checkCanReloadPage() {
			if (!CONFIG.allowedReloadPage) return false;
			if (!CONFIG.dontReloadWhileBusy) return true;
			if (document.activeElement?.matches('input, textarea, select')) return false;
			if (document.documentElement.scrollTop > CONFIG.maxScrollThreshold) return false;
			return true;
		}

		reloadPage() {
			const params = new URLSearchParams(location.search);
			if (this.currentVideoTime > 0) {
				params.set('t', Math.floor(this.currentVideoTime) + 's');
			}
			location.replace(`${location.origin}${location.pathname}?${params.toString()}`);
		}

		addCSSHideAds() {
			const styles = `
                #player-ads,
                #masthead-ad,
                ytd-ad-slot-renderer,
                ytd-rich-item-renderer:has(.ytd-ad-slot-renderer),
                ytd-rich-section-renderer:has(.ytd-statement-banner-renderer),
                ytd-reel-video-renderer:has(.ytd-ad-slot-renderer),
                tp-yt-paper-dialog:has(#feedback.ytd-enforcement-message-view-model),
                tp-yt-paper-dialog:has(> ytd-checkbox-survey-renderer),
                .ytp-suggested-action,
                .yt-mealbar-promo-renderer,
                ytmusic-mealbar-promo-renderer,
                ytmusic-statement-banner-renderer,
                .ytd-display-ad-renderer,
                .ytd-statement-banner-renderer,
                .ytd-in-feed-ad-layout-renderer {
                    display: none !important;
                }
            `;
			GM_addStyle(styles);
		}
	}

	new YouTubeAdSkipper();
})();