tampermonkey 油猴 自定义去广告模板

 某些网站有一些控件或功能,不想在浏览器中被展现出来。比如各种广告,会员,等。

可根据元素的类名,对象名,xpath路径来去掉,达到网站清爽的目的

下面例例举了baidu,有道,bing

// ==UserScript==
// @name         定制网站去广告
// @namespace    http://tampermonkey.net/
// @version      1.2.4
// @description  定制自己的网站净化
// @author       键盘上的农民工
// @match        https://*.blog.youkuaiyun.com/article/details/*
// @match        https://blog.youkuaiyun.com/*/article/details/*
// @match        https://www.baidu.com/
// @match        https://www.zhihu.com/explore
// @match        https://www.zhihu.com/question/*
// @match        https://fanyi.youdao.com/*
// @match        https://fanyi.baidu.com/*
// @match        https://cn.bing.com/*
// @match        https://bing.com/*
// @match        chrome-extension://bpelnogcookhocnaokfpoeinibimbeff/hitab/*
// @icon         http://zhouql.vip/images/icon/clear.png
// @run-at       document-start
// @grant        none
// @license      MIT
// ==/UserScript==

// 这个版本做了性能优化,简单版本将配置全部注入,这个版本会在特定的网站上注入特定的配置
// 只是配置有了约束,使用了对象配置,对象的key就是网站域名,value是一个数组,数组的值就是需要隐藏的元素
// class名 用 .
// 对象名  用 #
// 支持 xpath,更灵活
(function () {
    // 只需要在此处定义需要隐藏元素
    var clearElementArr = [
        { "fanyi.baidu.com": ['.nhcoTCy6'] },

        { "to8to.com": ['.quoted-outside', '.bottom_slide_box','#nstyle'] },
        // csdn
        { "youkuaiyun.com": ['.passport-container', '.passport-login-mark', '#tool-QRcode'] },
        // 知乎
        { "zhihu.com": ['.css-1izy64v', '.css-ysn1om', '.Modal-wrapper'] },
        // 百度搜索
        { "baidu.com": ['#s-top-left', '#s_top_wrap', '.s-top-right', '#s_main', '.s-bottom-layer-content','.s-isindex-wrap.s-hotsearch-wrapper'] },
        // 有道
        //{ "youdao.com": ['.pop-up-comp', '.download_ch', '.inner-box','.banner-img','.tab-left','.document-upload-entrance-container','.vip-menu-item','.tab-item color_text_3 tab-item-ai gradient-item','//*[@id="app"]/div[1]/div/div[2]/div[2]/div[2]/div/div[1]/div[1]/div[1]/div[2]'] },
        //{ "youdao.com": ['.pop-up-comp', '.download_ch', '.inner-box','.banner-img','.document-upload-entrance-container','.vip-menu-item','.tab-item.color_text_3.tab-item-ai'] },
        { "youdao.com": ['.pop-up-comp', '.download_ch', '.inner-box','.banner-img','.document-upload-entrance-container','.vip-menu-item','.tab-item.color_text_3.tab-item-ai','//*[@id="app"]/div[1]/div/div[2]/div[2]/div[2]/div/div[1]/div[1]/div[1]/div[4]'] },
        // bing
        { "bing.com": ['.scroll_cont','#id_qrcode_popup_positioner','.below_sbox','.modules_wrapper','.bottom_row widget msnpeek_notob nomvs','/html/body/div[1]/div/div[5]/div'] },
    ];

    console.log("准备隐藏以下元素 >>> " + clearElementArr);
    let style = document.createElement("style");
    window.location.href.includes("zhihu.com/question/") ? style.innerText += `html {overflow: auto}` : '';
    window.pageC = function (clearElements) {
        if (typeof (clearElements) === "object") {
            clearElementArr.forEach(o => {
                let key = Object.keys(o);
                if (window.location.href.includes(key)) {
                    o[key].forEach(el => {
                        if (el.startsWith('/')) {
                            setTimeout(() => {
                                hideElementByXPath(el);
                            }, 1000);
                        } else {
                            style.innerText += `${el} {display: none !important;} `;
                        }
                    })
                }
            })
        } else {
            console.error("param error,require array!");
        }
        style.innerText != '' ? document.head.appendChild(style) : '';
    };
    // 使用 XPath 隐藏元素
    function hideElementByXPath(xpath) {
        element = document.evaluate(
            xpath,
            document,
            null,
            XPathResult.FIRST_ORDERED_NODE_TYPE,
            null
        ).singleNodeValue;

        if (element) {
            element.remove();
            console.log(`已删除元素: ${xpath}`);
        } else {
            console.log(`未找到元素: ${xpath}`);
        }
        fixSearchTextBoxBing();
    }
    //固定bing的搜索框
    function fixSearchTextBoxBing(){
        if (window.location.href.indexOf('bing') !== -1) {
            'use strict';
            // 定义固定的宽度(根据你的需求调整)
            const searchBox = document.querySelector('.sbox_cn.sbox_larger'); // 你可以根据需要调整宽度值
            if (searchBox == 0){
                return;
            }
            const initialWidth = window.getComputedStyle(searchBox).width;
            // 创建一个 <style> 元素来注入 CSS
            const style = document.createElement('style');
            style.textContent = `
            /* 针对初始状态 */
            .sbox_cn.sbox_larger {
                width: ${initialWidth} !important;
            }
            .sb_form.hassbi{
                width: ${initialWidth} !important;
            }
            /* 针对点击后的状态 */
            .sbox_cn.sbox_larger.as_exp,
            .sbox_cn.sbox_larger.as_show {
                width: ${initialWidth} !important;
            }
            .sb_form.hassbi.as_exp,
            .sb_form.hassbi.as_show {
                width: ${initialWidth} !important;
            }
           `;
            document.head.appendChild(style);
        }
    }
    window.pageC(clearElementArr);
    console.log("清理完成!");


    // 获取需要修改的元素
    var element = document.evaluate('/html/body/div[8]/div/section/div[2]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    // 修改元素的样式
    if(element){
        element.style.width = '1000px';
        element.style.height = '1000px';
    }
    //csdn
    'use strict';
    if (window.location.href.indexOf('csdn') !== -1) {
        GM_addStyle(`pre,code{user-select:auto !important}`)
        //获取所有代码块
        let codes=document.querySelectorAll("code");
        //遍历所有代码块
        codes.forEach(c=>{
            c.contentEditable="true";
        })
    }

})();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值