remove_baidu_ad

去除百度搜索结果中的广告

As of 2020-02-04. See the latest version.

// ==UserScript==
// @name         remove_baidu_ad
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  去除百度搜索结果中的广告
// @author       zimingw
// @match        https://www.baidu.com/s?*
// @match        https://zhidao.baidu.com/search?*
// @grant        none
// ==/UserScript==
var adCount = 0;
(function () {
    'use strict';
    function display() {
        $('#content_right').html('已过滤 ' + adCount + ' 条广告。')
        console.log('删除了 ' + adCount + ' 条广告')
    }
    //获取广告元素
    function getAdElements() {
        let ads = []
        //左侧
        $('#content_left>div').each((i, e) => {
            if (!$(e).hasClass('c-container')) {
                ads.push($(e))
            }
        })
        //右侧全部视为广告
        ads.push($('#content_right').children())
        //shadow
        ads.push($('::shadow div'))
        return ads
    }
    //清除广告
    function clearAd() {
        let ads = getAdElements()
        for (let item of ads) {
            item.remove()
            adCount++
        }
    }
    if (window.location.host === 'zhidao.baidu.com') {
        if (document.getElementsByClassName('bannerdown').length > 0)
            document.getElementsByClassName('bannerdown')[0].remove()
        document.getElementsByTagName('aside')[0].remove()
    } else {
        clearAd()
        display()
        //动态监听是否有新元素插入,插入的全是广告
        $('#content_left').on('DOMNodeInserted', function (e) {
            $(e.target).remove()
            adCount++
            display()
        })
        //对用户在页面内进行二次搜索产生的广告进行拦截
        $.ajaxSetup({
            complete: function (XMLHttpRequest) {
                adCount = 0
                clearAd()
                display()
            }
        });
    }
})();
长期地址
遇到问题?请前往 GitHub 提 Issues,或加Q群1031348184

赞助商

Fishcpy

广告

Rainyun

注册一下就行

Rainyun

一年攒够 12 元