在编写manifest.json文件时,必须matches为内容脚本指定.在http和https做工精细,但如果我尝试包括chrome://*/*或它的任何变种,我得到我试图使用无效的方案,为我的错误matches.
不允许吗?
感谢@kofifus提供的信息,Chrome从61开始明确禁止其默认新标签页上的内容脚本
假设我有以下示例扩展名,它将test在控制台中输出.
的manifest.json
{
"name": "Test",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
content.js
console.log('test');
Run Code Online (Sandbox Code Playgroud)
上面的扩展会在chrome://newtab页面中运行良好吗?
一些有用的信息:
我知道默认情况下chrome扩展程序无法访问chrome://页面,我们可以通过更改此行为chrome://flags/#extensions-on-chrome-urls
chrome://newtab实际上是一个网址https://www.google.co.jp/_/chrome/newtab?espv=2&ie=UTF-8,所以不应该受到上述限制.
有许多鼠标手势扩展可用,如crxMouse,它们在chrome://newtab页面上运行良好
还有一些声音说它不允许注入内容脚本chrome://newtab,例如,@ Xan在这个答案下面的评论
和这位作者的情况

因此它在不同设备(或设置?)中的不同行为看起来很奇怪.是否有关于内容脚本是否可以在chrome://newtab页面中运行的官方声明?或者是否有一个设置我们可以改变这种行为?
我目前正在构建一个简单的谷歌浏览器扩展来恶作剧我的兄弟.扩展只是删除旧页面并将其替换为从"你没有互联网"页面复制的HTML和CSS,实质上是让用户认为他们没有互联网.以下是注入所有新页面的内容脚本:
var hed = document.head;
var bod = document.body;
document.head.parentElement.removeChild(document.head);
document.body.parentElement.removeChild(document.body);
document.write(/*Copy of "no internet" page source here*/);
Run Code Online (Sandbox Code Playgroud)
这个脚本完美无缺.我不希望发生更改的唯一页面是Google Chrome新标签页.通常排除页面的方法是在内容脚本下的Extensions manifest.json文件中指定它.这是我原来的manifest.json设置:
{
"manifest_version": 2,
"name": "No Internet Extention",
"description": "Make people with this extention think they have no internet",
"version": "1.0",
"content_scripts":
[
{
"matches": ["*://*/*"],
"js": ["bup.js"]
}
],
"permissions":
[
"activeTab",
"https://ajax.googleapis.com/"
]
}
Run Code Online (Sandbox Code Playgroud)
我目前在manifest.json文件content_scripts部分尝试了以下设置,以便排除Chrome新标签页无效:
"matches": ["http://*/*", "https://*/*"]
Run Code Online (Sandbox Code Playgroud)
我认为这会排除它,因为谷歌产品表格上的这个页面告诉我新标签页面的URL是"chrome:// newtab",它不是HTTP:或HTTPS:协议.
我也试过:
"matches": ["*//*/*"]
"exclude_matches": ["chrome://newtab"]
"matches": ["*//*/*"]
"exclude_matches": ["chrome://newtab/*"]
"matches": ["*//*/*"]
"exclude_matches": ["*://newtab/*"]
"matches": ["*//*/*"]
"exclude_globs": …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个 Chrome 扩展程序,该扩展程序在用户打开新标签时运行脚本。
我有以下基本代码,只要单击扩展按钮,它就会将页面涂成红色。
当我导航到实际网站时,它似乎工作正常(例如:在 stackoverflow.com 上,单击我的扩展程序图标,页面变为红色)。但是,如果我只是创建一个全新的选项卡并单击按钮,则会加载弹出窗口但页面永远不会改变颜色。
清单.json:
{
"manifest_version": 2,
"name": "ConsoleTap",
"version": "0.1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "menu.html"
},
"permissions": [
"tabs",
"<all_urls>",
"https://ajax.googleapis.com/"
]
}
Run Code Online (Sandbox Code Playgroud)
菜单.html:
<!doctype html>
<html>
<head>
<script src="menu.js"></script>
</head>
<body>
hello
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
菜单.js:
document.addEventListener('DOMContentLoaded', function() {
chrome.tabs.executeScript(null,{code:"document.body.style.backgroundColor='red'"});
});
Run Code Online (Sandbox Code Playgroud)
关于为什么页面不会更新新选项卡上的背景颜色的任何想法?我猜DOMContentLoaded它永远不会被解雇,或者它在以某种方式发生负载后正在监听?