简单几行代码,写一个百度广告屏蔽插件,爽到爆

news/2024/7/1 4:01:21

下载地址:https://github.com/jsoncode/b...
使用方法:

clipboard.png

将文件夹拖到这里面
clipboard.png

一个小问题:
我没有境外支付信用卡>无法向谷歌支付$5的费用>每次打开浏览器都会提示是否禁止>点取消就行了

也可以通过网上的方法,修改注册表关闭提示,这里就不再赘述
如果你有更新,需要打开第二张图的位置,重新载入才能生效


目录结构


-bd.png
-browser_action.js
-config.js
-manifest.json

manifest.json

{"name": "屏蔽百度广告","manifest_version": 2,"version": "1.0","description": "百度插件","browser_action": {"default_icon": "bd.png"},"icons": {"128":"bd.png"},"content_scripts": [{"matches": ["<all_urls>"],"js": ["config.js","browser_action.js"]}]
}

browser_action.js

var lists = str.split('\n').filter(function(v) {if (v) return true
});function clear(lists) {var local = location.href;for (var a = 0, list; list = lists[a++];) {var sub = list.split('$');if (new RegExp(sub[0]).test(local)) {var selector = sub[1];var obj = document.querySelectorAll(selector);for (var i = 0, item; item = obj[i++];) {item.parentNode.removeChild(item);}}}
}clear(lists);
setInterval(function() {clear(lists);
}, 2000);
var input = document.querySelector("input[name=wd]");
input&&input.addEventListener('change', function() {clear(lists);
});

之所以用定时器一直调用,是因为有些网站会动态延迟的加载动态脚本类的广告,比如bd,恶心无比,自己使用的adBlock,adBlock plus都不理想。

config.js

var str = `
www.baidu.com$#content_left [style="display:block !important;visibility:visible !important"]
www.baidu.com$[tpl="right_toplist"]
www.baidu.com$[class="c-gray c-feedback"]
www.baidu.com$[data-click*="vLevel"]
`;

用法:每次只需要修改config.js中的规则就行
规则约定:需要屏蔽的网站地址$css3选择器,中间的$是分隔符,选择器选中的就是要被删除的广告

上面四个规则自己常用的,
第一个可以屏蔽大多数动态流氓广告,之所以用这么低级的方法(style=*)是因为这类广告伪装实在太逼真(和正常搜索结果没任何区别)
第二个屏蔽右侧不喜欢的推荐
第三行忘记了,
第四行是一些推广广告

从此,世界一片洁净,我不会成为第二个魏则西,在此分享给大家,


这里更新一下,每次打开浏览器,都会提示的问题。

我找到了一个管理浏览器插件代码片段的工具Tampermonkey,是一个浏览器插件。
然后就可以把我的百度插件代码添加到这个插件中使用,就不会每次都提示了。

使用方法:
1,新增脚本
图片描述

2,粘贴代码
图片描述

代码如下:

// ==UserScript==
// @name         ad-baidu
// @namespace    https://baidu.com/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.baidu.com/**
// @grant        none
// ==/UserScript==
var selectors = `
#content_left [style*="display:block !important;visibility:visible !important"]
[tpl="right_toplist"]
[class="c-gray c-feedback"]
[data-click*="vLevel"]
`;
selectors = selectors.trim().split('\n');
var count = 0;
function showCount(cn){if(cn>0){count+=cn;var content_right = document.querySelector('#content_right');if(content_right){content_right.style.position = 'relative';var style = `
line-height: 42px;
text-align: center;
background: #0c0;
color: #fff;
margin-bottom:30px;
`;var msgCount = document.querySelector('#msg-remove');if(!msgCount){content_right.insertAdjacentHTML('afterbegin','<div id="msg-remove" style="'+style+'"></div>');msgCount = document.querySelector('#msg-remove');}msgCount.innerHTML = '已过滤:'+count+'条垃圾广告,本次过滤:'+cn+'条';}}
}
function clear() {selectors.forEach(function(selector) {var doms = document.querySelectorAll(selector);if(doms.length){doms = Array.from(doms);showCount(doms.length);doms.forEach(function(dom) {dom.parentNode.removeChild(dom);});}});
}
setInterval(function () {clear();
},500);

然后每次打开浏览器百度搜索,就不会再有提示了;
效果:
图片描述


http://lihuaxi.xjx100.cn/news/244335.html

相关文章

SpringBoot解决跨域问题

个人资源与分享网站&#xff1a;http://xiaocaoshare.com/ 1. 什么是跨域 要了解跨域&#xff0c;先要知道同源策略。同源策略是由 Netscape 公司提出的一个著名的安全策略&#xff0c;所有支持 JavaScript 的浏览器都会使用这个策略。所谓同源是指&#xff1a;协议&#xff…

jQuery中的.height()、.innerHeight()和.outerHeight()

jQuery中的.height()、.innerHeight()和.outerHeight()和W3C的盒模型相关的几个获取元素尺寸的方法。对应的宽度获取方法分别为.width()、.innerWidth()和.outerWidth()&#xff0c;在此不详述。1. .height()获取匹配元素集合中的第一个元素的当前计算高度值 或 设置每一个匹配…

CentOS7安装Python3

2019独角兽企业重金招聘Python工程师标准>>> CentOS7默认安装了python2.7.5&#xff0c;当需要使用python3的时候&#xff0c;可以手动下载Python源码后编译安装。 下载python(https://www.python.org 上面下载对应版本)安装一个软件 yum install -y readline-deve…

eclipse集成lombok

个人资源免费分享网站&#xff1a;http://xiaocaoshare.com/ 进入lombok.jar所在目录&#xff0c;执行java -jar lombok.jar命令选择要集成的ide 点击Install / Update 按钮

程序员说话技巧大放送,受教了

转&#xff1a;https://blog.csdn.net/u012149969/article/details/88605298 程序员除了给人一种发际线偏高的感觉外&#xff0c;很闷很宅很难沟通应该也是丢给大家的印象之一&#xff01;但往往有效的沟通又是程序员所必备的一项技能&#xff0c;因为掌握说话的方式技巧&…

控制语句 for while if switch

一、for…in 结构 for i in 0...4{ print(i) //使用到了变量 i } for _ in 0...1{ // 后期没有使用到变量,可以直接用个下划线 _ 占位就行了 print("hello") } 二、while 结构 var i : Int 0 while i < 5 { print("a") i 1 } //repeat…whil…

VS2010中“工具选项中的VC++目录编辑功能已被否决”解决方法

http://blog.csdn.net/chaijunkun/article/details/6658923 这是VS2010的改变&#xff0c;不能够在“工具-选项”中看到“VC目录”了。 但是呢&#xff0c;我们可以在另外一个地方找到它&#xff0c;请看下边的对比照片。 VS2008中&#xff1a; VS2010中&#xff1a; 打开方式非…

Map json数据解析

个人学习资源免费分享网站&#xff1a;http://xiaocaoshare.com/ public class App { public static void main(String[] args) { Map<String, List<Student>> map new HashMap<String, List<Student>>(); List<Student> …