我试图阻止用户在 chrome://
页面上运行我的扩展程序。当他们点击我的扩展程序时,他们会收到一个错误,该错误显示在我的扩展程序旁边,并且非常烦人:
我尝试过的
这是我的 manifest.json
。我想完全忽略 chrome://
URI。
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"exclude_globs": ["chrome://*"],
"css": ["style.css"]
}
],
"background": {
"persistent": false,
"scripts": ["background.js"],
"css": ["style.css"]
}
这是我的background.js
:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {file: "a.js"}, function(){
chrome.tabs.executeScript(null, {file: "b.js"});
});
});
我真的只想匹配所有 http
和 https
url,并忽略其他所有内容,这就是我的想法 "matches": ["http ://*/*", "https://*/*"],
在这里可以,但由于某种原因它不起作用。
我尝试过使用排除匹配,但结果是相同的。我尝试使用 "*://*/*"
作为匹配模式,但这也不起作用,即使 match patterns docs假设使用 *
作为方案应该只与 http/https
匹配。所以我没主意了。
或者,如果可以捕获此错误以防止其出现,那也对我有用。
最佳答案
我可以通过检查 background.js
文件中事件监听器内的选项卡 URL 来防止此错误:
chrome.browserAction.onClicked.addListener(function(tab) {
if(!tab.url || !tab.url.startsWith('http')){
return;
}
// do stuff
});
希望这对某人有帮助!
关于javascript - Chrome扩展,如何排除chrome ://URIs in manifest. json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60173517/