Tuesday, June 5, 2007

Accessing page content from an extension

Firefox version: 2.0.0.4 (Mac)

First roadblock
If JavaScript is embedded in a page, then the document or window.document variables point to the DOM model of the page. When writing an extension, window.document points to the DOM model of the Firefox GUI. How, then, does one get access to the contents of the page from an extension?

Solutions
Mira and Raphael provided two solutions:


window.content.document


or a more complicated option:


function getBrowserWindow() {
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
getService(Components.interfaces.nsIWindowMediator );
return wm.getMostRecentWindow("navigator:browser").content.document;
}



I have tested both of them and they both return the pointer to the document in the currently visible window tab or window. Are the two methods really equivalent or have I missed something?

0 Comments:

Post a Comment

<< Home