For a Google Chrome extension, I would like to have optional permissions for a "content script" so that the content script is activated only if the user has granted this optional permission. The extension's manifest has a "content_scripts": [{..., "matches": "url"}]
that triggers a non-optional request for permission to read and change everything at that URL.
The documentation on option permissions does not mention content scripts. And the documentation for RequestContentScript still says it is not available in stable builds.
Is there a way to do this?
You can use Firefox' browser.contentScripts.register() API and its Chrome polyfill to register new content scripts dynamically. In Chrome 96+ there's also chrome.scripting.registerContentScript, which does something similar.
You'll also have to watch for new permissions and register the scripts on the new hosts.
The module webext-dynamic-content-scripts takes care of all of this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With