Question

I'm having some trouble creating a new tab from inside a bootstrapped extension in Firefox. So far I have an extension that draws a few buttons for the context menu that can open modal windows on click with windows.open() like this :

        var contextMenu = document.getElementById("contentAreaContextMenu");

        var menuItemWebsite = document.createElement("menuitem");
        menuItemWebsite.setAttribute("id", "dev-website");
        menuItemWebsite.setAttribute("label", "Go to website");

        contextMenu.appendChild(menuItemWebsite);
        menuItemWebsite.addEventListener("command", function() {
          window.open(...);
        }, false);

There are very few resources on the web on restartless extension. Can anyone provide an example of opening a new tab to a specified URL?

Was it helpful?

Solution

You need to get a window first then you can open new tab in it.

This example here gets the most recent navigator window and adds a tab to it:

const {classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu, results: Cr, manager: Cm} = Components;
Cu.import('resource://gre/modules/Services.jsm');
var aDOMWindow = Services.wm.getMostRecentWindow('navigator:browser');
aDOMWindow.BrowserOpenTab()

another way to do this is use gBrowser.loadOneTab: so:

aDOMWindow.gBrowser.loadOneTab(aDOMWindow.BROWSER_NEW_TAB_URL, {relatedToCurrent:true});

this opens the tab next to the currently focused tab. more info on it here: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Method/loadOneTab

There are these other useful functions: OpenBrowserWindow

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top