1
votes

I am creating a firefox extension using the Add-on SDK. For various reasons (largely the paucity of features in the simple-prefs module) I've had to create a XUL window and include it using window/utils. This window displays correctly and works exactly how it should.

My problem is that I need to call a function in my main.js from a script file included in my XUL window. They don't exist in the same namespace, so I'm not sure how to access my function. I'm happy to use message passing, but I don't think that the port functionality is available to scripts included in XPCOM.

I'm thinking I need to set up some kind of callback, but I really have no idea how I might go about that. I looked into creating my own XPCOM component, but it seems I am not allowed to register XPCOM components with the Add-on SDK, according to the XUL Migration Guide

For reference, here is my (abbreviated) folder structure:

- package.json
- chrome.manifest (to allow me to open a XUL window)
- lib
   - main.js
- chrome
   - content
      - window.xul
      - script.js

I want some code in main.js to be called by some code in script.js. Note that the window is being created in main.js (like so)

var { open } = require('sdk/window/utils');
var window = open('chrome://my-app-id/content/window.xul', { features: {
    chrome: true,
    centerscreen: true,
    toolbar: true
}});

and that this much works. Does anyone have any ideas?

Edit: I can get around this by watching a preference change in main.js and then causing it to change when I want the function to be invoked, but that strikes me as a huge abuse of the preferences system. I'd like to do it the right way, if there is one.

1

1 Answers

1
votes

I solved my own problem in a way I am happy with.

I changed the above invocation of window.open to window.openDialog and passed in my callback function as an argument, like so:

var { openDialog } = require('sdk/window/utils');
var window = openDialog({url: 'chrome://my-app-id/content/window.xul',
    features: {
        chrome: true,
        centerscreen: true,
        toolbar: true
    },
    args: {
        doStuff: function(){
            //Do all the stuff
        }
    }
});

Then in script.js, when I want the callback to be invoked, I use

window.arguments[0].doStuff();