I decided to give Mozilla's Add-on Builder a try. My directory structure looks something like this:
The problem is that the file popup.html
needs to reference stackapi.js
. But I have absolutely no clue how to do that. Looking through the Mozilla docs, there seems to be a way to do the opposite:
var data = require("self").data;
var url_of_popup = data.url("popup.html");
This allows scripts in Lib to access data files in Data. But I need to do the opposite.
In add-ons built with the Add-on SDK (and the Builder is merely a web interface for the SDK) web pages cannot access extension modules directly - they don't have the necessary privileges. If you simply need to include a JavaScript file from the web page then you should put that file into the
data
directory. However, it won't have any special privileges then (like being able to callrequire()
).You don't tell how you are using
popup.html
but I guess that it is a panel. If you want that page to communicate with your add-on you need to use content scripts. Put a content script file into thedata
directory, assign it to your panel viacontentScriptFile
parameter. See documentation on content scripts, the content script will be able to useself.postMessage()
to send messages to the extension, the extension can perform the necessary operations and send a message back then.Had to go through the same scenario, but solution by jongo45 does not seem to work anymore. Somehow found a solution which worked for me. Posting below as it might help someone in need.
Below code obtains list of all files under "lib/subdir".
You can get the url of the
stackapi.js
file by navigating up from the/data
folder and back down the/lib
folder like so:Then use that resource url in the
contentScriptFile
parameter when attaching scripts to what I assume is going to bepopup.html
.You'll need to check which environment you're currently in to determine if you need to add any references to the
exports
object to make them accessible from within the addon.