YouTube去廣告

這個腳本可以移除所有廣告,甚至是視頻廣告.簡單高效的YouTube去廣告腳本,拒絕花裏胡哨.你可以嘗試為常量cssSeletorArr定義元素.

目前為 2023-02-14 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube去广告
// @name:zh-CN   YouTube去广告
// @name:zh-TW   YouTube去廣告
// @name:ar      YouTube AD Blocker
// @name:bg      YouTube AD Blocker
// @name:cs      YouTube AD Blocker
// @name:da      YouTube AD Blocker
// @name:de      YouTube AD Blocker
// @name:el      YouTube AD Blocker
// @name:eo      YouTube AD Blocker
// @name:es      YouTube AD Blocker
// @name:fi      YouTube AD Blocker
// @name:fr      YouTube AD Blocker
// @name:fr-CA   YouTube AD Blocker
// @name:he      YouTube AD Blocker
// @name:hu      YouTube AD Blocker
// @name:id      YouTube AD Blocker
// @name:ko      YouTube AD Blocker
// @name:nb      YouTube AD Blocker
// @name:nl      YouTube AD Blocker
// @name:pl      YouTube AD Blocker
// @name:pt-BR   YouTube AD Blocker
// @name:ro      YouTube AD Blocker
// @name:ru      YouTube AD Blocker
// @name:sk      YouTube AD Blocker
// @name:sr      YouTube AD Blocker
// @name:sv      YouTube AD Blocker
// @name:th      YouTube AD Blocker
// @name:tr      YouTube AD Blocker
// @name:uk      YouTube AD Blocker
// @name:ug      YouTube AD Blocker
// @name:vi      YouTube AD Blocker
// @name:it      YouTube AD Blocker
// @name:ja      YouTubeの広告を削除します
// @name:kr      포함한 YouTube 광고 차단

// @namespace    http://tampermonkey.net/
// @version      1.37

// @description         这个脚本可以移除所有广告,甚至是视频广告.简单高效的YouTube去广告脚本,拒绝花里胡哨.你可以尝试为常量cssSeletorArr定义元素.
// @description:zh-CN   这个脚本可以移除所有广告,甚至是视频广告.简单高效的YouTube去广告脚本,拒绝花里胡哨.你可以尝试为常量cssSeletorArr定义元素.
// @description:zh-TW   這個腳本可以移除所有廣告,甚至是視頻廣告.簡單高效的YouTube去廣告腳本,拒絕花裏胡哨.你可以嘗試為常量cssSeletorArr定義元素.
// @description:ar      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:bg      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:cs      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:da      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:de      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:el      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:eo      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:es      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:fi      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:fr      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:fr-CA   The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:he      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:hu      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:id      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:ko      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:nb      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:nl      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:pl      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:pt-BR   The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:ro      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:ru      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:sk      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:sr      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:sv      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:th      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:tr      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:uk      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:ug      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:vi      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:it      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:ja      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 
// @description:kr      The script removes all ads, even video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr. 

// @author       iamfugui
// @match        *://*.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=YouTube.com
// @grant        none
// @license MIT
// ==/UserScript==
(function() {
    'use strict';

    //界面广告选择器
    const cssSeletorArr = [
        `#masthead-ad`,//首页顶部横幅广告. Banner AD at the top of the homepage.
        `ytd-rich-item-renderer.style-scope.ytd-rich-grid-row:has(.ytd-display-ad-renderer)`,//首页视频排版广告. Video layout AD in the homepage.
        `ytd-rich-section-renderer #dismissible`,//首页中部横幅广告. Banner AD in the middle of the homepage.
        `.video-ads.ytp-ad-module`,//播放器底部广告. AD at the bottom of the player.
        `tp-yt-paper-dialog:has(yt-mealbar-promo-renderer)`,//播放页会员促销广告. Member promotion AD on the play page.
        `#related #player-ads`,//播放页评论区右侧推广广告. Promotion AD on the right side of the play page.
        `#related ytd-ad-slot-renderer`,//播放页评论区右侧视频排版广告. Video layout AD on the right side of the play page.
        `ytd-search-pyv-renderer:has(ytd-ad-slot-renderer)`,//搜索页广告.  Search AD.
        `ytd-item-section-renderer:has(ytd-ad-slot-renderer)`//搜索页广告2.  Search AD2.
    ];

    /**
    * 生成去除广告的css元素style并附加到HTML节点上
    * @param {String} styles 样式文本
    * @param {String} styleId 元素id
    * @return {undefined}
    */
    function generateRemoveADHTMLElement(styles,styleId) {
        //如果已经设置过,退出.
        if (document.getElementById(styleId)) {
            return false
        }

        //设置移除广告样式.
        let style = document.createElement(`style`);//创建style元素.
        style.id = styleId;
        (document.querySelector(`head`) || document.querySelector(`body`)).appendChild(style);//将节点附加到HTML.
        style.appendChild(document.createTextNode(styles));//附加样式节点到元素节点.
    }

    /**
    * 生成去除广告的css文本
    * @param {Array} cssSeletorArr 待设置css选择器数组
    * @return {String}
    */
    function generateRemoveADCssText(cssSeletorArr){
        cssSeletorArr.forEach((seletor,index)=>{
            cssSeletorArr[index]=`${seletor}{display:none!important}`;//遍历并设置样式.
        });
        return cssSeletorArr.join(" ");//拼接成字符串.
    }

    /**
    * 去除播放中的广告
    * @return {undefined}
    */
    function removePlayerAD(){
        let timerId =setInterval(function(){
            //拥有跳过按钮的广告. 
            let skipButton = document.querySelector(`.ytp-ad-skip-button`);
            if(skipButton)
            {
                skipButton.click();// 跳过广告.
                return false;//防止后面错判.
            }

            //片头短广告.
            let ADShortMsg = document.querySelector(`.video-ads.ytp-ad-module .ytp-ad-player-overlay`);
            if(ADShortMsg){
                location.href = location.href;//重新加载.
                clearInterval(timerId);
            }

        }, 16);//主流屏幕刷新率为60hz,此设置与16.666666毫秒每帧对应. 
    }

    /**
    * main函数
    */
    function main(){
        generateRemoveADHTMLElement(generateRemoveADCssText(cssSeletorArr),`removeAD`);//移除界面中的广告.
        removePlayerAD();//移除播放中的广告.
    }
    main();

})();