olsh
olsh

Reputation: 463

Open custom page on the settings click, firefox addon SDK

I'm using Firefox addon-SDK to create my addon. I use simple-prefs for the options, but how can I open custom page when user clicks on the "Settings" button in the Firefox addon manager?
Thank you for any help!

Upvotes: 2

Views: 699

Answers (2)

bgmCoder
bgmCoder

Reputation: 6371

How to Open Custom Page Directoly from Addons Manager

Actually, you CAN do exactly what you want. This will open YOUR addon's prefs page when you click on the Options Button right in the Addon Manager - it replaces the action of opening to the addon's about page.

Credit for this solution goes to Noitidart.

This is a no-javascript solution, believe it or not. The docs are here. You have to do two things in your install.rdf:

  1. Set <em:optionsURL>resource://myAddonID/data/my.html</em:optionsURL>
  2. Set <em:optionsType>3</em:optionsType>

Noitidart actually created a sample addon to illustrate this. The code is all blank except for those two lines in the install.rdf which does all the work.

Firefox does all the tab-handling for you.

Incidentally, there are three options for <em:optionsType>

  • 1 Opens optionsURL in a dialog box
  • 2 Options are displayed inside the Add-on Manager
  • 3 Opens optionsURL in a new tab (if the application supports that), or a dialog box

How to Open Custom Page from Addons Manager Addon About Page

Also, in the docs for simple-prefs, toward the bottom there is control that you put as a preference in package.json just like all the other prefs (all this info is directly from that page):

When the user clicks the button, the function listening to the on() function for this preference is called.

This type requires an mandatory attribute called "label" which is provided as a string. It is used to label the button.

So, your code (as a prefs along with the rest of them within the preferences object in your json file) to make that button appear in the addon options area would be like this:

{
    "type": "control",
    "label": "Click me!",
    "name": "sayHello",
    "title": "Say Hello"
}

And in your main.js, do this to get it to open a custom page (which would be in your data directory; or you could use a regular web page address here):

var data = require("sdk/self").data;
var sp = require("sdk/simple-prefs");
sp.on("sayHello", function() {
  tabs.open( data.url("myCustomPage.html") );  //<======The KEY
});

Remember that "name" is the name of the "message" and needs to be the same in both the package.json file AND in the main.js file.

Try it! It's very easy and it works wonderfully!


Upvotes: 0

ZER0
ZER0

Reputation: 25322

As far as I know, you can't. If you have some preferences that cannot be expressed by the simple-prefs system, you could add a button in the preferences of your add-on that can open a custom page in a new tab, or window. See control type: https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/simple-prefs.html

My suggestion, is stick to the simple-prefs when is possible, because they're synchronized across computers and devices using Firefox Sync.

Upvotes: 1

Related Questions