20 KiB
This module exports a single constructor function Panel which constructs a
new panel.
A panel is a dialog. Its content is specified as HTML and you can execute scripts in it, so the appearance and behaviour of the panel is limited only by what you can do using HTML, CSS and JavaScript.
The screenshot below shows a panel whose content is built from the list of currently open tabs:
Panels are useful for presenting temporary interfaces to users in a way that is easier for users to ignore and dismiss than a modal dialog, since panels are hidden the moment users interact with parts of the application interface outside them.
A panel's content is loaded as soon as it is created, before the panel is shown, and the content remains loaded when a panel is hidden, so it is possible to keep a panel around in the background, updating its content as appropriate in preparation for the next time it is shown.
Your add-on can receive notifications when a panel is shown or hidden by
listening to its show and hide events.
Panel Content
The panel's content is specified as HTML, which is loaded from the URL
supplied in the contentURL option to the panel's constructor.
You can load remote HTML into the panel:
var panel = require("sdk/panel").Panel({
width: 180,
height: 180,
contentURL: "https://en.wikipedia.org/w/index.php?title=Jetpack&useformat=mobile"
});
panel.show();
You can also load HTML that's been packaged with your add-on, and this is
most probably how you will create dialogs. To do this, save
the HTML in your add-on's data directory and load it using the data.url()
method exported by the
self module, like this:
var panel = require("sdk/panel").Panel({
contentURL: require("sdk/self").data.url("myFile.html")
});
panel.show();
Updating Panel Content
You can update the panel's content simply by setting the panel's contentURL
property.
Here's an add-on that adds two widgets to the add-on bar, one which
shows Google's mobile site and one which shows Bing's mobile site. The widgets
share a panel object, and switch between the two sites by updating the panel's
contentURL property:
var panel = require("sdk/panel").Panel({
contentURL: "about:blank",
onHide: function () {
panel.contentURL = "about:blank";
}
});
require("sdk/widget").Widget({
id: "bing",
label: "Bing",
contentURL: "http://www.bing.com/favicon.ico",
panel: panel,
onClick: function() {
panel.contentURL = "http://m.bing.com/";
}
});
require("sdk/widget").Widget({
id: "google",
label: "Google",
contentURL: "http://www.google.com/favicon.ico",
panel: panel,
onClick: function() {
panel.contentURL = "http://www.google.com/xhtml";
}
});
Scripting Panel Content
You can't directly access your panel's content from your main add-on code. To access the panel's content, you need to load a script into the panel. In the SDK these scripts are called "content scripts" because they're explicitly used for interacting with web content.
While content scripts can access the content they're attached to, they can't use the SDK's APIs. So implementing a complete solution usually means you have to send messages between the content script and the main add-on code.
-
You can specify one or more content scripts to load into a panel using the
contentScriptorcontentScriptFileoptions to thePanel()constructor. -
You can communicate with the script using either the
postMessage()API or (preferably, usually) theportAPI.
For example, here's an add-on whose content script intercepts mouse clicks
on links inside the panel, and sends the target URL to the main add-on
code. The content script sends messages using self.port.emit() and the
add-on script receives them using panel.port.on().
var myScript = "window.addEventListener('click', function(event) {" +
" var t = event.target;" +
" if (t.nodeName == 'A')" +
" self.port.emit('click-link', t.toString());" +
"}, false);"
var panel = require("sdk/panel").Panel({
contentURL: "http://www.bbc.co.uk/mobile/index.html",
contentScript: myScript
});
panel.port.on("click-link", function(url) {
console.log(url);
});
panel.show();
This example uses contentScript to supply the script as a string. It's
usually better practice to use contentScriptFile, which is a URL pointing
to a script file saved under your add-on's data directory.
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.
Getting User Input
The following add-on adds a widget which displays a panel when
clicked. The panel just contains a <textarea> element: when the user
presses the return key, the contents of the <textarea> is sent to the
main add-on code.
The add-on consists of three files:
main.js: the main add-on code, that creates the widget and panelget-text.js: the content script that interacts with the panel contenttext-entry.html: the panel content itself, specified as HTML
"main.js" is saved in your add-on's lib directory, and the other two files
go in your add-on's data directory:
my-addon/
data/
get-text.js
text-entry.html
lib/
main.js
The "main.js" looks like this:
var data = require("sdk/self").data;
// Create a panel whose content is defined in "text-entry.html".
// Attach a content script called "get-text.js".
var text_entry = require("sdk/panel").Panel({
width: 212,
height: 200,
contentURL: data.url("text-entry.html"),
contentScriptFile: data.url("get-text.js")
});
// Send the content script a message called "show" when
// the panel is shown.
text_entry.on("show", function() {
text_entry.port.emit("show");
});
// Listen for messages called "text-entered" coming from
// the content script. The message payload is the text the user
// entered.
// In this implementation we'll just log the text to the console.
text_entry.port.on("text-entered", function (text) {
console.log(text);
text_entry.hide();
});
// Create a widget, and attach the panel to it, so the panel is
// shown when the user clicks the widget.
require("sdk/widget").Widget({
label: "Text entry",
id: "text-entry",
contentURL: "http://www.mozilla.org/favicon.ico",
panel: text_entry
});
The content script "get-text.js" looks like this:
self.port.on("show", function (arg) {
var textArea = document.getElementById('edit-box');
textArea.focus();
// When the user hits return, send a message to main.js.
// The message payload is the contents of the edit box.
textArea.onkeyup = function(event) {
if (event.keyCode == 13) {
// Remove the newline.
text = textArea.value.replace(/(\r\n|\n|\r)/gm,"");
self.port.emit("text-entered", text);
textArea.value = '';
}
};
});
Finally, the "text-entry.html" file defines the <textarea> element:
<html>
<head>
<style type="text/css" media="all">
textarea {
margin: 10px;
}
</style>
</head>
<body>
<textarea rows="10" cols="20" id="edit-box"></textarea>
</body>
</html>
To learn much more about content scripts, see the Working with Content Scripts guide.
Scripting Trusted Panel Content
We've already seen that you can package HTML files in your add-on's data
directory and use them to define the panel's content. 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.
To show the difference, we can easily convert the text-entry add-on above
to use normal page scripts instead of content scripts.
The main add-on code is exactly the same as the main add-on code in the previous example, except that we don't attach a content script:
var data = require("sdk/self").data;
// Create a panel whose content is defined in "text-entry.html".
var text_entry = require("sdk/panel").Panel({
width: 212,
height: 200,
contentURL: data.url("text-entry.html"),
});
// Send the page script a message called "show" when
// the panel is shown.
text_entry.on("show", function() {
text_entry.port.emit("show");
});
// Listen for messages called "text-entered" coming from
// the page script. The message payload is the text the user
// entered.
// In this implementation we'll just log the text to the console.
text_entry.port.on("text-entered", function (text) {
console.log(text);
text_entry.hide();
});
// Create a widget, and attach the panel to it, so the panel is
// shown when the user clicks the widget.
require("sdk/widget").Widget({
label: "Text entry",
id: "text-entry",
contentURL: "http://www.mozilla.org/favicon.ico",
panel: text_entry
});
The page script is exactly the same as the content script above, except
that instead of self, we use addon to access the messaging APIs:
addon.port.on("show", function (arg) {
var textArea = document.getElementById('edit-box');
textArea.focus();
// When the user hits return, send a message to main.js.
// The message payload is the contents of the edit box.
textArea.onkeyup = function(event) {
if (event.keyCode == 13) {
// Remove the newline.
text = textArea.value.replace(/(\r\n|\n|\r)/gm,"");
addon.port.emit("text-entered", text);
textArea.value = '';
}
};
});
Finally, the HTML file now references "get-text.js" inside a script tag:
<html>
<head>
<style type="text/css" media="all">
textarea {
margin: 10px;
}
</style>
<script src="get-text.js"></script>
</head>
<body>
<textarea rows="10" cols="20" id="edit-box"></textarea>
</body>
</html>
Styling Trusted Panel Content
When the panel's content is specified using an HTML file in your data
directory, you can style it using CSS, either embedding the CSS directly
in the file or referencing a CSS file stored under data.
The panel's default style is different for each operating system:
This helps to ensure that the panel's style is consistent with the dialogs
displayed by Firefox and other applications, but means you need to take care
when applying your own styles. For example, if you set the panel's
background-color property to white and do not set the color property,
then the panel's text will be invisible on OS X although it looks fine on Ubuntu.
Once a panel object has been created it can be shown and hidden using its
show() and hide() methods. Once a panel is no longer needed it can be
deactivated using destroy().
The content of a panel is specified using the contentURL option. An add-on
can interact with the content of a panel using content scripts which it
supplies in the contentScript and/or contentScriptFile options. For example,
a content script could create a menu and send the user's selection to the
add-on.
* "start": load content scripts immediately after the document
element for the panel 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](https://developer.mozilla.org/en/Gecko-Specific_DOM_Events)
event
* "end": load content scripts once all the content (DOM, JS, CSS,
images) for the panel has been loaded, at the time the
[window.onload event](https://developer.mozilla.org/en/DOM/window.onload)
fires
This property is optional and defaults to "end".
@prop [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.
@prop [onMessage] {function}
Include this to listen to the panel's message event.
@prop [onShow] {function}
Include this to listen to the panel's show event.
@prop [onHide] {function}
Include this to listen to the panel's hide event.
- send events to the content script using the
port.emitfunction - receive events from the content script using the
port.onfunction
See the guide to
communicating using port for details.
- "start": load content scripts immediately after the document element for the panel 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 panel has been loaded, at the time the window.onload event fires
Any kind of jsonable value (object, array, string, etc.) can be used here. Optional.
@method Destroys the panel, unloading any content that was loaded in it. Once destroyed, the panel can no longer be used. If you just want to hide the panel and might show it later, use `hide` instead. @method Sends a message to the content scripts. @param message {value} The message to send. Must be stringifiable to JSON. @method Displays the panel. @method Stops displaying the panel. @method Resizes the panel. @param width {number} The new width of the panel in pixels. @param height {number} The new height of the panel in pixels. @method Registers an event listener with the panel. @param type {string} The type of event to listen for. @param listener {function} The listener function that handles the event. @method Unregisters an event listener from the panel. @param type {string} The type of event for which `listener` was registered. @param listener {function} The listener function that was registered. @event This event is emitted when the panel is shown. @event This event is emitted when the panel is hidden. @event If you listen to this event you can receive message events from content scripts associated with this panel. When a content script posts a message using `self.postMessage()`, the message is delivered to the add-on code in the panel's `message` event.@argument {value} Listeners are passed a single argument which is the message posted from the content script. The message can be any JSON-serializable value.
@event This event is emitted when an uncaught runtime error occurs in one of the panel's content scripts.@argument {Error} Listeners are passed a single argument, the Error object.



