Search code examples
javascriptfirefox-addonxulfirefox-addon-sdk

Access simple-prefs from xul window


I want to access the simple-prefs module from a XUL settings dialog. My code looks as follows.

var WINDOW = Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(Ci.nsIWindowWatcher);
var mainWindow = MEDIATOR.getMostRecentWindow(null);

var pref = require("simple-prefs");
var win = WINDOW.openWindow(mainWindow, "chrome://myextension/content/settings.xul", "aboutMyExtension", "chrome,centerscreen,modal,dependant,dialog", pref);

As shown above, i give the simple prefs reference as an argument to the xul window. In the xul page, I try the following to get the simple-prefs reference.

<script type="application/x-javascript" src="chrome://global/content/XPCNativeWrapper.js"/>
... 
XPCNativeWrapper.unwrap(window.arguments[0])

window.arguments[0] is a [xpconnect wrapped nsISupports], but the unwrap() returns a [xpconnect wrapped nsISupports] instead of actually unwrapping it.

So, how do I access the simple-prefs module from the xul dialog?


Solution

  • I'd suggest using the loader from a script tag within your XUL window.

    Like this:

    <script type='application/javascript' src='resource://gre/modules/toolkit/loader.js'></script>
    

    Then you could just require('simple-prefs') in your XUL window instead of trying to pass it over.

    See more about the loader in the sdk low-level docs: https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/toolkit/loader.html