George Hodgson
George Hodgson

Reputation: 471

Want add-on to restart Firefox and show new page, but instead all old tabs are displayed

I'd like my add-on to restart Firefox and upon restart have a pre-defined webpage displayed.

When I restart the browser via nsIAppStartup.quit(), all of the tabs I had been viewing will be re-opened after restart. I have tried to cope with this manually by closing all the tabs before restarting (see code below), but I find that although I can visually confirm that all of the tabs are closed, they will be present again after a restart.

I assume that some session restore mechanism is re-creating them after the restart. I have found several preferences regarding "SessionStore" (browser.sessionstore.max_tabs_undo, browser.sessionstore.resume_from_crash, etc), but toggling them does not seem to help me resolve this issue.

How should I go about ensuring that the old tabs are closed after a restart? Thank you!

// get gBrowser reference
var mainWindow = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator).getMostRecentWindow("navigator:browser");
var gBrowser = mainWindow.gBrowser;

// build a collection of tabs to close
var tabsToClose = [];
for( var i = 0; i < gBrowser.mTabContainer.childNodes.length - 1; i++ ){
    tabsToClose.push( gBrowser.mTabContainer.childNodes[i] );
}

// Open homepage in new tab
gBrowser.addTab(DEFAULT_HOMEPAGE);

// Close all tabs except for homepage
for( var i = 0; i < tabsToClose.length; i++ ){      
    gBrowser.removeTab( tabsToClose[i] );
}

// restart browser
Cc["@mozilla.org/toolkit/app-startup;1"].getService(nsIAppStartup).quit(nsIAppStartup.eRestart | nsIAppStartup.eAttemptQuit);   

Upvotes: 0

Views: 195

Answers (1)

Filipe Silva
Filipe Silva

Reputation: 21657

I changed your code a bit and came up with this solution:

var Cc = Components.classes;
var Ci = Components.interfaces;

var pref = Cc["@mozilla.org/preferences-service;1"]
    .getService(Components.interfaces.nsIPrefService);
pref.setBoolPref("browser.tabs.warnOnCloseOtherTabs",false);

var mainWindow = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator).getMostRecentWindow("navigator:browser");
var gBrowser = mainWindow.gBrowser;

var newTab = gBrowser.addTab("www.google.com");

// Easier solution than having two loops to identify the open tabs
gBrowser.removeAllTabsBut(newTab);

var nsIAppStartup = Ci.nsIAppStartup; // You were missing this part on your question
Cc["@mozilla.org/toolkit/app-startup;1"].getService(nsIAppStartup).quit(nsIAppStartup.eRestart | nsIAppStartup.eAttemptQuit);

The preference that you need to change is browser.tabs.warnOnCloseOtherTabs so you don't get a warning when closing multiple tabs. The other sessionstore prefs i didn't touch.

Hope it helps.

Upvotes: 1

Related Questions