Chrome扩展程序清单“匹配” [英] Chrome Extension Manifest 'Matches'
问题描述
我正在尝试使用简单的Chrome扩展程序,但遇到了在中为
。匹配
数组提供值的问题。 content_scripts
{
name:My Extension,
version:1.0,
description:我的扩展实验,
browser_action:{
default_icon:icon.png,
default_title:Ext,
default_popup:popup.html
},
content_scripts:{
matches:[http:// *],
js:[scripts.js]
}
}
当我尝试将此扩展程序加载到Chrome中时,出现以下消息:
$ b
无法加载扩展程序from'C:\Users\foo\Desktop\Extensions\bar'。
无效的'content_scripts'值。
虽然我看不到什么是无效。我想要做的就是匹配每个URL,所以我的扩展可以操纵任何运行页面的DOM(通过 scripts.js
中的javascript)。我是否错过了一些东西,这些都是错误的,或者是什么?
更新
<在发布这个问题后,我注意到Google的例子与我的例子稍有不同,所以我修改了一下代码以反映它们的语法:
content_scripts:[{
matches:[http:// *],
js:[scripts.js]
} ]
这就是说,我在尝试加载我的扩展时仍然遇到以下错误:
无法从'C:\Users\foo\Desktop\Extensions\bar'加载扩展名。
'content_scripts [0] .matches [0]'的值无效。
您需要围绕方括号中 content_scripts
字段的值:
content_scripts:[{
matches:[http:// *],
js:[scripts.js]
}]
(请参阅 Chrome文档了解更多信息)
顺便提一句,使用 http:// * / *
将会更好地匹配所有网址(请参阅文档),并添加 https:// * / *
,如果您还需要匹配这些内容。
在编辑之后,您收到的错误是由于匹配模式不正确。
I'm trying my hands at a simple Chrome Extension, but am running into a problem with providing a value for the matches
array in my content_scripts
.
{
"name": "My Extension",
"version": "1.0",
"description": "My Extension Experiment",
"browser_action": {
"default_icon": "icon.png",
"default_title": "Ext",
"default_popup": "popup.html"
},
"content_scripts": {
"matches": ["http://*"],
"js": ["scripts.js"]
}
}
When I try to load this extension into Chrome, I get the following message:
Could not load extension from 'C:\Users\foo\Desktop\Extensions\bar'.
Invalid value for 'content_scripts'.
I cannot see what is "invalid" about my value though. What I'm trying to do is match every URL, so my extension can manipulate the DOM (via javascript within scripts.js
) of any page it is ran on. Am I missing something, going about this all wrong, or what?
update
After posting this question, I did notice that the Google example was slightly different than mine, so I modified my code a bit to reflect their syntax:
"content_scripts": [{
"matches": ["http://*"],
"js": ["scripts.js"]
}]
That being said, I still get the following error when trying to load my extension:
Could not load extension from 'C:\Users\foo\Desktop\Extensions\bar'.
Invalid value for 'content_scripts[0].matches[0]'.
You need to surround the value of the content_scripts
field in square brackets:
"content_scripts": [ {
"matches": ["http://*"],
"js": ["scripts.js"]
} ]
(see the Chrome Docs for more info)
Incidentally, using http://*/*
would be a better match for all urls (see the docs), adding https://*/*
if you also need to match those as well.
Edit:
Following your edit, the error you are getting is because of the match pattern being incorrect.
这篇关于Chrome扩展程序清单“匹配”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!