Reputation: 391
In a project to create a web extension for Safari, using scripting.registerContentScript()
API to inject a bunch of scripts into web pages, I needed to manage a dynamic whitelist (i.e., web pages where the scripts should not be injected).
Fortunately, scripting.registerContentScripts()
gives you the option of defining a list of web pages to be considered as a whitelist, using the excludeMatches
parameter in the directive, to represent an array of pages where the script should not be injected.
Here just a sample of what I mean:
const matches = ['*://*/*'];
const excludeMatches = ['*://*.example.com/*'];
const directive = {
id: 'injected-jstest',
js: ['injectedscript.js'],
matches: matches,
excludeMatches: excludeMatches,
persistAcrossSessions: false,
runAt: 'document_start'
};
await chrome.scripting.registerContentScripts([directive])
.catch(reason => { console.log("[SW] >>> inject script error:",reason); });
Of course, the whitelist is not static, but varies over time according to the needs of the moment.
I want to use scripting.registerContentScript()
API to inject scripts in browser context and not every time a page is open (page context), which would also involve adding all scripts to be injected in manifest.json
web_accessible_resources
list.
Everything works perfectly in Chromium browsers (Chrome, Edge, ...) and Firefox, but fails miserably in Safari. In fact, Safari seems to completely ignore the excludeMatches
parameter and injects the script even where it should not.
In addition, in Safari the script is injected multiple times into the same page (!!!), which also suggests an underlying problem with scripting.registerContentScript()
API, or, even worse, in the management of the extension's service worker.
Has anyone had the same problem and solved it somehow?
NOTE: To test the correctness and capabilities of the API in each browser, I created a simple repository on Github with the extension code for Chromium, Firefox and Safari (XCode project).
EDIT I created a post in Apple Developers Forum about this issue. No answer (as always).
Upvotes: 0
Views: 28