• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
Add-ons
  1. MDN
  2. Mozilla
  3. Add-ons
  4. Add-on SDK
  5. High-Level APIs
  6. page-worker

page-worker

In This Article
  1. Usage
    1. Scripting page content
    2. Scripting trusted page content
  2. Globals
    1. Constructors
      1. Page(options)
  3. Page
    1. Methods
      1. destroy()
      2. postMessage(message)
      3. on(type, listener)
      4. removeListener(type, listener)
    2. Properties
      1. port
      2. contentURL
      3. allow
      4. include
      5. contentScriptFile
      6. contentScript
      7. contentScriptWhen
      8. contentScriptOptions
    3. Events
      1. message
      2. error

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).

From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

Stable

Create a permanent, invisible page and access its DOM.

Usage

The module exports a constructor function Page, which constructs a new page worker. A page worker may be destroyed, after which its memory is freed, and you must create a new instance to load another page.

You specify the page to load using the contentURL option to the Page() constructor. This can point to a remote file:

pageWorker = require("sdk/page-worker").Page({
  contentScript: "console.log(document.body.innerHTML);",
  contentURL: "http://en.wikipedia.org/wiki/Internet"
});

It can also point to an HTML file which you've packaged with your add-on. To do this, save the file in your add-on's data directory and create the URL using the data.url() method of the self module:

pageWorker = require("sdk/page-worker").Page({
  contentScript: "console.log(document.body.innerHTML);",
  contentURL: require("sdk/self").data.url("myFile.html")
});

From Firefox 34, you can use "./myFile.html" as an alias for self.data.url("myFile.html"). So you can rewrite the above code like this:

pageWorker = require("sdk/page-worker").Page({
  contentScript: "console.log(document.body.innerHTML);",
  contentURL: "./myFile.html"
});

You can load a new page by setting the page worker's contentURL property. In this example we fetch the first paragraph of a page from Wikipedia, then the first paragraph of a different page:

var getFirstParagraph = "var paras = document.getElementsByTagName('p');" +
                        "console.log(paras[0].textContent);" +
                        "self.port.emit('loaded');"
pageWorker = require("sdk/page-worker").Page({
  contentScript: getFirstParagraph,
  contentURL: "http://en.wikipedia.org/wiki/Chalk"
});
pageWorker.port.on("loaded", function() {
  pageWorker.contentURL = "http://en.wikipedia.org/wiki/Cheese"
});

Scripting page content

To access the page's DOM you need to attach a script to it. In the SDK these scripts are called "content scripts" because they're explicitly used for interacting with web content.

You can specify one or more content scripts to load into the page using the contentScript or contentScriptFile options to the Page() constructor. With contentScript you pass the script as a string, as in the examples above. With contentScriptFile you pass a URL which points to a script saved under your add-on's data directory. You construct the URL using the data.url() method of the self module.

While content scripts can access DOM content, they can't access any of the SDK APIs, so in many cases you'll need to exchange messages between the content script and your main add-on code for a complete solution.

For example, the content script might read some content and send it back to the main add-on, which could store it using the simple-storage API. You can communicate with the script using either the postMessage() API or (preferably, usually) the port API.

For example, this add-on loads a page from Wikipedia, and runs a content script in it to send all the headers back to the main add-on code:

var pageWorkers = require("sdk/page-worker");
// This content script sends header titles from the page to the add-on:
var script = "var elements = document.querySelectorAll('h2 > span'); " +
             "for (var i = 0; i < elements.length; i++) { " +
             "  postMessage(elements[i].textContent) " +
             "}";
// Create a page worker that loads Wikipedia:
pageWorkers.Page({
  contentURL: "http://en.wikipedia.org/wiki/Internet",
  contentScript: script,
  contentScriptWhen: "ready",
  onMessage: function(message) {
    console.log(message);
  }
});

For conciseness, this example creates the content script as a string and uses the contentScript property. In your own add-ons, you will probably want to create your content scripts in separate files and pass their URLs using the contentScriptFile property.

Unless your content script is extremely simple and consists only of a static string, don't use contentScript: if you do, you may have problems getting your add-on approved on AMO.

Instead, keep the script in a separate file and load it using contentScriptFile. This makes your code easier to maintain, secure, debug and review.

To learn much more about content scripts, see the Working with Content Scripts guide.

Scripting trusted page content

We've already seen that you can package HTML files in your add-on's data directory and load them using page-worker. We can call this "trusted" content, because unlike content loaded from a source outside the add-on, the add-on author knows exactly what it's doing. To interact with trusted content you don't need to use content scripts: you can just include a script from the HTML file in the normal way, using <script> tags.

Like a content script, these scripts can communicate with the add-on code using the postMessage() API or the port API. The crucial difference is that these scripts access the postMessage and port objects through the addon object, whereas content scripts access them through the self object.

