How to load image in Firefox addon sdk module?

OK so I've got this addon where I'm trying to load a bitmap from a file:/// URI and draw it to canvas.

All that goes fine until I need to get the data off the canvas using getImageData, then I run into a security exception. I go to Moz Chat and am told that because I'm loading the image from a page modded HTML File, it's a cross domain policy issue and not allowed.

The solution, they say, is to go to the main module and load the image there, copy it to a canvas, then serialize the data with getImageData and send it back to the HTML doc. One problem: Jetpack doesn't know what "Image" is because it doesn't have an HTML DOM, thus the operation seems to be rendered more or less impossible.

Why is this a cross domain policy issue in the first place? Beyond that, how do I load the image without access to the DOM?


The simpler and best example of communication between main.js module and a content script you can read is in Add-on SDK docs: look for the section under the title Communicating With Content Scripts.

Basically, this is how the main module tells the content script (the pagemod in your case) something:

worker.port.emit("getElements", tag);

and this is how it listens to whatever the content script tells him:

worker.port.on("gotElement", function(elementContent) {

On the other side, the content script listens to what the main module says to it this way:

self.port.on("getElements", ...

And finally, a case this example is missing is how the content script may emit an event to tell something to the main module:

self.port.emit("myCustomEvent", var1=someValue, var2=otherValue, ...)

But that's the idea. I also recommend you to tale a careful look at this more general explanation about how content script (pagemods, widgets, tabs, panels, etc) work because this is the mosst important concept to understand how sdk addons work.

Last, about the cross domain issue on content scripts you can read more here.

Need Your Help

Classic ASP Site Throwing Date Conversion Error After Moving Servers?

sql sql-server database datetime asp-classic

I am moving an old store from a Win2003 IIS6 server to a Win2008 IIS7 server, moved everything across including database. The front end seems to work just fine, but when I login it has to do pull i...