我刚刚启动了Chrome扩展程序。我有这个简单的文件结构
清单文件如下所示
{ "manifest_version": 2, "name": "fu", "description": "block audio from videos set to autoplay", "version": "1", "author": "ddpf", "browser_action": { "default_title": "Have a better day" }, "chrome_url_overrides" : { "newtab": "newtab.html"}, "permissions": ["activeTab"]}
到目前为止,newtab是非常基本的,只是您好世界,并链接到js文件
<!doctype html>
<html>
<head>
<title>Test</title>
</head> <body>
<h1>Hello World!</h1>
<video>
<iframe width="420" height="345" src="https://www.youtube.com/embed/tgbNymZ7vqY">
</iframe>
</video>
<script src="popup.js"></script>
</body></html>
还有相当基本的JS代码
var videos = document.querySelectorAll('video'); /*Loop*/
if (videos.length <= 1) {
console.log("there have been no video elements to mute or remove")
}
else {
var i;
for (i = 0; i < videos.length; i++) {
videos[i].style.display = 'none';
console.log("there have been " + i +" video elements")
}
}
var totalNoOfResrouces = window.performance.getEntriesByType("resource").length;
console.log("totalNoOfResrouces ")
if (totalNoOfResrouces.length <= 10) {
console.log("well done")
}
其他{
console.log("Too many telemetry and tracking and 3rd party, we will redirect this")
}
如果我在新标签页中运行此命令,则会呈现“ hello world”,视频和iframe位于DOM中(根据需要设置为display:none),然后在控制台中获取它。
there have been 0 video elements (this should show 1 instead of 0, why 0?)
popup.js:19 0 (this seems ok but js line 26 should return the if clause condition on line 22, not the line 26 else clause )
popup.js:26 Too many telemetry and tracking and 3rd party scripts, we will redirect this /// This should show well done,
但是,真正的问题是,如果更改URL或转到任何选项卡并更改URL,JS或扩展名都不会在新选项卡页面上运行。
为了进行测试,我去了https://www.dailymail.co.uk/news/article-9106063/Biggest-teaching-union-calls-emergency-meeting.html,该邮件中包含大量的自动播放视频,看得出来,它们都自动播放,控制台中什么也没有。
在上面的链接中,当向下滚动时,它们会弹出一个视频元素#vjs_video_2122_html5_api和另一个具有html属性data-mol-fe-video-preview的视频元素。两者都运行时,好像他们为视频元素上的广告调用了许多外部服务,控制台中出现了许多错误,但我的扩展程序中没有任何错误,而vid仍然运行。
我没有为Google支付5美元开发费,如果现在仅在我的本地计算机上运行可以,这是可以的,这与可发布和业余爱好项目相去甚远。但是,为什么只在全新的标签中运行呢?
我是否必须对manifest.json文件中的所有选项卡授予权限?我该怎么做?谢谢
您想将popup.js
其注入到您访问的每个页面中吗?在这种情况下,只需将以下代码粘贴到您的manifest.js
文件中。如果要查看console.log条目,只需打开任何选项卡页,打开devtools,console.log条目就会出现。
内容脚本被注入到与matchs数组中的模式匹配的每个页面中。在以下代码的情况下,它将是“所有网址”。
另一件事,如果您想授予扩展程序访问所有选项卡的权限,只需删除“ activeTab”并将其替换为 "<all_urls>"
{
"manifest_version": 2,
"name": "fu",
"description": "block audio from videos set to autoplay",
"version": "1",
"author": "ddpf",
"browser_action": {
"default_title": "Have a better day"
},
"chrome_url_overrides": {
"newtab": "newtab.html"
},
"content_scripts": [{
"js": ["popup.js"],
"matches": [
"<all_urls>"
]
}],
"permissions": ["activeTab"]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句