Search code examples
javascriptfirefoxsandboxfirefox-addon-sdkrequire

Firefox addonsdk sandboxing and require API


I have a firefox addon-sdk. I created a test add-on similar to https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Getting_started

Now in my main.js I do something like this

const { XMLHttpRequest } = require('sdk/net/xhr');

var fooLibrary = require("foo"); 

Where foo is a module that provides some functionality using XMLHttpRequest

The problem is now when I try to run some function inside fooLibrary I get an error that says "XMLHttpRequest is not defined"

I have learnt that when I load a module inside my add-on using the "require" API it gets loaded in a sandbox that is created for my module.

My question is how can I set the XMLHttpRequest API in a global scope so that my foo library can simply call

 var xhr = new XMLHttpRequest() ? 

Any help on this subject would be extremely helpful.

Thank you!


Solution

  • There is no global scope. You need to require XMLHttpRequest in foo.js.