remove_baijiahao

删除百度搜索中的百家号结果

目前為 2020-08-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         remove_baijiahao
// @icon         https://baijiahao.baidu.com/favicon.ico
// @namespace    http://tampermonkey.net/
// @description  删除百度搜索中的百家号结果
// @version      1.0
// @include      http://www.baidu.com/s*
// @include      https://www.baidu.com/s*
// @author       Greyh4t
// @grant        GM.xmlHttpRequest
// @run-at       document-end
// ==/UserScript==

(function () {
    'use strict';

    let blockList = ["baijiahao.baidu.com"];

    deal_content();
    bind_change();

    function bind_change() {
        let titleEl = document.getElementsByTagName("title")[0];
        let MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
        if (MutationObserver) {
            let MutationObserverConfig = {
                childList: true,
                subtree: true,
                characterData: true
            };
            let observer = new MutationObserver(function (mutations) {
                deal_content();
            });
            observer.observe(titleEl, MutationObserverConfig);
        } else if (titleEl.addEventListener) {
            titleEl.addEventListener("DOMSubtreeModified", function (evt) {
                deal_content();
            }, false);
        }
    }

    function deal_content() {
        let content = $('#content_left');
        let containers = content.find('.c-container');

        for (let i = 0; i < containers.length; i++) {
            let container = containers[i];
            let tpl = container.getAttribute('tpl');
            switch (tpl) {
                case "se_com_default":
                    removeType0(container);
                    break;
                case "timeliness_news2":
                    removeType1(container);
                    break;
                case "sp_realtime_bigpic5":
                    removeType2(container);
                    break;
                case "short_video_pc":
                    removeType3(container);
                    break;
            };
        }
    }

    // se_com_default
    function removeType0(container) {
        let top = $(container);
        let divs = top.children('div');
        handle_divs(container, top, divs);
    }

    // timeliness_news2
    function removeType1(container) {
        let top = $(container);
        let divs = top.children('div');
        handle_divs(container, top, divs);
    }

    // sp_realtime_bigpic5
    function removeType2(container) {
        let top = $(container).children('div');
        let divs = top.children('div');
        handle_divs(container, top, divs);
    }

    // short_video_pc
    function removeType3(container) {
        let top = $(container).children('div').children('div');
        let divs = top.children('div');
        handle_divs(container, top, divs);
    }

    function handle_divs(container, top, divs) {
        for (let i = 0; i < divs.length; i++) {
            let div = divs[i];
            handle_div(container, top, div);
        }
    }

    function handle_div(container, top, div) {
        let as = $(div).find('a');
        for (let i = 0; i < as.length; i++) {
            let a = as[i];
            if (is_block(a.hostname)) {
                check_remove(container, top, div);
                return;
            }

            if (a.href.indexOf("www.baidu.com/link?url=") > -1) {
                handle_fake_url(container, top, div, a);
            }
        }
    }

    function is_block(hostname) {
        for (let i = 0; i < blockList.length; i++) {
            if (hostname.endsWith(blockList[i])) {
                return true;
            }
        }
        return false;
    }

    function handle_fake_url(container, top, div, a) {
        let tmpURL = a.href.indexOf("eqid") < 0 ? a.href + "&wd=&eqid=" : a.href;
        GM.xmlHttpRequest({
            method: "GET",
            url: tmpURL,
            headers: {
                "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
                "Host": "www.baidu.com",
                "Accept-Encoding": "gzip, deflate, br",
                "Connection": "keep-alive"
            },
            onload: function (response) {
                let reg = /URL=['|"]([^'|"]+)/;

                if (reg.test(response.responseText)) {
                    let realURL = response.responseText.match(reg)[1];
                    let o = new URL(realURL);

                    if (is_block(o.hostname)) {
                        check_remove(container, top, div);
                    } else {
                        // 还原真实地址
                        $(container).find('a[href="' + a.href + '"]').attr('href', realURL);
                    }
                }
            }
        });
    }

    function check_remove(container, top, div) {
        div.remove();
        if (top.children('div').length == 0) {
            container.remove();
        }
    }
})()