How to open new tab, and create a new HTML document in it? Preferably using old restart-required API, like Components.classes
, Components.interfaces
stuff, but any way that works is fine.
Asked
Active
Viewed 177 times
1
-
What is going to work *for you* will depend on the type of add-on you are writing. What you have stated implies that you are writing a Overlay/XUL add-on. Is that correct? If so, you should **seriously** consider writing a WebExtension, or Add-on SDK based add-on. See the [Introduction to Firefox add-ons in documentation](http://stackoverflow.com/documentation/firefox-addon/3235/introduction-to-firefox-add-ons/13574/introduction#t=201609290133319078047). – Makyen Nov 22 '16 at 10:01
-
Yes, I have Overlay/XUL addons. I considered moving them, but didn't had enough time to do so. Things I need to know in order to migrate are (1) Is it possible to set global variables / access extensions state via browser console in new extensions? and (2) Is there still an alternative to nsIStreamListener/TracingListener in the newer api? – Vibok Nov 22 '16 at 10:43
-
In the Add-on SDK you can do almost anything in JavaScript which you could do in Overlay or bootstrap add-on (Obviously not *chrome.manifest* `overlay`s). The add-on SDK is a bootstrapped add-on with a wrapper. You can break out of the wrapper when you have things you want to do which are not directly supported by the Add-on SDK APIs. – Makyen Nov 22 '16 at 10:46
1 Answers
0
In one of my add-ons, I use the following code to open a URL in either a tab or a window:
/**
* Open a URL in a window or a tab.
*/
function openUrlInWindowOrTab(url, titleText, inWindow, makeTabActive) {
// Default: in tab; tab not activated
if(typeof (url) !== "string" ) {
return;
}//else
// Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
/* Add-on SDK:
let activeWindow = require('sdk/window/utils').getMostRecentBrowserWindow();
//*/
//* Overlay and bootstrap (from almost any context/scope):
Components.utils.import("resource://gre/modules/Services.jsm"); //Services
let activeWindow = Services.wm.getMostRecentWindow("navigator:browser");
//*/
let gBrowser = activeWindow.gBrowser;
if(inWindow) {
// Set default title
titleText = (typeof titleText === "string") ? titleText : "Opened by [Your add-on]";
//Open a window
return activeWindow.open(url, titleText);
} else {
//Open a tab
let newTab = gBrowser.addTab(url);
if(makeTabActive) {
//Make the tab active
gBrowser.selectedTab = newTab;
}
return newTab;
}
}
The above should work in Overlay and Bootstrapped add-ons. It can also work in the Add-on SDK by uncommenting the code for the Add-on SDK and commenting out the Overlay/bootstrapped code (the line(s) that get the activeWindow
). However, for the Add-on SDK, it would be better to use the SDK specific APIs.
If you want it to say "Hello World" in the new tab, then provide an HTML file in your chrome/content
directory and use an appropriate URL for it (e.g. chrome://[as defined in your chrome.manifest]/content/helloWorld.html
), as defined for your add-on in a content
line in your chrome.manifest.

Makyen
- 31,849
- 12
- 86
- 121