Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

json - 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.

{
  "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:UsersfooDesktopExtensionsar'.
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:UsersfooDesktopExtensionsar'.
Invalid value for 'content_scripts[0].matches[0]'.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...