Chrome Extension Manifest 'Matches'

前端 未结 5 1957
甜味超标
甜味超标 2020-12-12 23:54

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

相关标签:
5条回答
  • 2020-12-13 00:08

    If you want to match every URL, then Google has a special pattern just for this purpose: <all_urls>

    Sample usage:

    "matches": ["<all_urls>"],
    

    See this page for more info: https://developer.chrome.com/extensions/match_patterns

    0 讨论(0)
  • 2020-12-13 00:12

    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.

    0 讨论(0)
  • 2020-12-13 00:20

    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.

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

    Note: jquery.js is a file in my project's directory and you should replace it with whatever script file you want.

    0 讨论(0)
  • 2020-12-13 00:26

    Any match pattern should be of the following structure [scheme]://[host][path]

    1. scheme is '*' | 'http' | 'https' | 'file' | 'ftp'
    2. host is '' | '.' (any char except '/' and '*')+
    3. path is '/' (any chars)

    For matching any HTTP/S and FILE URL use:

     "matches": [
        "*://*/*",
        "file://*/*"
      ],
    

    Ref: 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.

    0 讨论(0)
  • 2020-12-13 00:31

    Bro you forgot to add

    "manifest_version":2

    Which is mandatory one.

    0 讨论(0)
提交回复
热议问题