在使用百度搜索的过程中,右侧栏会出现一些不想要的结果:
我想把这些无用的东西都屏蔽掉,让页面清爽一些。
于是我编写了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);
}
但这种方法不够自动化。
所以我借助了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);
}
})();
不会新建脚本的同学,可以直接访问:
来安装
建立完成后,刷新一下百度的搜索结果,会发现,右侧的内容被屏蔽掉了: