Search code examples
firefoxfirefox-addonfirefox-addon-sdk

Firefox Extension - New Tab - How To Override Preferences?


I am new to Firefox extension, that is why I use the Add On SDK.

I want to create an extension that shows a specific site every time the user opens up a new tab. This is my code so far:

var self = require("sdk/self");
var tabs = require("sdk/tabs");

// Listen for tab openings.
tabs.on('open', function onOpen(tab) {
   getActiveTab();
});

function getActiveTab(){
tabs.on('activate', function (tab) {
  tab.url = "http://www.example.com";
});
}

This works. But before it loads the specified domain it loads the Firefox default newtab page. Now is there an API reference to access the newtab setting and change to example.com?

Thanks,

Gerd


Solution

  • It was possible to change about:newtab URL using SDK:

    require('sdk/preferences/service').set('browser.newtab.url', 'http://www.stackoverflow.com');
    

    but it becomes obsolete with FF41, as there isn't a browser.newtab.url preference any more.


    If you still plan on using it, you might also consider adding this to your code:

    var { when: unload } = require('sdk/system/unload');
    var reason;
    unload( function ( reason ) {
      require('sdk/preferences/service').set('browser.newtab.url', 'about:newtab');
    });
    

    so that the preference change gets undone after add-on unload. You can also pass one of unload reasons to the function: 'uninstall', 'disable', 'shutdown', 'upgrade', or 'downgrade', or not provide reason argument at all / leave it undefined.