0

I have a manifest.json file like this:

{
  "name": "YouTube Trending to Paid promotion!",
  "manifest_version": 2,
  "version": "1.0",
  "description": "Change Trending to Paid promotion!",
  "permissions": ["tabs", "*://*.youtube.*/*"],
    "browser_action": {
    "default_icon": "icon.png"
  },
  "content_scripts": [
    {
      "matches": ["*://*.youtube.*/*"],
      "js": ["popup.js"]
    }
  ]
}

For my JS extension that just finds the 41st <span> and renames the contents. And when I inject it directly, it works fine. But when I try to upload it, Chrome gives me this error message: Invalid value for 'content_scripts[0].matches[0]': Invalid host wildcard.

My JS is following:

document.getElementsByTagName("span")[39].textContent="Paid promotion!";

Anyone got an idea whats causing the error ?

Jousi
  • 456
  • 4
  • 26

2 Answers2

2

The problem, as the error states, is that your matches value is invalid. The Match Patterns documentation explains that *, when used in the host portion, has the following requirements:

* in the host can be followed only by a . or /
If * is in the host, it must be the first character

Thus, your match pattern of:

"matches": ["*://*.youtube.*/*"],

is invalid because the second * (youtube.*) is not permitted.

Explicitly list the Top Level Domains (TLDs) you desire to match

You need to explicitly determine the TLDs you desire to match, then include a list of them in your matches array. For example:

"matches": [
    "*://*.youtube.com/*",
    "*://*.youtube.co.uk/*"
],

However, for YouTube, they appear to redirect all traffic to youtube.com. Thus, you are probably just fine using:

"matches": ["*://*.youtube.com/*"],

This matching limitation is by design. There are a large number of TLDs, and more are likely to be created in the future. There is no guarantee that a particular site which you desire to match currently has a domain in every TLD, nor that they will obtain one in every new TLD which is created. Thus, the better solution is to not permit wildcards for the TLD when a portion of the host is specified.

Alternately, use include_globs, but then have possible false matches

You can use include_globs to obtain what you desire, but doing so has security risks due to possible false matches. You would need something like:

"content_scripts": [
  {
    "matches": ["*://*/*"],
    "js": ["popup.js"],
     "include_globs": [
       "http://*.youtube.*/*",
       "https://*.youtube.*/*"
     ]
  }
]

This is still imperfect, as it will match URLs which contain .youtube.*/* anywhere in their URL (e.g. http://foo.example.com/clone/www.youtube.com/myVideo). Unfortunately, because include_globs is a glob, not a full regular expression, you can not specify that you want to match all characters but /, which you could do in a regular expression (e.g. [^/]). Overall, you are better off explicitly determining the TLDs you desire to match, then include a list of them in your matches array.

Note: In the above example, your matches pattern could be either "*://*/*", or "<all_urls>". The difference is that "<all_urls>" also matches "file:" and "ftp:" URLs, which are then excluded by the include_globs.

Makyen
  • 31,849
  • 12
  • 86
  • 121
1

Seems Content Script matches are slightly different.

See the matterns are allowed in chrome https://developer.chrome.com/extensions/match_patterns

"content_scripts": [
    {
      "matches": ["*://*.youtube.com/*"],
      "js": ["popup.js"]
    }
  ]

or use "matches": ["<all_urls>"]

Refer : Chrome Extension Manifest 'Matches' already discussed by stackoverflow.

Community
  • 1
  • 1
maha lakshmi
  • 57
  • 1
  • 2
  • 9