简体   繁体   English

Chrome 扩展清单“匹配”

[英]Chrome Extension Manifest 'Matches'

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 .我正在尝试一个简单的 Chrome 扩展程序,但在为我的content_scripts中的matches数组提供值时遇到了问题。

{
  "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'.无法从“C:\Users\foo\Desktop\Extensions\bar”加载扩展。
Invalid value for 'content_scripts'. “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.我想要做的是匹配每个 URL,因此我的扩展程序可以操纵它运行的任何页面的 DOM(通过 scripts.js 中的scripts.js )。 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:发布这个问题后,我确实注意到 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'.无法从“C:\Users\foo\Desktop\Extensions\bar”加载扩展。
Invalid value for 'content_scripts[0].matches[0]'. “content_scripts[0].matches[0]”的值无效。

You need to surround the value of the content_scripts field in square brackets:您需要将content_scripts字段的值括在方括号中:

"content_scripts": [ {
  "matches": ["http://*"],
  "js": ["scripts.js"]
} ]

(see the Chrome Docs for 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.顺便说一句,使用http://*/*将更好地匹配所有网址(请参阅文档),如果您还需要匹配这些网址,请添加https://*/*

Edit:编辑:

Following your edit, the error you are getting is because of the match pattern being incorrect.在您编辑之后,您得到的错误是因为匹配模式不正确。

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

Any match pattern should be of the following structure [scheme] :// [host][path]任何匹配模式都应具有以下结构 [scheme] :// [host][path]

  1. scheme is '*' |方案是'*' | 'http' | 'http' | 'https' | 'https' | 'file' | '文件' | 'ftp' 'ftp'
  2. host is ' ' |主机是 ' ' | ' .' ' .' (any char except '/' and '*')+ (除“/”和“*”之外的任何字符)+
  3. path is '/' (any chars)路径是“/”(任何字符)

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.或者在扩展设置页面上批准文件访问。

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.尝试使用您需要的特定 URL 填充或创建匹配字段。 If you want to use all URLs, then use the <all_urls> tag like below.如果要使用所有 URL,请使用如下所示的<all_urls>标记。

"content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": [ "jquery.js" ]
        }
    ]

Files listed in the "js" array have their path relative to you app. “js”数组中列出的文件具有相对于您的应用程序的路径。 In other words, the location of "manifest.json" is your root directory.换句话说,“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 是我项目目录中的一个文件,你应该用你想要的任何脚本文件替换它。

After many tries following working , it matches all URL's and also supports http & https.enter code here经过多次尝试后,它匹配所有 URL 并且还支持 http 和 https.enter 代码here

 "manifest_version": 2,
 "content_scripts": [
    {
     "matches": [
        "*://*/*",
      ]
   }

If you are using Jquery on a CDN, Just download it and include it to your working folder, And then try to import it如果您在 CDN 上使用 Jquery,只需下载它并将其包含到您的工作文件夹中,然后尝试导入它

"manifest_version":2 “manifest_version”:2

Yes this is mandatory one.. 是的,这是必须的.​​.

Bro you forgot to add兄弟你忘记添加了

"manifest_version":2 “清单版本”:2

Which is mandatory one.这是强制性的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM