Greasy Fork is available in English.

百度搜索优化sp

Tampermonkey脚本,屏蔽百度推广和各种广告+页面样式美化+url重定向

Από την 11/10/2018. Δείτε την τελευταία έκδοση.

// ==UserScript==
// @name         百度搜索优化sp
// @namespace    bdso_sp
// @description  Tampermonkey脚本,屏蔽百度推广和各种广告+页面样式美化+url重定向
// @update-url   https://qq2020.coding.me/lib/bdso.js
// @author       vizo
// @version      v2.2
// @require      https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js
// @include      *://www.baidu.com/*
// @resource     bdsoCss https://qq2020.coding.me/lib/css/bdso.css?v=1100
// @run-at       document-start
// @grant        GM_addStyle
// @grant        GM_getResourceText
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_xmlhttpRequest
// @connect      *
// ==/UserScript==
'use strict'

//导入CSS 
function importCSS() {
    const css = GM_getResourceText('bdsoCss')
    GM_addStyle(css)
}
importCSS()

$(function () {
    init()

    try {
        let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
        let observer = new MutationObserver(mutationfunc)
        let wrapper = document.querySelector("#wrapper")
        //动态监视DOM树的变化
        observer.observe(wrapper, {
            "attributes": true
        })
    } catch (e) {}

    // 动态加载函数
    function mutationfunc() {
        init()
    }

    function init() {
        importCSS()
        removeADS()
        cdxUrl()
        appendElem()
        setPNStatus()
    }

    // 设置a标签真实地址
    function cdxUrl() {
        $('#content_left .c-container .t > a').each(function () {
            let that = $(this)
            let url = that.attr('href')
            let cdx = that.hasClass('cdx_ed')
            if (!cdx) {
                GM_xmlhttpRequest({
                    url: url,
                    method: 'head',
                    onload: function (xhr) {
                        try {
                            that.attr('href', xhr.finalUrl).addClass('cdx_ed')
                        } catch (e) {}
                    },
                })
            }
        })
    }

    // 上一页下一页按钮
    function appendElem() {
        let len = $('body').find('.bdpage-l').length
        if (!len) {
            $('body').append('<div class="bdpage-l"></div><div class="bdpage-r"></div>')
        }
    }
    
    // 设置上下页状态
    function setPNStatus() {
        let fkl = true
        let fkr = true
        $('#page .n').each(function () {
            let that = $(this)
            let text = that.text()
            if (~text.indexOf('上一页')) {
                fkl = false
            }
            if (~text.indexOf('下一页')) {
                fkr = false
            }
        })
        if (fkl) {
            $('body').find('.bdpage-l').addClass('disa')
        } else {
            $('body').find('.bdpage-l').removeClass('disa')
        }
        if (fkr) {
            $('body').find('.bdpage-r').addClass('disa')
        } else {
            $('body').find('.bdpage-r').removeClass('disa')
        }
    }

    //屏蔽广告和推广
    function removeADS() {
        let $ads = [
            '#content_left>div[style*="display:block !important"]',
            '#content_left>div:not([id])',
            '#content_left>#clone',
        ]
        let $selctor = $($ads.join())
        $selctor.remove()
    }

    // 空格键按下快速搜索
    $(document).on('keydown', function (e) {
        let isFocus = $('#kw').is(':focus')
        if (!isFocus && e.which === 32) {
            $('#kw').focus().select()
            return false
        }
    })
    $('body').on('click', '.bdpage-l', function () {
        $('#page .n').each(function () {
            let that = $(this)
            let text = that.text()
            if (~text.indexOf('上一页')) {
                that[0].click()
            }
        })
    })
    $('body').on('click', '.bdpage-r', function () {
        $('#page .n').each(function () {
            let that = $(this)
            let text = that.text()
            if (~text.indexOf('下一页')) {
                that[0].click()
            }
        })
    })
})