百度搜索屏蔽掉右侧的推荐

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012282037/article/details/73479898

在使用百度搜索的过程中,右侧栏会出现一些不想要的结果:


我想把这些无用的东西都屏蔽掉,让页面清爽一些。


于是我编写了javascript脚本来实现:

if (location.hostname=="www.baidu.com"){
    var auto = setInterval(function() {
        if (document.getElementById('content_right')){
            document.getElementById('content_right').style.display="none";
        }

        if(document.getElementById('rrecom-container')){
            document.getElementById('rrecom-container').style.display="none";
        }

        if(document.getElementsByClassName("opr-recommends-merge-content")[0]){

            document.getElementsByClassName("opr-recommends-merge-content")[0].style.display="none";
            clearInterval(auto);
        }
    }, 50);
}


这时,只要在console里输入这些代码,就会发现,右侧的推广内容都被隐藏掉了。、

但这种方法不够自动化。


所以我借助了chrome的插件tampermonkey(油猴,在firefox下面叫做greasymonkey)来写脚本。

tampermonkey的基本原理是在页面加载完后,使用javascript控制dom,以达到改变页面的效果。

没用过的建议先学习一下基本的使用方法


新建一个用户脚本,脚本的内容如下:

// ==UserScript==  
// @name         fuck百度搜索右侧推广  
// @namespace    http://kongpingfan.com/  
// @version      0.1.2
// @description  将百度右侧的结果屏蔽掉。  
// @author       pyufftj  
// @match        *://*.baidu.com/*  
// @grant        none  
// ==/UserScript==   
(function() {  
    'use strict';  
  
    if (location.hostname=="www.baidu.com"){  
        var auto = setInterval(function() {  
            if (document.getElementById('content_right')){  
                document.getElementById('content_right').style.display="none";  
            }  
            if(document.getElementById('rrecom-container')){  
                document.getElementById('rrecom-container').style.display="none";  
            }  
            if(document.getElementsByClassName("opr-recommends-merge-content")[0]){  
                document.getElementsByClassName("opr-recommends-merge-content")[0].style.display="none";  
              
            }  
        }, 500);  
    }  
  
})();  


不会新建脚本的同学,可以直接访问:

https://greasyfork.org/en/scripts/30777-%E7%99%BE%E5%BA%A6%E6%90%9C%E7%B4%A2%E5%8F%B3%E4%BE%A7%E6%8E%A8%E5%B9%BF

来安装


建立完成后,刷新一下百度的搜索结果,会发现,右侧的内容被屏蔽掉了:

猜你喜欢

转载自blog.csdn.net/u012282037/article/details/73479898