So given an add-on that loads trusted content and uses content scripts to access it, there are typically three changes you have to make, if you want to use normal page scripts instead:

  • in the content script: change occurrences of self to addon. For example, self.port.emit("my-event") becomes addon.port.emit("my-event").

  • in the HTML page itself: add a <script> tag to load the script. So if your content script is saved under data as "my-script.js", you need a line like <script src="my-script.js"></script> in the page header.

  • in the "main.js" file: remove the contentScriptFile option in the Page() constructor.

Globals

Constructors

Page(options)

Creates an uninitialized page worker instance.

Parameters

options : object
Optional options:

Name Type  
contentURL string

The URL of the content to load in the panel.

allow object

An object with keys to configure the permissions on the page worker. The boolean key script controls if scripts from the page are allowed to run. script defaults to true.

include

string,

RegExp,

array of

(String or RegExp)

This is useful when your page worker loads a page which will redirect to other pages.

A match pattern string or an array of match pattern strings. These define the documents to which the page-worker's content worker applies. At least one match pattern must be supplied.

See the match-pattern module for a detailed description of match pattern syntax.

contentScriptFile string,array

A local file URL or an array of local file URLs of content scripts to load. Content scripts specified by this option are loaded before those specified by the contentScript option. See Working with Content Scripts for help on setting this property.

contentScript string,array

A string or an array of strings containing the texts of content scripts to load. Content scripts specified by this option are loaded after those specified by the contentScriptFile option.

contentScriptWhen string

When to load the content scripts. This may take one of the following values:

  • "start": load content scripts immediately after the document element for the page is inserted into the DOM, but before the DOM content itself has been loaded
  • "ready": load content scripts once DOM content has been loaded, corresponding to the DOMContentLoaded event
  • "end": load content scripts once all the content (DOM, JS, CSS, images) for the page has been loaded, at the time the window.onload event fires

This property is optional and defaults to "end".

contentScriptOptions object

Read-only value exposed to content scripts under self.options property.

Any kind of jsonable value (object, array, string, etc.) can be used here. Optional.

onMessage function

Use this to add a listener to the page worker's message event.

Page

A Page object loads the page specified by its contentURL option and executes any content scripts that have been supplied to it in the contentScript and contentScriptFile options.

The page is not displayed to the user.

The page worker is loaded as soon as the Page object is created and stays loaded until its destroy method is called or the add-on is unloaded.

Methods

destroy()

Unloads the page worker. After you destroy a page worker, its memory is freed and you must create a new instance if you need to load another page.

postMessage(message)

Sends a message to the content scripts.

Parameters

message : value
The message to send. Must be JSON-able.

on(type, listener)

Registers an event listener with the page worker. See Working with Events for help with events.

Parameters

type : string
The type of event to listen for.

listener : function
The listener function that handles the event.

removeListener(type, listener)

Unregisters an event listener from the page worker.

Parameters

type : string
The type of event for which listener was registered.

listener : function
The listener function that was registered.

Properties

port

Object that allows you to:

  • send events to the content script using the port.emit function
  • receive events from the content script using the port.on function

See the guide to communicating using port for details.

contentURL

The URL of content to load. This can point to local content loaded from your add-on's "data" directory or remote content. Setting it loads the content immediately.

allow

A object describing permissions for the content. It contains a single key named script whose value is a boolean that indicates whether or not to execute script in the content. script defaults to true.

include

A set of match patterns to define the urls which the page-worker's content script will be applied. This is useful when using pages which redirect to other pages in your page-worker.

contentScriptFile

A local file URL or an array of local file URLs of content scripts to load.

contentScript

A string or an array of strings containing the texts of content scripts to load.

contentScriptWhen

When to load the content scripts. This may have one of the following values:

  • "start": load content scripts immediately after the document element for the page is inserted into the DOM, but before the DOM content itself has been loaded
  • "ready": load content scripts once DOM content has been loaded, corresponding to the DOMContentLoaded event
  • "end": load content scripts once all the content (DOM, JS, CSS, images) for the page has been loaded, at the time the window.onload event fires

contentScriptOptions

Read-only value exposed to content scripts under self.options property.

Any kind of jsonable value (object, array, string, etc.) can be used here. Optional.

Events

message

If you listen to this event you can receive message events from content scripts associated with this page worker. When a content script posts a message using self.postMessage(), the message is delivered to the add-on code in the page worker's message event.

Arguments

value : Listeners are passed a single argument which is the message posted from the content script. The message can be any JSON-serializable value

error

This event is emitted when an uncaught runtime error occurs in one of the page worker's content scripts.

Arguments

Error : Listeners are passed a single argument, the Error object. 

Document Tags and Contributors

Tags: 
  • Add-on SDK
 Contributors to this page: wbamberg, evold
 Last updated by: wbamberg, Dec 1, 2016, 10:22:27 AM
