views:

108

answers:

3

The note at the bottom of this Mozilla wiki page currently says: "Using canvas.drawWindow() while handling a document's onload event doesn't work. In Firefox 3.5 or later, you can do this in a handler for the MozAfterPaint event to successfully draw HTML content into a canvas on page load." Which is fine, except that I tried it in Firefox 3.6.6 and it did work, leading me to believe that perhaps it used to not work, due to some bug which has since been fixed. I'd rather not use MozAfterPaint since it won't work in versions earlier than 3.5. Is there an important reason not to use the "load" event, and if so what can I do instead that will be compatible with older versions of Firefox?

EDIT: This is how my code works. In the init() function of my extension, I call gBrowser.addEventListener("load", MyExtension.onPageLoad, true); Then MyExtension.onPageLoad is essentially:

onPageLoad : function(e) {
  var win = e.originalTarget.defaultView;
  // create an html:canvas, adjust its size, etc. following the example of the "TabPreview" extension
  var ctx = canvas.getContext("2d");
  ctx.drawWindow(win, 0, 0, w, h, "rgb(255, 255, 255");
  // add the canvas to the DOM
},
+1  A: 

@MatrixFrog, I'm struggling to solve the same problem, could you post your complete code if not proprietary.

Don Don
Unfortunately, it is proprietary. You could try posting *your* code (if it's not proprietary, of course) as a new question (since "Why isn't this working?" is more answerable than the present question, which is more like, "This IS working, but I'm not sure if it's "the right way" or not.") and someone can probably help you. If you do that, please post a link here.
MatrixFrog
A: 

Looks like the Mozilla documentation is wrong, and it is okay to call canvas.drawWindow() from the "load" event.

MatrixFrog
@Matrix: is that official? or from what do you draw this conclusion? (so others can know how much they can rely on this answer)
LarsH
Basically, I figure that if there were some reason not to do this, then someone from the SO community would have mentioned it by now. And if others believe this answer to be the correct one, then they can upvote it. I should have made that more clear.
MatrixFrog
+1  A: 

I expect it's not a case of won't work "at all", but rather a case of won't work "correctly".

Traditionally, onload ran when the html of the page was finished loading. The CSS and scripting would happen later, or possibly at the same time 1.

This is why the MozAfterPaint was introduced. It lets you inject code after Gecko has enough information to render the page.

You might be able work around the absence of MozAfterPaint, by listening for DOM mutation events. It's not as clean, but I think it will work if you use it to clear and reset a 100-200 ms tineout. That shouldn't cause too much of a performance hit. When the timeout finally expires you know the page has been stable for at least that long.

[1] I spent a week chasing a global variable that went from undefined to defined during the execution of the onload handler. It turned out to be a script tag pulled in a JS file that had some top-level code and it was executing in parallel with the onload handler.

Devon_C_Miller