Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 510057 of page-worker

  • Revision slug: Mozilla/Add-ons/SDK/High-Level_APIs/page-worker
  • Revision title: page-worker
  • Revision id: 510057
  • Created:
  • Creator: wbamberg
  • Is current revision? No
  • Comment
Tags: 

Revision Content

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: "https://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")
});

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: "https://en.wikipedia.org/wiki/Chalk"
});

pageWorker.port.on("loaded", function() {
  pageWorker.contentURL = "https://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: "https://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.

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.

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.

Revision Source

<div class="note">
 <p>Stable <!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at https://mozilla.org/MPL/2.0/. --><!-- contributed by Felipe Gomes [[email protected]] --></p>
</div>
<p><span class="seoSummary">Create a permanent, invisible page and access its DOM.</span></p>
<h2 id="Usage">Usage</h2>
<p>The module exports a constructor function <code>Page</code>, 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.</p>
<p>You specify the page to load using the <code>contentURL</code> option to the <a href="/en-US/Add-ons/SDK/High-Level_APIs/page-worker#Page(options)"><code>Page()</code> constructor</a>. This can point to a remote file:</p>
<pre class="brush: js">
pageWorker = require("sdk/page-worker").Page({
  contentScript: "console.log(document.body.innerHTML);",
  contentURL: "https://en.wikipedia.org/wiki/Internet"
});</pre>
<p>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 <code>data</code> directory and create the URL using the <code>data.url()</code> method of the <a href="/en-US/Add-ons/SDK/High-Level_APIs/self"><code>self</code></a> module:</p>
<pre class="brush: js">
pageWorker = require("sdk/page-worker").Page({
  contentScript: "console.log(document.body.innerHTML);",
  contentURL: require("sdk/self").data.url("myFile.html")
});</pre>
<p>You can load a new page by setting the page worker's <code>contentURL</code> property. In this example we fetch the first paragraph of a page from Wikipedia, then the first paragraph of a different page:</p>
<pre class="brush: js">
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: "https://en.wikipedia.org/wiki/Chalk"
});

pageWorker.port.on("loaded", function() {
  pageWorker.contentURL = "https://en.wikipedia.org/wiki/Cheese"
});</pre>
<h3 id="Scripting_page_content">Scripting page content</h3>
<p>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.</p>
<p>You can specify one or more content scripts to load into the page using the <code>contentScript</code> or <code>contentScriptFile</code> options to the <a href="/en-US/Add-ons/SDK/High-Level_APIs/page-worker#Page(options)"><code>Page()</code> constructor</a>. With <code>contentScript</code> you pass the script as a string, as in the examples above. With <code>contentScriptFile</code> you pass a URL which points to a script saved under your add-on's <code>data</code> directory. You construct the URL using the <code>data.url()</code> method of the <a href="/en-US/Add-ons/SDK/High-Level_APIs/self"><code>self</code></a> module.</p>
<p>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.</p>
<p>For example, the content script might read some content and send it back to the main add-on, which could store it using the <a href="/en-US/Add-ons/SDK/High-Level_APIs/simple-storage"><code>simple-storage</code></a> API. You can communicate with the script using either the <a href="/en-US/Add-ons/SDK/Guides/using_postMessage"><code>postMessage()</code></a> API or (preferably, usually) the <a href="/en-US/Add-ons/SDK/Guides/using_port"><code>port</code></a> API.</p>
<p>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:</p>
<pre class="brush: js">
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 &gt; span'); " +
             "for (var i = 0; i &lt; elements.length; i++) { " +
             "  postMessage(elements[i].textContent) " +
             "}";

// Create a page worker that loads Wikipedia:
pageWorkers.Page({
  contentURL: "https://en.wikipedia.org/wiki/Internet",
  contentScript: script,
  contentScriptWhen: "ready",
  onMessage: function(message) {
    console.log(message);
  }
});</pre>
<p>For conciseness, this example creates the content script as a string and uses the <code>contentScript</code> property. In your own add-ons, you will probably want to create your content scripts in separate files and pass their URLs using the <code>contentScriptFile</code> property.</p>
<div class="warning">
 <p>Unless your content script is extremely simple and consists only of a static string, don't use <code>contentScript</code>: if you do, you may have problems getting your add-on approved on AMO.</p>
 <p>Instead, keep the script in a separate file and load it using <code>contentScriptFile</code>. This makes your code easier to maintain, secure, debug and review.</p>
