In the “Simple Addon”-tutorial of the Mozilla Addon-SDK, it reads:
/data– contains resources such as icons or HTML files, as well as
any content scripts included with your add-on. You can access the
content of the data subdirectory from within your add-on’s code using
the Add-on SDK’s self module.
In my Add-On, I use a HTML-page to display some contents in a panel. Since the contents are loaded by a Content-Script, i want to display a spinner.gif-file on the HTML-Page to show the user that the contents are being loaded. After the contents are fully loaded, I hide the spinner-image and display the contents (using jQuery).
My problem now is, that i don’t have any clue how to embed the image in the HTML-page. The problem I’m facing is that I don’t know the path to the image-file.
I know you use the self-module in an AddOn-Script to access the /data-directory, but on how to do this from a plain HTML-file or a Content-Script is not mentioned in the docs.
So this is what I have in my HTML-File (there is no need for the <head>-tag):
<html>
<h2>StackExchange Statistics</h2>
<div id="loading">
<img src="spinner.gif" alt="loading...">
<p>Fetching Data, please stand by</p>
</div>
<ul id="infos" style="display: none">
</ul>
</html>
Both this HTML-page and the image (spinner.gif) are located in the root of the /data-directory, but the above code only shows the alt-text. I also tried using /data/spinner.gif as the source, no luck.
So, how do I load an image from the /data-directory?
I tried reproducing your issue. I’ve created a trivial add-on and added a script file with the following code to the
/libdirectory:Now the add-on will open a tab immediately after being installed. I created a file
/data/test.htmlwith the source code from your question and an image file/data/spinner.gif. And after the installation of the add-on a new tab opens and the image is there – beautiful. Note that the URL of the tab isresource://something-related-to-add-on-id-data/test.htmland the URL of the image isresource://something-related-to-add-on-id-data/spinner.gifwhich is why the relative URL works. A URL like/data/spinner.gifwouldn’t work, there is nodatadirectory after the add-on has been compiled.Now I guess that you are asking how you would find this URL from a content script? Fact is, the content script itself doesn’t have the necessary API for that. But it can receive messages from your add-on. See “Communicating With Content Scripts” section in the documentation. Your content script needs to declare a function
onMessageand theonAttachfunction in the add-on needs to actually send this message. Message payload can be this one URL or some more complicated data structure if you want to transfer more data.As to regular web pages (not belonging to the add-on): all they can do is “guessing” the URL. You know what URL
/data/spinner.gifis mapped to for your add-on. So you can simply use that URL in a web page. The catch: the URL might change in future SDK versions.