See also
  1. WebExtensions
  2. Getting started
    1. What are WebExtensions?
    2. Your first WebExtension
    3. Your second WebExtension
    4. Anatomy of a WebExtension
    5. Example WebExtensions
  3. How to
    1. Intercept HTTP requests
    2. Modify a web page
    3. Add a button to the toolbar
    4. Implement a settings page
  4. Concepts
    1. Using the JavaScript APIs
    2. User interface components
    3. Content scripts
    4. Match patterns
    5. Internationalization
    6. Content Security Policy
    7. Native messaging
  5. Porting
    1. Porting a Google Chrome extension
    2. Porting a legacy Firefox add-on
    3. Embedded WebExtensions
    4. Comparison with the Add-on SDK
    5. Comparison with XUL/XPCOM extensions
    6. Chrome incompatibilities
  6. Firefox workflow
    1. Temporary Installation in Firefox
    2. Debugging
    3. Getting started with web-ext
    4. web-ext command reference
    5. WebExtensions and the Add-on ID
    6. Publishing your WebExtension
  7. JavaScript APIs
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browsingData
    6. commands
    7. contextMenus
    8. contextualIdentities
    9. cookies
    10. downloads
    11. events
    12. extension
    13. extensionTypes
    14. history
    15. i18n
    16. identity
    17. idle
    18. management
    19. notifications
    20. omnibox
    21. pageAction
    22. runtime
    23. sessions
    24. sidebarAction
    25. storage
    26. tabs
    27. topSites
    28. webNavigation
    29. webRequest
    30. windows
  8. Manifest keys
    1. applications
    2. author
    3. background
    4. browser_action
    5. chrome_url_overrides
    6. commands
    7. content_scripts
    8. content_security_policy
    9. default_locale
    10. description
    11. developer
    12. homepage_url
    13. icons
    14. manifest_version
    15. name
    16. omnibox
    17. options_ui
    18. page_action
    19. permissions
    20. short_name
    21. sidebar_action
    22. version
    23. web_accessible_resources
  9. Add-on SDK
  10. Getting started
    1. Installation
    2. Getting started
    3. Troubleshooting
  11. High-Level APIs
    1. addon-page
    2. base64
    3. clipboard
    4. context-menu
    5. hotkeys
    6. indexed-db
    7. l10n
    8. notifications
    9. page-mod
    10. page-worker
    11. panel
    12. passwords
    13. private-browsing
    14. querystring
    15. request
    16. selection
    17. self
    18. simple-prefs
    19. simple-storage
    20. system
    21. tabs
    22. timers
    23. ui
    24. url
    25. webextension
    26. widget
    27. windows
  12. Low-Level APIs
    1. /loader
    2. chrome
    3. console/plain-text
    4. console/traceback
    5. content/content
    6. content/loader
    7. content/mod
    8. content/symbiont
    9. content/worker
    10. core/heritage
    11. core/namespace
    12. core/promise
    13. dev/panel
    14. event/core
    15. event/target
    16. frame/hidden-frame
    17. frame/utils
    18. fs/path
    19. io/byte-streams
    20. io/file
    21. io/text-streams
    22. lang/functional
    23. lang/type
    24. loader/cuddlefish
    25. loader/sandbox
    26. net/url
    27. net/xhr
    28. places/bookmarks
    29. places/favicon
    30. places/history
    31. platform/xpcom
    32. preferences/event-target
    33. preferences/service
    34. remote/child
    35. remote/parent
    36. stylesheet/style
    37. stylesheet/utils
    38. system/child_process
    39. system/environment
    40. system/events
    41. system/runtime
    42. system/unload
    43. system/xul-app
    44. tabs/utils
    45. test/assert
    46. test/harness
    47. test/httpd
    48. test/runner
    49. test/utils
    50. ui/button/action
    51. ui/button/toggle
    52. ui/frame
    53. ui/id
    54. ui/sidebar
    55. ui/toolbar
    56. util/array
    57. util/collection
    58. util/deprecate
    59. util/list
    60. util/match-pattern
    61. util/object
    62. util/uuid
    63. window/utils
  13. Firefox for Android
  14. Getting started
    1. Walkthrough
    2. Debugging
    3. Code snippets
  15. APIs
    1. Accounts.jsm
    2. BrowserApp
    3. HelperApps.jsm
    4. Home.jsm
    5. HomeProvider.jsm
    6. JavaAddonManager.jsm
    7. NativeWindow
    8. Notifications.jsm
    9. PageActions.jsm
    10. Prompt.jsm
    11. RuntimePermissions.jsm
    12. Snackbars.jsm
    13. Sound.jsm
    14. Tab
  16. Legacy
  17. Restartless extensions
    1. Overview
  18. Overlay extensions
    1. Overview
  19. Themes
  20. Lightweight themes
    1. Overview
  21. Complete themes
    1. Overview
  22. Publishing add-ons
  23. Guides
    1. Signing and distribution overview
    2. Submit an add-on
    3. Review policies
    4. Developer agreement
    5. Featured add-ons
    6. Contact addons.mozilla.org
  24. Community and support
  25. Channels
    1. Add-ons blog
    2. Add-on forums
    3. Stack Overflow
    4. Development newsgroup
    5. IRC Channel