欢迎光临
我们一直在努力

搜索结果过滤,通过关键字屏蔽百度搜索中的垃圾结果【油猴脚本】

屏蔽垃圾网站,如CSDN。

没装油猴的可以百度搜索【Tampermonkey 安装】

油猴脚本:

由于百度搜索结果加密了链接,不能直接通过链接判断。

所以这里,我直接用一些特征关键字判断了!

// ==UserScript==
// @name        通过关键字屏蔽百度搜索结果
// @author      www.itblogcn.com
// @version     1.0
// @update      2021-09-02
// @description 通过关键字屏蔽百度搜索结果
// @match       https://www.baidu.com
// @match       https://www.baidu.com/s*
// @require     http://code.jquery.com/jquery-migrate-1.2.1.min.js
// @grant       none
// ==/UserScript==
//'use strict';
(function () {

    // 屏蔽标题(包含关键字则屏蔽)
    const titleArr = ["软件园", "下载之家", "下载网", "百度健康", "快速问医生", "求医网", "求医问药", "家庭医生"]

    // 屏蔽标题(匹配正则则屏蔽)
    const titleRegexArr = ["- CSDN$"]

    // 屏蔽描述(包含关键字则屏蔽)
    const descArr = ["csdn已为您找到", "也称为可见性", "为您推荐的内容", "阿里云为您提供", "医疗咨询服务"]

    // 屏蔽来源(包含关键字则屏蔽)
    const sourceArr = ["CSDN技术社区", "csdn.net", "百度知道", "腾讯云计算", "so.com",
        "百度文库", "greenxf.com", "sd173.com", "当下软件园", "ucbug.com", "luxurydz.com",
        "pc6.com", "epinv.com", "pconline.com.cn", "7xdown.com",
        "liangchan.net", "32r.com", "fxxz.com", "xue51.com", "东坡下载站",
        "qqtn.com", "xz7.com", "wmzhe.com", "downcc.com", "cr173.com", "系统之家", "minixiazai.com",
        "downkuai.com", "iteye.com", "zdfans.com", "upantool.com", "win7xzb.com", "epinv.com",
        "cr173.com", "greenxf.com", "duote.com", "软件园", "appcgn.com", "anxz.com",
        "xitongcheng.com", "ddooo.com", "gpsmap.cc", "dugoogle.com", "之家", "veryhuo.com", "xzji.com",
        "kxdw.com", "winwin7.com", "2265.com", "rz520.com", "shuajizhijia.net", "chaoxz.com",
        "runker.net", "clinicmed.net", "siemens-home.cn", "ps123.net", "itmop.com", "y8l.com",
        "yiwan.com", "81857.net", "52jingyan.com", "cncrk.com", "weidown.com", "qh24.com", "962.net",
        "121down.com", "pw88.com", "greenxiazai.com", "/soft/", "软件", "下载网", "寻医",
        "健康", "健客网", "医生", "柠檬爱美", "紫一商城", "120.net", "求医", "宝宝知道"]

    let timer = undefined;

    window.exec = function () {

        console.log("%c执行屏蔽搜索结果script", "color: red;")

        // 文章列表
        let contentLeft = document.getElementById("content_left")

        if (!contentLeft) {
            console.log("%c非搜索页...", "color: red;")
            return
        }

        // let documents = contentLeft.getElementsByClassName("result");
        let documents = $(contentLeft).find('div[class*="result"]');
        console.log("当前文章数:%s", documents.length)
        console.log(documents)
        for (let i = documents.length - 1; i >= 0; i--) {
            console.log("--------------------------------------")
            isRemove(documents[i]);
        }
    }

    function isRemove(document) {

        let title = getTitle(document)
        console.log("title: " + title);
        if (title) {
            for (let element of titleRegexArr) {
                let regexp = new RegExp(element);
                if (regexp.test(title)) {
                    document.remove();
                    console.warn("remove by title regex[%s]", element)
                    return;
                }
            }
            for (let element of titleArr) {
                if (title.indexOf(element) != -1) {
                    document.remove();
                    console.warn("remove by title[%s]", element)
                    return;
                }
            };
        }

        let desc = getDesc(document);
        console.log("desc: " + desc);
        if (desc) {
            for (let element of descArr) {
                if (desc.indexOf(element) != -1) {
                    document.remove();
                    console.warn("remove by desc[%s]", element)
                    return;
                }
            };
        }

        let source = getSource(document);
        console.log("source: " + source);
        if (source) {
            for (let element of sourceArr) {
                if (source.indexOf(element) != -1) {
                    document.remove();
                    console.warn("remove by source[%s]", element)
                    return;
                }
            };
        }
    }

    /**
     * 文章标题
     * @param {*} element
     * @returns
     */
    function getTitle(element) {
        try {
            return element.getElementsByClassName("t")[0].innerText;
        } catch (error) {
            console.error("get title error.")
        }
        return "";
    }

    /**
     * 文章描述
     * @param {*} element
     * @returns
     */
    function getDesc(element) {
        try {
            // return element.getElementsByClassName("c-abstract")[0].innerText;
            return $(element).find('span[class*="content"],.c-abstract')[0].innerText;
        } catch (error) {
            console.error("get desc error.")
        }
        return "";
    }

    /**
     * 文章来源
     * @param {*} element
     * @returns
     */
    function getSource(element) {
        try {
            // let source = element.getElementsByClassName("user-avatar")[0];
            // return source.getElementsByTagName("a")[0].innerText;
            return $(element).find('div[class*="source"],.user-avatar').find("a:first")[0].innerText;
        } catch (error) {
            console.error("get source error.", error)
        }
        return "";
    }

    /**
     *  屏蔽相关内容
     */
    function hiddenRelated() {
        let termDom = document.getElementsByClassName("opr-recommends-merge-content")
        if (termDom && termDom.length != 0) termDom[0].remove()
    }

    /**
     *  屏蔽内容
     */
    function hiddenContent() {
        document.body.addEventListener("DOMNodeInserted", (e) => {
            if (timer) {
                window.clearTimeout(timer)
            } else {
                hiddenRelated();
            }
            timer = window.setTimeout('exec()', 300)
        });
    }

    $(function () {
        hiddenContent();
    });
})();

启用脚本之后就舒服了!

赞(0)
未经允许不得转载:拆东墙 » 搜索结果过滤,通过关键字屏蔽百度搜索中的垃圾结果【油猴脚本】

评论 抢沙发

登录

找回密码

注册