How to open an add-on's preference panel?

<<

d4k0

Posts: 8

Joined: Sun Apr 21, 2013 4:52 am

Post Wed Mar 12, 2014 5:51 pm

How to open an add-on's preference panel?

Hi,

in my add-on which I'm currently developing with the Add-on SDK I'm using a panel (which is attached to the widget) to "simulate" a menu with buttons (like "start" etc.) using HTML and CSS. Now I would like to add a button which opens the add-on's preference panel in a new tab. Unfortunately, I couldn't find anything about this. Some people also had this question, but they either got no response (like here) or were using XUL for the add-on development.

So, is there a (easy) way to open an add-on's preference panel in a new tab (like using the tabs module for that)? Or isn't this possible? Maybe someone can answer my question :).
<<

Noitidart

Posts: 329

Joined: Sun Nov 28, 2010 4:30 am

Post Wed Mar 12, 2014 9:02 pm

Re: How to open an add-on's preference panel?

  Code:
Services.wm.getMostRecentWindow('navigator:browser').BrowserOpenAddonsMgr();


This code will look for addon manager in all open firefox windows, if found it will focus it, if not found it will open it in the current browser window.
If you want to open the addon manager on a specific page, supply the argument aView, see mxr code below. If no aView supplied it doesnt change the page loaded in the addon manager.
But this example loads the services panel:
  Code:
Services.wm.getMostRecentWindow('navigator:browser').BrowserOpenAddonsMgr('addons://list/service');


at mxr: http://mxr.mozilla.org/mozilla-release/ ... er.js#6071
  Code:
6071 function BrowserOpenAddonsMgr(aView) {
6072   if (aView) {
6073     let emWindow;
6074     let browserWindow;
6075
6076     var receivePong = function receivePong(aSubject, aTopic, aData) {
6077       let browserWin = aSubject.QueryInterface(Ci.nsIInterfaceRequestor)
6078                                .getInterface(Ci.nsIWebNavigation)
6079                                .QueryInterface(Ci.nsIDocShellTreeItem)
6080                                .rootTreeItem
6081                                .QueryInterface(Ci.nsIInterfaceRequestor)
6082                                .getInterface(Ci.nsIDOMWindow);
6083       if (!emWindow || browserWin == window /* favor the current window */) {
6084         emWindow = aSubject;
6085         browserWindow = browserWin;
6086       }
6087     }
6088     Services.obs.addObserver(receivePong, "EM-pong", false);
6089     Services.obs.notifyObservers(null, "EM-ping", "");
6090     Services.obs.removeObserver(receivePong, "EM-pong");
6091
6092     if (emWindow) {
6093       emWindow.loadView(aView);
6094       browserWindow.gBrowser.selectedTab =
6095         browserWindow.gBrowser._getTabForContentWindow(emWindow);
6096       emWindow.focus();
6097       return;
6098     }
6099   }
6100
6101   var newLoad = !switchToTabHavingURI("about:addons", true);
6102
6103   if (aView) {
6104     // This must be a new load, else the ping/pong would have
6105     // found the window above.
6106     Services.obs.addObserver(function observer(aSubject, aTopic, aData) {
6107       Services.obs.removeObserver(observer, aTopic);
6108       aSubject.loadView(aView);
6109     }, "EM-loaded", false);
6110   }
6111 }
<<

Noitidart

Posts: 329

Joined: Sun Nov 28, 2010 4:30 am

Post Wed Mar 12, 2014 9:12 pm

Re: How to open an add-on's preference panel?

Two more notes about the code above.

If you want to open the addon manager to your addons preferences view use your addon view url. To figure out your view url, go to addon manager, open the page in addon manager you want, (ie: your addon page) then go to scratchpad then paste this code:
  Code:
alert(gViewController.currentViewId);

it will alert you the view id, set that to aView and it should open your view

ie to open addblock plus you would use this code:
  Code:
Services.wm.getMostRecentWindow('navigator:browser').BrowserOpenAddonsMgr('addons://detail/%7Bd10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d%7D/preferences')






If you don't want to open the addon manger, or focus it if its open (as the BrowserOpenAddonManager function does), but just want to search for it, to see if its open, and get its window objects see this function here: https://gist.github.com/Noitidart/9521720
<<

Noitidart

Posts: 329

Joined: Sun Nov 28, 2010 4:30 am

Post Wed Mar 12, 2014 9:15 pm

Re: How to open an add-on's preference panel?

you must import Services module to use stuff above, in addon sdk code is something like, im not sure i dont do sdk
  Code:
var {Cu} = require('chrome');
Cu.import('resource://gre/modules/Services.jsm');
<<

d4k0

Posts: 8

Joined: Sun Apr 21, 2013 4:52 am

Post Tue Mar 18, 2014 11:54 am

Re: How to open an add-on's preference panel?

Noitidart, many thanks for your answers and sorry for my late reply (I've been busy this week). This is how I am using your code (maybe this helps someone):

  Code:
const {Cu} = require('chrome');
const {Services} = Cu.import('resource://gre/modules/Services.jsm');

// Open the add-on's preference panel (the url has to be replaced by your own add-on view url):
Services.wm.getMostRecentWindow('navigator:browser').BrowserOpenAddonsMgr('addons://detail/%7Bd10d0bf8-f5b5-c8b4-a8b2-2b9879e08c5d%7D/preferences');


This works like a charm in an Add-on SDK environment.

BTW: Great that you also answered the guy on Stackoverflow ;).
<<

Noitidart

Posts: 329

Joined: Sun Nov 28, 2010 4:30 am

Post Tue Mar 18, 2014 10:19 pm

Re: How to open an add-on's preference panel?

Cool man :)

Return to Extension Development

Who is online

Users browsing this forum: No registered users

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
CA Gen2 style designed by Vjacheslav Trushkin.