10.0.1 • Published 3 months ago

webext-dynamic-content-scripts v10.0.1

Weekly downloads
859
License
MIT
Repository
github
Last release
3 months ago

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-domain-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"],
	"optional_host_permissions": ["*://*/*"],
	"background": {
		"service_worker": "background.worker.js"
	},
	"content_scripts": [
		{
			"matches": ["https://github.com/*"],
			"css": ["content.css"],
			"js": ["content.js"]
		}
	]
}

activeTab tracking

By 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

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

Permissions

Others

  • webext-options-sync - Helps you manage and autosave your extension's options. Chrome and Firefox.
  • webext-storage-cache - Map-like promised cache storage with expiration. Chrome and Firefox
  • webext-detect-page - Detects where the current browser extension code is being run. Chrome and Firefox.
  • web-ext-submit - Wrapper around Mozilla’s web-ext to submit extensions to AMO.
  • Awesome-WebExtensions - A curated list of awesome resources for WebExtensions development.

License

MIT © Federico Brigante

10.0.1

3 months ago

10.0.0

7 months ago

10.0.0-0

11 months ago

9.2.1-0

1 year ago

10.0.0-1

11 months ago

9.2.2

1 year ago

9.2.1

1 year ago

10.0.0-2

11 months ago

10.0.0-3

11 months ago

9.1.1-0

1 year ago

9.1.0

1 year ago

9.2.0-5

1 year ago

9.2.0-4

1 year ago

9.2.0-3

1 year ago

9.2.0-2

1 year ago

9.2.0-1

1 year ago

9.2.0-0

1 year ago

9.2.0-6

1 year ago

9.2.0

1 year ago

9.3.0-0

1 year ago

9.1.0-0

1 year ago

9.1.0-2

1 year ago

9.1.0-1

1 year ago

9.0.0

1 year ago

9.0.0-1

2 years ago

9.0.0-0

2 years ago

8.1.1

2 years ago

8.1.0

2 years ago

8.0.2

2 years ago

8.0.1-0

2 years ago

8.0.1

2 years ago

8.0.0-3

3 years ago

8.0.0-1

3 years ago

8.0.0

3 years ago

8.0.0-0

3 years ago

7.2.0-0

3 years ago

7.1.2

3 years ago

7.1.1

3 years ago

7.1.0

3 years ago

7.0.0

4 years ago

6.0.5

4 years ago

6.0.4

4 years ago

6.0.3

5 years ago

6.0.3-0

5 years ago

6.0.2

5 years ago

6.0.1

5 years ago

6.0.0

5 years ago

6.0.0-2

5 years ago

6.0.0-1

5 years ago

6.0.0-0

5 years ago

5.0.2

5 years ago

5.0.2-0

5 years ago

5.0.1

6 years ago

5.0.0

6 years ago

5.0.0-2

6 years ago

5.0.0-1

6 years ago

5.0.0-0

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

3.0.0-0

6 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago