J.P. Armstrong
J.P. Armstrong

Reputation: 836

manifest.json, content_scripts limit matching to url with port number

I'm writing a Google chrome extension that should only run when the URL matches http://*:8000/*

Part of my manifest.json file:

"content_scripts": [{
    "matches": ["http://*:8000/*"],
    "js" : ["client.js"],
    "run_at": "document_end"
}] 

I get the following error when trying to pack the extension.

Invalid value for 'content_scripts[0].matches[0]': Invalid host wildcard.

If I change matches to <all_url> it packs properly and works but on every page.

How can I get it to work only with a URL that contains port 8000?

Upvotes: 2

Views: 3132

Answers (1)

zertosh
zertosh

Reputation: 5381

You're going to have to programmatically check the port number and stop the evaluation of the script if it's not the port you want. You can check window.location.port (fyi, which is an empty string for port 80 when on http) or window.location.host which includes the hostname and port (but not window.location.hostname- it doesn't include the port). So, I'd start my script with something like this:

(window.location.port === "8000") && (function() {
    // your code...
})();

Upvotes: 3

Related Questions