youtube去广告

简单高效的youtube去广告脚本,拒绝花里胡哨。如果你有一丢丢编程知识,可以尝试为常量cssSeletorArr定义元素。如果你有好的建议可以联系我[email protected]

当前为 2023-02-09 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         youtube去广告
// @namespace    http://tampermonkey.net/
// @version      1.31
// @description  简单高效的youtube去广告脚本,拒绝花里胡哨。如果你有一丢丢编程知识,可以尝试为常量cssSeletorArr定义元素。如果你有好的建议可以联系我[email protected]。
// @author       FuckAD
// @match        https://www.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`,//首页顶部横幅广告
        `ytd-rich-item-renderer.style-scope.ytd-rich-grid-row:has(.ytd-display-ad-renderer)`,//首页视频排版广告
        `ytd-rich-section-renderer #dismissible`,//首页中部横幅广告
        `.video-ads.ytp-ad-module`,//播放器底部广告
        `tp-yt-paper-dialog:has(yt-mealbar-promo-renderer)`,//播放页会员促销广告
        `#related #player-ads`,//播放页评论区右侧推广广告
        `#related ytd-ad-slot-renderer`,//播放页评论区右侧视频排版广告
    ];

    /**
    * 生成去除广告的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();

})();