Glen Little
Glen Little

Reputation: 7128

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?

Upvotes: 5

Views: 1353

Answers (1)

fregante
fregante

Reputation: 31698

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.

Upvotes: 4

Related Questions