Files
webextensions-examples/content-script-register/background.js
T
rebloor 177f603557 New user script example (#438)
* New user script example

This PR is the content from [PR 426](https://github.com/mdn/webextensions-examples/pull/426), originally submitted by Irene, addressing the feedback on the readme content.

* Including the following changes:
* renaming the content script example and updating the readme file to reference the user script example
* renaming the user script example and versioning as v1
* addressing feedback on the user script example: limiting input to hosts and script, addressing code and HTML/CSS comments

I haven't changed the "eating" script example, I feel it is useful to have similar demonstrations in the content and user script examples to make it easier for developers to compare and contrast.

* Various changes for feedback

* Added a user script ID that is stored in the registered script's metadata and then used in the API script to key the data stored in local storage

* Update to emphasize scoping of local storage using an ID provided in the user script metadata

Co-authored-by: Richard Bloor <rbloor@atlassian.com>
2020-03-19 08:43:36 -07:00

23 lines
377 B
JavaScript

'use strict';
var registered = null;
async function registerScript(message) {
let hosts = message.hosts;
let code = message.code;
if (registered) {
registered.unregister();
}
registered = await browser.contentScripts.register({
matches: hosts,
js: [{code}],
runAt: "document_idle"
});
}
browser.runtime.onMessage.addListener(registerScript);