标签搜索

目 录CONTENT

文章目录

油猴脚本屏蔽网页元素

陈铭
2022-12-08 / 0 评论 / 0 点赞 / 444 阅读 / 264 字 / 正在检测是否收录...

安装油猴

去谷歌插件商店打不开,百度一堆,执行安装

新建脚本

主要就是加载了一个外部依赖(Jquery),方便我在脚本内进行元素的选择和过滤,然后根据关键词,屏蔽掉针对的元素

// ==UserScript==
// @name         removeCertainElement
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @require      https://code.jquery.com/jquery-3.4.1.min.js
// @match        http://xxxxx
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @run-at       document-body
// ==/UserScript==


(function() {
    // 加载外部依赖:@require      https://code.jquery.com/jquery-3.4.1.min.js
    // 匹配加载脚本的url:@match        http://xxxx
    // 尽可能快执行脚本:@run-at       document-start
    // body元素加载完再执行脚本:@run-at       document-body
    // 页面加载完再执行脚本:@run-at       document-end

    var words=[
        "关键词1",“关键词2”
    ]
    // 过滤元素
    let children = $("#waterfall").children();
    for (let i = 0; i < children.length; i++) {
        let a = $(children[i]).find("a")[0];
        let title = $(a).attr("title");
        for (let word of words) {
            if (title.indexOf(word)!=-1){
                $(children[i]).hide()
                break
            }
        }
    }
})();
0

评论区