</div>
<p>To learn much more about content scripts, see the <a href="dev-guide/guides/content-scripts/index.html">Working with Content Scripts</a> guide.</p>
<h3 id="Scripting_trusted_page_content">Scripting trusted page content</h3>
<p>We've already seen that you can package HTML files in your add-on's <code>data</code> directory and load them using <code>page-worker</code>. 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 <code>&lt;script&gt;</code> tags.</p>
<p>Like a content script, these scripts can communicate with the add-on code using the <a href="/en-US/Add-ons/SDK/Guides/using_postMessage"><code>postMessage()</code></a> API or the <a href="/en-US/Add-ons/SDK/Guides/using_port"><code>port</code></a> API. The crucial difference is that these scripts access the <code>postMessage</code> and <code>port</code> objects through the <code>addon</code> object, whereas content scripts access them through the <code>self</code> object.</p>
<p>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:</p>
<ul>
 <li>
  <p><strong>in the content script</strong>: change occurrences of <code>self</code> to <code>addon</code>. For example, <code>self.port.emit("my-event")</code> becomes <code>addon.port.emit("my-event")</code>.</p>
 </li>
 <li>
  <p><strong>in the HTML page itself</strong>: add a <code>&lt;script&gt;</code> tag to load the script. So if your content script is saved under <code>data</code> as "my-script.js", you need a line like <code>&lt;script src="my-script.js"&gt;&lt;/script&gt;</code> in the page header.</p>
 </li>
 <li>
  <p><strong>in the "main.js" file</strong>: remove the <code>contentScriptFile</code> option in the <code>Page()</code> constructor.</p>
 </li>
</ul>
<h2 id="Globals">Globals</h2>
<h3 id="Constructors">Constructors</h3>
<h4 class="addon-sdk-api-name" id="Page(options)"><code>Page(options)</code></h4>
<p>Creates an uninitialized page worker instance.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>options : object</strong><br />
 Optional options:</p>
<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Name</th>
   <th scope="col">Type</th>
   <th scope="col">&nbsp;</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>contentURL</td>
   <td>string</td>
   <td>
    <p>The URL of the content to load in the panel.</p>
   </td>
  </tr>
  <tr>
   <td>allow</td>
   <td>object</td>
   <td>
    <p>An object with keys to configure the permissions on the page worker. The boolean key <code>script</code> controls if scripts from the page are allowed to run. <code>script</code> defaults to true.</p>
   </td>
  </tr>
  <tr>
   <td>contentScriptFile</td>
   <td>string,array</td>
   <td>
    <p>A local file URL or an array of local file URLs of content scripts to load. Content scripts specified by this option are loaded <em>before</em> those specified by the <code>contentScript</code> option. See <a href="/en-US/Add-ons/SDK/Guides/Content_Scripts">Working with Content Scripts</a> for help on setting this property.</p>
   </td>
  </tr>
  <tr>
   <td>contentScript</td>
   <td>string,array</td>
   <td>
    <p>A string or an array of strings containing the texts of content scripts to load. Content scripts specified by this option are loaded <em>after</em> those specified by the <code>contentScriptFile</code> option.</p>
   </td>
  </tr>
  <tr>
   <td>contentScriptWhen</td>
   <td>string</td>
   <td>
    <p>When to load the content scripts. This may take one of the following values:</p>
    <ul>
     <li>"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</li>
     <li>"ready": load content scripts once DOM content has been loaded, corresponding to the <a href="https://developer.mozilla.org/en/Gecko-Specific_DOM_Events">DOMContentLoaded</a> event</li>
     <li>"end": load content scripts once all the content (DOM, JS, CSS, images) for the page has been loaded, at the time the <a href="https://developer.mozilla.org/en/DOM/window.onload">window.onload event</a> fires</li>
    </ul>
    <p>This property is optional and defaults to "end".</p>
   </td>
  </tr>
  <tr>
   <td>contentScriptOptions</td>
   <td>object</td>
   <td>
    <p>Read-only value exposed to content scripts under <code>self.options</code> property.</p>
    <p>Any kind of jsonable value (object, array, string, etc.) can be used here. Optional.</p>
   </td>
  </tr>
  <tr>
   <td>onMessage</td>
   <td>function</td>
   <td>
    <p>Use this to add a listener to the page worker's <code>message</code> event.</p>
   </td>
  </tr>
 </tbody>
