YouTube去廣告

簡單高效的YouTube去廣告腳本,拒絕花裏胡哨.如果你有一丟丟編程知識,可以嘗試為常量cssSeletorArr定義元素.如果你有好的建議可以聯系我([email protected]).

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

您需要先安裝使用者腳本管理器擴展,如 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      Remove YouTube AD
// @name:bg      Remove YouTube AD
// @name:cs      Remove YouTube AD
// @name:da      Remove YouTube AD
// @name:de      Remove YouTube AD
// @name:el      Remove YouTube AD
// @name:eo      Remove YouTube AD
// @name:es      Remove YouTube AD
// @name:fi      Remove YouTube AD
// @name:fr      Remove YouTube AD
// @name:fr-CA   Remove YouTube AD
// @name:he      Remove YouTube AD
// @name:hu      Remove YouTube AD
// @name:id      Remove YouTube AD
// @name:ko      Remove YouTube AD
// @name:nb      Remove YouTube AD
// @name:nl      Remove YouTube AD
// @name:pl      Remove YouTube AD
// @name:pt-BR   Remove YouTube AD
// @name:ro      Remove YouTube AD
// @name:ru      Remove YouTube AD
// @name:sk      Remove YouTube AD
// @name:sr      Remove YouTube AD
// @name:sv      Remove YouTube AD
// @name:th      Remove YouTube AD
// @name:tr      Remove YouTube AD
// @name:uk      Remove YouTube AD
// @name:ug      Remove YouTube AD
// @name:vi      Remove YouTube AD
// @name:it      Remove YouTube AD
// @name:ja      YouTubeの広告を削除します
// @name:kr      포함한 YouTube 광고 차단

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

// @description  简单高效的YouTube去广告脚本,拒绝花里胡哨.如果你有一丢丢编程知识,可以尝试为常量cssSeletorArr定义元素.如果你有好的建议可以联系我([email protected]).
// @description:zh-CN   简单高效的YouTube去广告脚本,拒绝花里胡哨.如果你有一丢丢编程知识,可以尝试为常量cssSeletorArr定义元素.如果你有好的建议可以联系我([email protected]).
// @description:zh-TW   簡單高效的YouTube去廣告腳本,拒絕花裏胡哨.如果你有一丟丟編程知識,可以嘗試為常量cssSeletorArr定義元素.如果你有好的建議可以聯系我([email protected]).
// @description:ar      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:bg      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:cs      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:da      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:de      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:el      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:eo      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:es      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:fi      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:fr      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:fr-CA   Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:he      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:hu      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:id      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:ko      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:nb      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:nl      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:pl      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:pt-BR   Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:ro      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:ru      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:sk      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:sr      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:sv      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:th      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:tr      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:uk      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:ug      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:vi      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:it      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:ja      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).
// @description:kr      Simple and efficient YouTube remove AD script, no bells and whistles. If you have a little programming knowledge, you can try to define cssSeletorArr. If you have suggestions, you can contact me ([email protected]).

// @author       FuckAD
// @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`,//首页顶部横幅广告. Homepage banner AD1.
        `ytd-rich-item-renderer.style-scope.ytd-rich-grid-row:has(.ytd-display-ad-renderer)`,//首页视频排版广告. Homepage video layout AD.
        `ytd-rich-section-renderer #dismissible`,//首页中部横幅广告. Homepage banner AD2.
        `.video-ads.ytp-ad-module`,//播放器底部广告. Player bottom AD.
        `tp-yt-paper-dialog:has(yt-mealbar-promo-renderer)`,//播放页会员促销广告. Play page member promotion AD.
        `#related #player-ads`,//播放页评论区右侧推广广告. Play page, right side, promotion AD.
        `#related ytd-ad-slot-renderer`,//播放页评论区右侧视频排版广告. Play page, right side, video layout AD.
    ];

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

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

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

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

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

        }, 16);//主流屏幕刷新率为60hz,此设置与16.666666毫秒每帧对应. The mainstream screen refresh rate is 60hz, which corresponds to 16.666666 milliseconds per frame.


    }

    /**
    * main函数
    */
    function main(){
        generateRemoveAdHTMLElement(generateRemoveAdCssText(cssSeletorArr),`removeAd`);//移除界面中的广告. Remove AD from page.
        removePlayerAd();//移除播放中的广告. Remove AD from player.
    }
    main();

})();