cfx
Version:
programmatically use cfx with node.js
481 lines (367 loc) • 14.1 kB
Markdown
<!-- 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 http://mozilla.org/MPL/2.0/. -->
<!-- contributed by Dietrich Ayala [dietrich.com] -->
<!-- edited by Noelle Murata [fiveinchpixie.com] -->
The `tabs` module provides easy access to tabs and tab-related events.
The module itself can be used like a basic list of all opened
tabs across all windows. In particular, you can enumerate it:
var tabs = require('sdk/tabs');
for each (var tab in tabs)
console.log(tab.title);
You can also access individual tabs by index:
var tabs = require('sdk/tabs');
tabs.on('ready', function () {
console.log('first: ' + tabs[0].title);
console.log('last: ' + tabs[tabs.length-1].title);
});
You can open a new tab, specifying various properties including location:
var tabs = require("sdk/tabs");
tabs.open("http://www.example.com");
You can register event listeners to be notified when tabs open, close, finish
loading DOM content, or are made active or inactive:
var tabs = require("sdk/tabs");
// Listen for tab openings.
tabs.on('open', function onOpen(tab) {
myOpenTabs.push(tab);
});
// Listen for tab content loads.
tabs.on('ready', function(tab) {
console.log('tab is loaded', tab.title, tab.url)
});
You can get and set various properties of tabs (but note that properties
relating to the tab's content, such as the URL, will not contain valid
values until after the tab's `ready` event fires). By setting the `url`
property you can load a new page in the tab:
var tabs = require("sdk/tabs");
tabs.on('activate', function(tab) {
tab.url = "http://www.example.com";
});
You can attach a [content script](dev-guide/guides/content-scripts/index.html)
to the page hosted in a tab, and use that to access and manipulate the page's
content (see the
[Modifying the Page Hosted by a Tab](dev-guide/tutorials/modifying-web-pages-tab.html) tutorial):
var tabs = require("sdk/tabs");
tabs.on('activate', function(tab) {
tab.attach({
contentScript: 'self.postMessage(document.body.innerHTML);',
onMessage: function (message) {
console.log(message);
}
});
});
## Private Windows ##
If your add-on has not opted into private browsing, then you won't see any
tabs that are hosted by private browser windows.
Tabs hosted by private browser windows won't be seen if you enumerate the
`tabs` module itself, and you won't receive any events for them.
To learn more about private windows, how to opt into private browsing, and how
to support private browsing, refer to the
[documentation for the `private-browsing` module](modules/sdk/private-browsing.html).
<api name="activeTab">
{Tab}
The currently active tab in the active window. This property is read-only. To
activate a `Tab` object, call its `activate` method.
**Example**
// Get the active tab's title.
var tabs = require("sdk/tabs");
console.log("title of active tab is " + tabs.activeTab.title);
</api>
<api name="length">
{number}
The number of open tabs across all windows.
</api>
<api name="open">
Opens a new tab. The new tab will open in the active window or in a new window,
depending on the `inNewWindow` option.
**Example**
var tabs = require("sdk/tabs");
// Open a new tab on active window and make tab active.
tabs.open("http://www.mysite.com");
// Open a new tab in a new window and make it active.
tabs.open({
url: "http://www.mysite.com",
inNewWindow: true
});
// Open a new tab on active window in the background.
tabs.open({
url: "http://www.mysite.com",
inBackground: true
});
// Open a new tab as an app tab and do something once it's open.
tabs.open({
url: "http://www.mysite.com",
isPinned: true,
onOpen: function onOpen(tab) {
// do stuff like listen for content
// loading.
}
});
options {object}
An object containing configurable options for how and where the tab will be
opened, as well as a listeners for the tab events.
If the only option being used is `url`, then a bare string URL can be passed to
`open` instead of adding at a property of the `options` object.
[url] {string}
String URL to be opened in the new tab.
This is a required property.
[inNewWindow] {boolean}
If present and true, a new browser window will be opened and the URL will be
opened in the first tab in that window. This is an optional property.
[inBackground] {boolean}
If present and true, the new tab will be opened to the right of the active tab
and will not be active. This is an optional property.
isPrivate {boolean}
Boolean which will determine whether the new tab should be private or not.
If your add-on does not support private browsing this will have no effect.
See the [private-browsing](modules/sdk/private-browsing.html) documentation for more information.
[isPinned] {boolean}
If present and true, then the new tab will be pinned as an
[app tab](http://support.mozilla.com/en-US/kb/what-are-app-tabs).
[onOpen] {function}
A callback function that will be registered for 'open' event.
This is an optional property.
[onClose] {function}
A callback function that will be registered for 'close' event.
This is an optional property.
[onReady] {function}
A callback function that will be registered for 'ready' event.
This is an optional property.
[onLoad] {function}
A callback function that will be registered for 'load' event.
This is an optional property.
[onPageShow] {function}
A callback function that will be registered for 'pageshow' event.
This is an optional property.
[onActivate] {function}
A callback function that will be registered for 'activate' event.
This is an optional property.
[onDeactivate] {function}
A callback function that will be registered for 'deactivate' event.
This is an optional property.
</api>
<api name="Tab">
A `Tab` instance represents a single open tab. It contains various tab
properties, several methods for manipulation, as well as per-tab event
registration.
Tabs emit all the events described in the Events section. Listeners are
passed the `Tab` object that triggered the event.
<api name="id">
{string}
The unique id for the tab. This property is read-only.
</api>
<api name="title">
{string}
The title of the tab (usually the title of the page currently loaded in the tab)
This property can be set to change the tab title.
</api>
<api name="url">
{String}
The URL of the page currently loaded in the tab.
This property can be set to load a different URL in the tab.
</api>
<api name="favicon">
{string}
The URL of the favicon for the page currently loaded in the tab.
This property is read-only.
<div class="warning">
This property is deprecated.
From version 1.15, use the <a href="modules/sdk/places/favicon.html#getFavicon()">favicon module's <code>getFavicon()</code></a> function instead.
</div>
</api>
<api name="contentType">
{string}
<div class="experimental">
<strong>
This is currently an experimental API, so we might change it in future releases.
</strong>
<p>
Returns the MIME type that the document currently loaded in the tab is being
rendered as.
This may come from HTTP headers or other sources of MIME information,
and might be affected by automatic type conversions performed by either the
browser or extensions.
This property is read-only.
</p>
</div>
</api>
<api name="index">
{integer}
The index of the tab relative to other tabs in the application window.
This property can be set to change its relative position.
</api>
<api name="isPinned">
{boolean}
Whether or not tab is pinned as an [app tab][].
This property is read-only.
[app tab]:http://support.mozilla.com/en-US/kb/what-are-app-tabs
</api>
<api name="pin">
Pins this tab as an [app tab][].
[app tab]:http://support.mozilla.com/en-US/kb/what-are-app-tabs
</api>
<api name="unpin">
Unpins this tab.
</api>
<api name="close">
Closes this tab.
[callback] {function}
A function to be called when the tab finishes its closing process.
This is an optional argument.
</api>
<api name="reload">
Reloads this tab.
</api>
<api name="activate">
Makes this tab active, which will bring this tab to the foreground.
</api>
<api name="getThumbnail">
Returns thumbnail data URI of the page currently loaded in this tab.
</api>
<api name="attach">
Create a page mod and attach it to the document in the tab.
**Example**
var tabs = require("sdk/tabs");
tabs.on('ready', function(tab) {
tab.attach({
contentScript:
'document.body.style.border = "5px solid red";'
});
});
options {object}
Options for the page mod, with the following keys:
[contentScriptFile] {string,array}
The local file URLs of content scripts to load. Content scripts specified
by this option are loaded *before* those specified by the `contentScript`
option. Optional.
[contentScript] {string,array}
The texts of content scripts to load. Content scripts specified by this
option are loaded *after* those specified by the `contentScriptFile` option.
Optional.
[onMessage] {function}
A function called when the page mod receives a message from content scripts.
Listeners are passed a single argument, the message posted from the
content script. Optional.
{Worker}
The [Worker](modules/sdk/content/worker.html#Worker) object can be used to
communicate with the content script.
See [Content Scripts guide](dev-guide/guides/content-scripts/index.html)
to learn the details.
</api>
<api name="close">
This event is emitted when the tab is closed. It's also emitted when the
tab's window is closed.
{Tab}
Listeners are passed the tab object.
</api>
<api name="ready">
This event is emitted when the DOM for the tab's content is ready. It is
equivalent to the `DOMContentLoaded` event for the given content page.
A single tab will emit this event every time the DOM is loaded: so it will be
emitted again if the tab's location changes or the content is reloaded.
After this event has been emitted, all properties relating to the tab's
content can be used.
{Tab}
Listeners are passed the tab object.
</api>
<api name="load">
This event is emitted when the page for the tab's content is loaded. It is
equivalent to the `load` event for the given content page.
A single tab will emit this event every time the page is loaded: so it will be
emitted again if the tab's location changes or the content is reloaded.
After this event has been emitted, all properties relating to the tab's
content can be used.
This is fired after the `ready` event on DOM content pages and can be used
for pages that do not have a `DOMContentLoaded` event, like images.
{Tab}
Listeners are passed the tab object.
</api>
<api name="pageshow">
This event is emitted when the page for the tab's content is potentially
from the cache. It is equivilent to the [pageshow](https://developer.mozilla.org/en-US/docs/DOM/Mozilla_event_reference/pageshow) event for the given
content page.
After this event has been emitted, all properties relating to the tab's
content can be used.
While the `ready` and `load` events will not be fired when a user uses the back
or forward buttons to navigate history, the `pageshow` event will be fired.
If the `persisted` argument is true, then the contents were loaded from the
bfcache.
{Tab}
Listeners are passed the tab object.
{persisted}
Listeners are passed a boolean value indicating whether or not the page
was loaded from the [bfcache](https://developer.mozilla.org/en-US/docs/Working_with_BFCache) or not.
</api>
<api name="activate">
This event is emitted when the tab is made active.
{Tab}
Listeners are passed the tab object.
</api>
<api name="deactivate">
This event is emitted when the tab is made inactive.
{Tab}
Listeners are passed the tab object.
</api>
</api>
<api name="open">
This event is emitted when a new tab is opened. This does not mean that
the content has loaded, only that the browser tab itself is fully visible
to the user.
Properties relating to the tab's content (for example: `title`, `favicon`,
and `url`) will not be correct at this point. If you need to access these
properties, listen for the `ready` event:
var tabs = require("sdk/tabs");
tabs.on('open', function(tab){
tab.on('ready', function(tab){
console.log(tab.url);
});
});
{Tab}
Listeners are passed the tab object that just opened.
</api>
<api name="close">
This event is emitted when a tab is closed. When a window is closed
this event will be emitted for each of the open tabs in that window.
{Tab}
Listeners are passed the tab object that has closed.
</api>
<api name="ready">
This event is emitted when the DOM for a tab's content is ready.
It is equivalent to the `DOMContentLoaded` event for the given content page.
A single tab will emit this event every time the DOM is loaded: so it will be
emitted again if the tab's location changes or the content is reloaded.
After this event has been emitted, all properties relating to the tab's
content can be used.
{Tab}
Listeners are passed the tab object that has loaded.
</api>
<api name="activate">
This event is emitted when an inactive tab is made active.
{Tab}
Listeners are passed the tab object that has become active.
</api>
<api name="deactivate">
This event is emitted when the active tab is made inactive.
{Tab}
Listeners are passed the tab object that has become inactive.
</api>