Set Browser Home Page using Add-on

Hi All,

I am creating Firefox Add-on with help of below Url.
https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials

I have successfully created Firefox add-on.

Now I want to achieve following functionality like,

  1. Setting Home Page of Browser using Add-on.
  2. Where can I find all the addon event(Install/Uninstall/Load) of Add-on?
    (I want to integrate Google analytic)

Could you please help me with my functionality?

Regards
Ashish Sapkale

Firefox removed the pref that allowed easy overriding of the new tab page.

I thought I could use http-on-modify-request to listen for about:newtab but apparently going to about:newtab loads https://tiles.services.mozilla.com/v3/links/view so i tried to listen for that and do redirectTo but its not working its so weird. Anyone know why? Here’s my code:

var { interfaces: Ci, utils: Cu, classes: Cc, results: Cr } = Components;
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/devtools/Console.jsm');

var observers = {
    'http-on-modify-request': {
        observe: function (aSubject, aTopic, aData) {
            console.info('http-on-modify-request: aSubject = ' + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData);
            var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
                    var requestUrl = httpChannel.URI.spec
                    console.error('requestUrl:', requestUrl);
                    if (requestUrl == 'https://tiles.services.mozilla.com/v3/links/view') {
                        console.error('yes redir now!!');
                        // httpChannel.cancel(Cr.NS_BINDING_ABORTED);
                        httpChannel.redirectTo(Services.io.newURI('data:text/html,you went to about:newtab page and i replaced it with this', null, null));
                    }
        },
        reg: function () {
            Services.obs.addObserver(observers['http-on-modify-request'], 'http-on-modify-request', false);
        },
        unreg: function () {
            Services.obs.removeObserver(observers['http-on-modify-request'], 'http-on-modify-request');
        }
    }
};

//register all observers
for (var o in observers) {
    observers[o].reg();
}

You can change the home page by changing the browser.startup.homepage preference.

Which you can do with the preferences/service module:
https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/preferences_service

Example:

require("sdk/preferences/service").set( "browser.startup.homepage", "MyCustomPage.com");

@noitidart You should checkout resource:///modules/NewTabURL.jsm

1 Like

O interesting, i thought they removed that pref. Or there were plans to no?

I think you are confusing the home page and newtab page with each other.

The newtab page pref(browser.newtab.url) has been removed, not the home page pref.

Here is the bug for it:
https://bugzilla.mozilla.org/show_bug.cgi?id=1118285

1 Like

Ooo that’s it! :smile:
Thanks man, I’ll post another topic on how to override that new tab page (with my attempted code) as I’ll be needing it soon. :slight_smile:

Thanks MartijnJ.

I have also set “require(“sdk/preferences/service”).set( “browser.newtab.url”, “http://MyCustomPage.com”);”

To set it on everypage(Tab)

If possible Could you help me with my 2nd query?

I think this is what you are looking for:
https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Listening_for_load_and_unload

Thanks a Lot MartijnJ!

Very quick reply…

Yes, This is what I am looking for. :smile: