This repository contains example add-ons that show how to use content scripts in the Add-on SDK. Most of these examples are taken from the content scripts documentation: https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts.
Note that although all these examples should work fine and are complete runnable add-ons, they're not intended to be production quality. For example, they might omit error handling or not clean up after themselves.
You're welcome to add more examples! If you do, please update the list below with a link to the example, a very short description of what the example is demonstrating, and a list of the main modules it uses.
page-mod-content-script-file
Use contentScriptFile to pass a content script stored as a separate file.
page-mod-content-script-insulation
Show how content scripts and page scripts can't directly access each other.
page-mod-content-script-string
Use contentScript to pass a content script as a string.
page-mod-content-script-two-scripts
Use contentScript to pass two content scripts as strings.
page-mod-content-script-two-scripts-jquery
Pass two scripts, one as a file, one as a string.
page-mod-customevent-from-content-script
Use custom DOM events to send a message from a content script to a page script.
page-mod-customevent-from-page-script
Use custom DOM events to send a message from a page script to a content script.
page-mod-port
Listen for attach
to get a reference to a page-mod's worker
, then use port
to communicate between the main add-on and a content script.
Uses: page-mod.
Send JSON objects with port
.
page-mod-unsafewindow
Content script using unsafeWindow
to access variables defined in a page script.
page-mod-windowpostmessage-from-content-script
Use window.postMessage
to send a message from a content script to a page script.
page-mod-windowpostmessage-from-page-script
Use window.postMessage
to send a message from a content script to a page script.
page-worker-button-once
Use port.once()
to receive only the first occurrence of a message.
- MDN docs
- Modules used:
page-worker
,ui/button/action
page-worker-button-removelistener
Use port.removeListener()
to stop listening for messages.
- MDN docs
- Modules used:
page-worker
,ui/button/action
page-worker-port
Use port
to communicate between the main add-on and a content script.
- MDN docs
- Modules used:
page-worker
page-worker-x-domain-iframes
Grant a content script cross-domain privileges, using the cross-domain-content
option under the permissions
key, and use that to access content from a different domain inside an iframe.
- MDN docs
- Modules used:
page-worker
page-worker-x-domain-xhr
Grant a content script cross-domain privileges, using the cross-domain-content
option under the permissions
key, and use that to execute cross-domain XHR. Note that this example requires an API key from the Met Office DataPoint service.
- MDN docs
- Modules used:
page-worker
tabs-button-port
Use port
to communicate between the main add-on code and a content script.
- MDN docs
- Modules used:
tabs
,ui/button/action
tabs-cloneinto
Use cloneInto to share an object from a content script to a page script.
tabs-content-script-options
Provide configuration options to a content script with contentScriptOptions
.
tabs-content-script-string
Pass a content script as a string.
tabs-createobjectin
Use createObjectIn to create a new object in a page script scope, from a content script.
tabs-exportfunction
Use exportFunction to share a function from a content script to a page script.
tabs-port
Use port
to communicate between the main add-on code and a content script.