Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
webext-dynamic-content-scripts
Advanced tools
WebExtension module: Automatically registers your `content_scripts` on domains added via `permission.request` or on `activeTab`
WebExtension module: Automatically registers your
content_scripts
on domains added viapermissions.request
This module will automatically register your content_scripts
from manifest.json
into new domains granted via permissions.request()
, or via webext-permission-toggle.
The main use case is ship your extension with a minimal set of hosts and then allow the user to enable it on any domain; this way you don't need to use a broad <all_urls>
permission.
How to let your users enable your extension on any domain.
You can download the standalone bundle and include it in your manifest.json
. Or use npm:
npm install webext-dynamic-content-scripts
// This module is only offered as a ES Module
import 'webext-dynamic-content-scripts';
For Manifest v2, refer to the usage-mv2 documentation.
You need to:
webext-dynamic-content-scripts
in the worker (no functions need to be called)optional_host_permissions
in the manifest to allow new permissions to be addedcontent_scripts
// example background.worker.js
navigator.importScripts('webext-dynamic-content-scripts.js');
// example manifest.json
{
"permissions": ["scripting", "storage"],
"optional_host_permissions": ["*://*/*"],
"background": {
"service_worker": "background.worker.js"
},
"content_scripts": [
{
"matches": ["https://github.com/*"],
"css": ["content.css"],
"js": ["content.js"]
}
]
}
activeTab
trackingBy default, the module will only inject the content scripts into newly-permitted hosts, but it will ignore temporary permissions like activeTab
. If you also want to automatically inject the content scripts into every frame of tabs as soon as they receive the activeTab
permission, import a different entry point instead of the default one.
import 'webext-dynamic-content-scripts/including-active-tab.js';
Note This does not work well in Firefox because of some compounding bugs:
activeTab
seems to be lost after a reload- further
contextMenu
clicks receive a moz-extension URL rather than the current page’s URL
isContentScriptRegistered(url)
You can detect whether a specific URL will receive the content scripts by importing the utils
file:
import {isContentScriptRegistered} from 'webext-dynamic-content-scripts/utils.js';
if (await isContentScriptRegistered('https://google.com/search')) {
console.log('Either way, the content scripts are registered');
}
isContentScriptRegistered
returns a promise that resolves with a string indicating the type of injection ('static'
or 'dynamic'
) or false
if it won't be injected on the specified URL.
MIT © Federico Brigante
FAQs
WebExtension module: Automatically registers your `content_scripts` on domains added via `permission.request` or on `activeTab`
The npm package webext-dynamic-content-scripts receives a total of 178 weekly downloads. As such, webext-dynamic-content-scripts popularity was classified as not popular.
We found that webext-dynamic-content-scripts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.