4

I'm looking for a way how to make Chrome Content Script match URL configurable.

Usually the URL for the script is specified in manifest.json within matches array. I believe I can set it to http://*/ or https://*/ and check/filter the execution at the beginning of the script for the required URL, but I was wondering if there is a cleaner way how to do this.

Is there a way how to run the script for the configured URL, without being run at all if the URL doesn't match?

Thanks

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
grizzly
  • 1,146
  • 12
  • 26
  • if you want it to be for example.com you could use `*://*.example.com/*` – dmlittle Jul 09 '15 at 15:29
  • That's a good suggestion, however, valid only for subdomains. I am looking for something more generic that is configurable by the user. – grizzly Jul 09 '15 at 15:31
  • If it's user configurable then you're going to have to allow everything since you don't know what the user wants. I don't think you can change the value without physically changing the manifest.json (which isn't what you would want from a user) – dmlittle Jul 09 '15 at 15:33

2 Answers2

5

this is possible. take a look at optional permissions. its configuration is explained in the official docs.

in short, you ask for all urls in optional permissions, and then the user can add more domains and chrome remembers each aproval.

when the user performs a user action (like pressing an OK button after typing the new domain) make a chrome api call to request the permission. if it already has it, permission isnt asked again. Else chrome will display a modal chrome dialog asking the user to approve the new permission.

I was going to continue explaining options but I found this other s.o. answer which explains well the options you have. https://stackoverflow.com/a/26677279/2213940

Community
  • 1
  • 1
Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • I am studying it but this allows me to add more permissions, not content scripts or its matches. Content Script or its match is not a permission type. Can you be more concrete? – grizzly Jul 09 '15 at 16:22
  • It looks like `chrome.declarativeContent.RequestContentScript` in the linked answer is what I was looking for, I will check it. – grizzly Jul 10 '15 at 08:05
2

Thanks for the suggestions, but I ended up using Programmatic Injection of the scripts (more here: Chrome Developer: Programmatic Injection.

Following the documentation, I added two permissions into manifest.json: tabs and a generic http://*/ to access any domain the user might want.

Then created a background script that checks for the URL of the tab if it changes, and if it matches the setting stored in chrome.settings set by the user, it injects the requested script.

That way only the check is being done without the script actually loading into memory.

Here is a part of manifest.json:

"permissions": [
    "tabs",
    "http://*/", "https://*/"
],
"background": {
    "scripts": [ "background.js" ],
    "persistent": false
},

Script injection:

// inject the script
chrome.tabs.executeScript(tabId, {
    file: 'script.js',
    runAt: 'document_end'
});

EDIT

Check for the requested URL:

// var url = ''; // get URL from chrome.settings

// attach a listener
chrome.tabs.onUpdated.addListener(tabUpdated);

// check that the url starts with the saved one
var tabUpdated = function (tabId, changeInfo, tab) {
    if (tab.url.indexOf(url) === 0) {
        // run the script
    }
};
grizzly
  • 1,146
  • 12
  • 26
  • Downvoting for omitting critical parts of the script. **Your approach is valid**, but _"check for the URL of the tab if it changes"_ part is surprisingly difficult (many events need to be checked) and you omit this completely. – Xan Jul 09 '15 at 18:59
  • I edited the answer with the check that suits my requirements, and answers my question. Actually, I don't think that a proper check is difficult and listing all of the supported `chrome.tabs` events here on SO is important for solving this question, when all of them sharing observer pattern. – grizzly Jul 10 '15 at 08:00
  • 1
    you dont need to have such unlimited permissions. use optional permissions to build your permissions at runtime. – Zig Mandel Jul 10 '15 at 12:09