Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional permissions for "content scripts" in Chrome Extension?

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?

like image 450
Glen Little Avatar asked Sep 12 '16 14:09

Glen Little


1 Answers

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.

like image 114
fregante Avatar answered Oct 15 '22 12:10

fregante