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

一、测试网址 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/这个网址中用,其他网址要根据其实际情况进行修改

三、去广告前

 四、去广告后

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Chrome扩展content-scripts请求跨域的问题,可以通过在manifest.json文件中添加"permissions"和"web_accessible_resources"字段来解决。具体步骤如下: 1. 在manifest.json文件中添加"permissions"字段,用于声明扩展需要访问的权限。例如: ``` { "name": "My Extension", "version": "1.0", "manifest_version": 2, "permissions": [ "http://www.example.com/*" ], "content_scripts": [ { "matches": ["http://www.example.com/*"], "js": ["content_script.js"] } ] } ``` 以上代码中,"http://www.example.com/*"表示扩展需要访问的域名,"content_scripts"表示扩展需要注入的脚本。 2. 在manifest.json文件中添加"web_accessible_resources"字段,用于声明扩展需要访问的资源。例如: ``` { "name": "My Extension", "version": "1.0", "manifest_version": 2, "permissions": [ "http://www.example.com/*" ], "content_scripts": [ { "matches": ["http://www.example.com/*"], "js": ["content_script.js"] } ], "web_accessible_resources": [ "http://www.example.com/*" ] } ``` 以上代码中,"http://www.example.com/*"表示扩展需要访问的资源。 3. 在content_script.js文件中使用XMLHttpRequest或fetch等API发送跨域请求,代码示例如下: ``` fetch('http://www.example.com/api/data') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); ``` 以上代码中,fetch方法用于发送跨域请求,并且在响应中获取数据。 需要注意的是,如果要在content-scripts中使用cookie等认证信息,需要在manifest.json文件中添加"permissions"字段,并使用XMLHttpRequest或fetch等API发送带有cookie的跨域请求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值