Auto Skip YouTube Ads

Auto skip YouTube ads instantly. Very lightweight and efficient.

目前為 2024-06-21 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Auto Skip YouTube Ads
// @name:vi      Tự Động Bỏ Qua Quảng Cáo YouTube
// @namespace    https://github.com/tientq64/userscripts
// @version      2.1.3
// @description  Auto skip YouTube ads instantly. Very lightweight and efficient.
// @description:vi  Tự động bỏ qua quảng cáo YouTube ngay lập tức. Rất nhẹ và hiệu quả.
// @author       https://github.com/tientq64
// @icon         https://cdn-icons-png.flaticon.com/64/9639/9639954.png
// @match        https://www.youtube.com/*
// @grant        none
// @license      MIT
// @noframes
// @homepage     https://github.com/tientq64/userscripts/tree/main/scripts/auto-skip-youtube-ads
// ==/UserScript==

function skipAd() {
	const hasAd = player.classList.contains('ad-showing')
	if (hasAd) {
		const skipButton = document.querySelector('.ytp-skip-ad-button, .ytp-ad-skip-button')
		if (skipButton) {
			skipButton.click()
		} else {
			const video = player.querySelector('video')
			video.currentTime = video.duration
		}
	}
	if (popupContainer) {
		const dismissButton = popupContainer.querySelector('tp-yt-paper-dialog #dismiss-button')
		if (dismissButton) {
			dismissButton.click()
			const dialog = dismissButton.closest('tp-yt-paper-dialog')
			dialog.remove()
		}
	}
}
const player = document.querySelector('.html5-video-player')
let popupContainer
if (window.MutationObserver) {
	const adObserver = new MutationObserver(skipAd)
	adObserver.observe(player, { attributeFilter: ['class'] })
} else {
	setInterval(skipAd, 1000)
}
skipAd()
const intervalId = setInterval(tryFindPopupContainer, 100)
function tryFindPopupContainer() {
	popupContainer = document.querySelector('ytd-popup-container')
	if (!popupContainer) return
	clearInterval(intervalId)
	if (window.MutationObserver) {
		const warningObserver = new MutationObserver(skipAd)
		warningObserver.observe(popupContainer, { childList: true })
	}
	skipAd()
}
const style = document.createElement('style')
style.textContent = `
	#player-ads,
	#masthead-ad,
	ytd-rich-item-renderer:has(.ytd-ad-slot-renderer),
	ytd-reel-video-renderer:has(.ytd-ad-slot-renderer),
	tp-yt-paper-dialog:has(#dismiss-button) {
		display: none !important;
	}`
document.head.appendChild(style)