Skip to content

WebExtension module: Automatically registers your content_scripts on domains added via permission.request

License

Notifications You must be signed in to change notification settings

fregante/webext-dynamic-content-scripts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

6f1fd26 · Feb 1, 2025
Feb 1, 2025
Feb 1, 2025
Feb 1, 2025
Aug 3, 2019
Jan 15, 2023
Feb 1, 2025
Jan 15, 2023
Jun 14, 2022
Nov 24, 2022
Jun 11, 2024
May 16, 2023
Jan 24, 2021
Jul 1, 2024
Feb 1, 2025
Feb 1, 2025
Jan 15, 2023
Nov 24, 2022
Jan 15, 2023
Jan 15, 2023
Jan 15, 2023
Jan 16, 2023

Repository files navigation

webext-dynamic-content-scripts npm version

WebExtension module: Automatically registers your content_scripts on domains added via permissions.request

  • Browsers: Chrome, Firefox, and Safari
  • Manifest: v2 and v3

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.

Guides

How to let your users enable your extension on any domain.

Install

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';

Usage

For Manifest v2, refer to the usage-mv2 documentation.

You need to:

  • import webext-dynamic-content-scripts in the worker (no functions need to be called)
  • specify optional_host_permissions in the manifest to allow new permissions to be added
  • specify at least one content_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"]
		}
	]
}

Additional APIs

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.

Related

License

MIT © Federico Brigante