Skip to content

Conversation

@fregante
Copy link
Owner

@fregante fregante commented Aug 3, 2019

Closes #4

The previous versions of webext-dynamic-content-scripts tried to do something that was not possible while only truly supporting its main use:
automatically registering existing scripts on new domains.

Migration from v5

DCS.addToTab

Use chrome.tabs.executeScript/chrome.tabs.insertCSS directly. Also see my next comment.

If you want to ensure scripts are only injected once, try webext-content-script-ping.

DCS.addToFutureTabs

With argument: DCS.addToFutureTabs(scripts)

Use Firefox’ new contentScripts.register API; there's a polyfill for Chrome.

Without argument: DCS.addToFutureTabs()

You can keep using webext-dynamic-content-scripts but:

  1. You no longer have to include it as a content script; only include it as a background script.
  2. There's no API. Just import the script:
// in background.js
import 'webext-dynamic-content-scripts';

Or if you don't use a bundler, you only need include it as a background script:

{
	"background": {
		"scripts": [
			"webext-dynamic-content-scripts.js"
		]
	}
}

fregante and others added 4 commits June 25, 2019 13:23
The module was written with one use case in mind, registering scripts on new new hosts. Other situations were not working
@fregante fregante merged commit 4152f51 into master Aug 3, 2019
@fregante fregante deleted the restart branch August 3, 2019 06:20
@fregante
Copy link
Owner Author

fregante commented Nov 20, 2019

Here's a migration example of an advanced usage of DCS.addToTab

v5

await DCS.addToTab(tab, {
	run_at: 'document_start',
	all_frames: true,
	css: [
		'vendor/humane-ghosttext.css',
		'scripts/content.css'
	],
	js: [
		'vendor/webext-dynamic-content-scripts.js',
		'vendor/humane-ghosttext.min.js',
		'vendor/one-event.browser.js',
		'scripts/unsafe-messenger.js',
		'scripts/content.js'
	]
});

v6

Using the Promisified browser.* APIs in Firefox and webextension-polyfill

const defaults = {
	runAt: 'document_start',
	allFrames: true
};
await Promise.all([
	browser.tabs.insertCSS(tab, {...defaults, file: 'vendor/humane-ghosttext.css'}),
	browser.tabs.insertCSS(tab, {...defaults, file: 'vendor/humane-ghosttext.css'}),
	browser.tabs.executeScript(tab, {...defaults, file: 'vendor/webext-dynamic-content-scripts.js'}),
	browser.tabs.executeScript(tab, {...defaults, file: 'vendor/humane-ghosttext.min.js'}),
	browser.tabs.executeScript(tab, {...defaults, file: 'vendor/one-event.browser.js'}),
	browser.tabs.executeScript(tab, {...defaults, file: 'scripts/unsafe-messenger.js'}),
	browser.tabs.executeScript(tab, {...defaults, file: 'scripts/content.js'}),
	browser.tabs.executeScript(tab, {...defaults, code: 'startGT()'}) // Even inlined code, not possible with v5 at all
]);

Regular chrome.* APIs work just as well, but you can't await them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewrite with declarative APIs

2 participants