BrunoLM
BrunoLM

Reputation: 100322

More generic Content Scripts match pattern?

I want to match:

http://images.orkut.com/
http://www.orkut.co.uk/
http://www.orkut.com.br/
http://images.orkut.jp/

The pattern I am trying to use is:

http://*.orkut.*/*

But when I try to load the extension it says:

invalid value for content_scripts[0].matches[0]

Is there a way to match these urls without specifying the full domain?


In the manifest file

"permissions": [ "tabs", "http://*.orkut.*/*", "https://*.orkut.*/*" ],
"content_scripts":
[
    {
        "matches": [ "http://*.orkut.*/" ], // error
        "js": ["content/loader.js"]
    }
]

A more generic doesn't work, while this one works:

"matches": [ "http://*.orkut.co.uk/" ],

Upvotes: 1

Views: 3792

Answers (2)

Jossef Harush Kadouri
Jossef Harush Kadouri

Reputation: 34207

As Mohamed Mansour's answer mentioned, this is not supported by google in the manifest.json

However, you can still add a simple javascript check in your content script!

For example, this is how I workaround a similar case where I wanted the content script to be executed only on 192.168.*.* hosts (it's an in-house chrome extension)

  1. in your manifest.json use <all_urls>

    ...
      "content_scripts": [
        {
          "matches": ["<all_urls>"],
          "js": ["content-script.js"]
        }
      ],
    ...
    
  2. in your content-script.js use a simple condition to verify if the hostname matches a regex pattern

    let isValidHostname = /^192.168.\d+.\d+$/.test(window.location.hostname);
    if (isValidHostname){
        // Do your stuff here
    }
    

Upvotes: 0

Mohamed Mansour
Mohamed Mansour

Reputation: 40149

You cannot do that do to security reasons, please refer to http://code.google.com/chrome/extensions/match_patterns.html for more information.

As well, team lead of Chrome Extension did a nice explanation why: http://groups.google.com/a/chromium.org/group/chromium-extensions/msg/3d305eb340f01763

Upvotes: 2

Related Questions