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
}
};