</table>
<h2 id="Page">Page</h2>
<p>A <code>Page</code> object loads the page specified by its <code>contentURL</code> option and executes any content scripts that have been supplied to it in the <code>contentScript</code> and <code>contentScriptFile</code> options.</p>
<p>The page is not displayed to the user.</p>
<p>The page worker is loaded as soon as the <code>Page</code> object is created and stays loaded until its <code>destroy</code> method is called or the add-on is unloaded.</p>
<h3 id="Methods">Methods</h3>
<h4 class="addon-sdk-api-name" id="destroy()"><code>destroy()</code></h4>
<p>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.</p>
<h4 class="addon-sdk-api-name" id="postMessage(message)"><code>postMessage(message)</code></h4>
<p>Sends a message to the content scripts.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>message : value</strong><br />
 The message to send. Must be JSON-able.</p>
<h4 class="addon-sdk-api-name" id="on(type.2C_listener)"><code>on(type, listener)</code></h4>
<p>Registers an event listener with the page worker. See <a href="/en-US/Add-ons/SDK/Guides/Working_with_Events">Working with Events</a> for help with events.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>type : string</strong><br />
 The type of event to listen for.</p>
<p><strong>listener : function</strong><br />
 The listener function that handles the event.</p>
<h4 class="addon-sdk-api-name" id="removeListener(type.2C_listener)"><code>removeListener(type, listener)</code></h4>
<p>Unregisters an event listener from the page worker.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>type : string</strong><br />
 The type of event for which <code>listener</code> was registered.</p>
<p><strong>listener : function</strong><br />
 The listener function that was registered.</p>
<h3 id="Properties">Properties</h3>
<h4 class="addon-sdk-api-name" id="port"><code>port</code></h4>
<p>Object that allows you to:</p>
<ul>
 <li>send events to the content script using the <code>port.emit</code> function</li>
 <li>receive events from the content script using the <code>port.on</code> function</li>
</ul>
<p>See the guide to <a href="/en-US/Add-ons/SDK/Guides/using_port"> communicating using <code>port</code></a> for details.</p>
<h4 class="addon-sdk-api-name" id="contentURL"><code>contentURL</code></h4>
<p>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.</p>
<h4 class="addon-sdk-api-name" id="allow"><code>allow</code></h4>
<p>A object describing permissions for the content. It contains a single key named <code>script</code> whose value is a boolean that indicates whether or not to execute script in the content. <code>script</code> defaults to true.</p>
<h4 class="addon-sdk-api-name" id="contentScriptFile"><code>contentScriptFile</code></h4>
<p>A local file URL or an array of local file URLs of content scripts to load.</p>
<h4 class="addon-sdk-api-name" id="contentScript"><code>contentScript</code></h4>
<p>A string or an array of strings containing the texts of content scripts to load.</p>
<h4 class="addon-sdk-api-name" id="contentScriptWhen"><code>contentScriptWhen</code></h4>
<p>When to load the content scripts. This may have one of the following values:</p>
<ul>
 <li>"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</li>
 <li>"ready": load content scripts once DOM content has been loaded, corresponding to the <a href="https://developer.mozilla.org/en/Gecko-Specific_DOM_Events">DOMContentLoaded</a> event</li>
 <li>"end": load content scripts once all the content (DOM, JS, CSS, images) for the page has been loaded, at the time the <a href="https://developer.mozilla.org/en/DOM/window.onload">window.onload event</a> fires</li>
</ul>
<h4 class="addon-sdk-api-name" id="contentScriptOptions"><code>contentScriptOptions</code></h4>
<p>Read-only value exposed to content scripts under <code>self.options</code> property.</p>
<p>Any kind of jsonable value (object, array, string, etc.) can be used here. Optional.</p>
<h3 id="Events">Events</h3>
<h4 class="addon-sdk-api-name" id="message"><code>message</code></h4>
<p>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 <code>self.postMessage()</code>, the message is delivered to the add-on code in the page worker's <code>message</code> event.</p>
<h5 id="Arguments">Arguments</h5>
<p><strong>value</strong> : Listeners are passed a single argument which is the message posted from the content script. The message can be any <a href="/en-US/Add-ons/SDK/Guides/Content_Scripts/using_port#JSON-Serializable_Values">JSON-serializable value</a></p>
<h4 class="addon-sdk-api-name" id="error"><code>error</code></h4>
<p>This event is emitted when an uncaught runtime error occurs in one of the page worker's content scripts.</p>
<h5 id="Arguments">Arguments</h5>
<p><strong>Error</strong> : Listeners are passed a single argument, the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error">Error</a> object.</p>
Revert to this revision