json Chrome 扩展清单“匹配”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2769525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome Extension Manifest 'Matches'
提问by Aristotle
I'm trying my hands at a simple Chrome Extension, but am running into a problem with providing a value for the matchesarray in my content_scripts.
我想我的手在一个简单的Chrome扩展,但我遇到了一个问题,为提供一个值matches在我的数组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:
当我尝试将此扩展程序加载到 Chrome 时,我收到以下消息:
Could not load extension from 'C:\Users\foo\Desktop\Extensions\bar'.
Invalid value for 'content_scripts'.
无法从“C:\Users\foo\Desktop\Extensions\bar”加载扩展。
“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?
不过,我看不出我的价值有什么“无效”。我想要做的是匹配每个 URL,所以我的扩展程序可以操作scripts.js它运行的任何页面的 DOM(通过 javascript )。我错过了什么,这一切都错了,还是什么?
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:
发布此问题后,我确实注意到 Google 示例与我的略有不同,因此我稍微修改了我的代码以反映它们的语法:
"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]'.
无法从“C:\Users\foo\Desktop\Extensions\bar”加载扩展。
“content_scripts[0].matches[0]”的值无效。
回答by adrianbanks
You need to surround the value of the content_scriptsfield in square brackets:
您需要将content_scripts字段的值括在方括号中:
"content_scripts": [ {
"matches": ["http://*"],
"js": ["scripts.js"]
} ]
(see the Chrome Docsfor more info)
(有关详细信息,请参阅Chrome 文档)
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.
顺便说一句, usinghttp://*/*会更好地匹配所有 url(请参阅docs),https://*/*如果您还需要匹配这些,请添加。
Edit:
编辑:
Following your edit, the error you are getting is because of the match pattern being incorrect.
在您编辑之后,您得到的错误是因为匹配模式不正确。
回答by thdoan
If you want to match every URL, then Google has a special pattern just for this purpose: <all_urls>
如果你想匹配每个 URL,那么 Google 有一个特殊的模式就是为了这个目的: <all_urls>
Sample usage:
示例用法:
"matches": ["<all_urls>"],
See this page for more info: https://developer.chrome.com/extensions/match_patterns
有关更多信息,请参阅此页面:https: //developer.chrome.com/extensions/match_patterns
回答by David D.
Any match pattern should be of the following structure [scheme]://[host][path]
任何匹配模式都应具有以下结构 [scheme] ://[host][path]
- schemeis '*' | 'http' | 'https' | 'file' | 'ftp'
- hostis '' | '.' (any char except '/' and '*')+
- pathis '/' (any chars)
- 方案是'*' | 'http' | 'https' | '文件' | 'ftp'
- 主机是 ' ' | '.' (除“/”和“*”之外的任何字符)+
- 路径是“/”(任何字符)
For matching any HTTP/S and FILE URL use:
要匹配任何 HTTP/S 和 FILE URL,请使用:
"matches": [
"*://*/*",
"file://*/*"
],
Ref: https://developer.chrome.com/apps/match_patterns
参考:https: //developer.chrome.com/apps/match_patterns
By the way, in order to allow access to local files - add the permission:
顺便说一句,为了允许访问本地文件 - 添加权限:
"permissions": [
"file://*/*"
]
Or approve file access on the extension settings page.
或者在扩展设置页面上批准文件访问。
回答by Anselm
For many that are getting errors involving:
对于许多遇到错误的人来说:
'content_scripts[0].matches' is missing or invalid.
or
或者
'content_scripts[0].matches[0]': Empty path.
Trying filling in, or creating, the matches field with your specific URL needed.
If you want to use all URLs, then use the <all_urls>tag like below.
尝试使用您需要的特定 URL填充或创建匹配字段。如果要使用所有 URL,请使用<all_urls>如下所示的标记。
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [ "jquery.js" ]
}
]
Files listed in the "js" array have their path relative to you app. In other words, the location of "manifest.json" is your root directory.
“js”数组中列出的文件具有相对于您的应用程序的路径。换句话说,“manifest.json”的位置就是你的根目录。
Note: jquery.js is a file in my project's directory and you should replace it with whatever script file you want.
注意:jquery.js 是我项目目录中的一个文件,你应该用你想要的任何脚本文件替换它。
回答by Ayush kumar
Bro you forgot to add
兄弟你忘记添加了
"manifest_version":2
“清单版本”:2
Which is mandatory one.
这是强制性的。

