8、content-scripts实现一个简单的去除广告插件

8、content-scripts实现一个简单的去除广告插件一 测试网址 https www yy6080 info manifest json manifest version 2 name NoAd version 1 0 0 descript

大家好,欢迎来到IT知识分享网。

一、测试网址 https://www.yy6080.info/

manifest.json

{ "manifest_version": 2, "name": "NoAd", "version": "1.0.0", "description": "", "icons": { "16": "icon/icon.png", "48": "icon/icon.png", "128": "icon/icon.png" }, "content_scripts": [ { "matches": ["*://www.yy6080.info/*"], "js": ["js/content-scripts.js"], "run_at": "document_end", "all_frames":true } ], "permissions":["*://www.yy6080.info/*"], "browser_action":{}, "options_ui": { "chrome_style": true } }

二、content-scripts

function getElement(element) { if (element.parentElement === document.body ) { return element } else return getElement(element.parentElement) } function filter() { for (let i of document.getElementsByTagName('a')) { if (i.href !== "javascript:;" && new RegExp(location.origin).test(i.href) === false) { let element = getElement(i); if(element.className !== "container"){ element.remove(); } } } for (let i of document.getElementsByTagName('img')) { if (new RegExp(location.origin).test(i.src) === false) { console.log(new RegExp(location.origin).test(i.src)) let element = getElement(i); if(element.className !== "container"){ element.remove(); } } } for (let i of document.getElementsByTagName('video')) { console.log(new RegExp(location.origin).test(i.src)) if (new RegExp(location.origin).test(i.src) === false) { let element = getElement(i); if(element.className !== "container"){ element.remove(); } } } for (let i of document.getElementsByTagName('iframe')) { console.log(new RegExp(location.origin).test(i.src)) if (new RegExp(location.origin).test(i.src) === false) { let element = getElement(i); if(element.className !== "container"){ element.remove(); } } } } window.onload = function () { filter(); } 

这种方式在触犯onload事件后,对含有广告的块级元素进行移除,也就是说在触发onload之前,网址上还是可以看到广告的,目前这段代码只能在https://www.yy6080.info/这个网址中用,其他网址要根据其实际情况进行修改

三、去广告前

8、content-scripts实现一个简单的去除广告插件

 四、去广告后

8、content-scripts实现一个简单的去除广告插件

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/101456.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信