isn't quite ashamed enough to present

jr conlin's ink stained banana

2008-08-26

::Opening The Window

Continuing on lessons learned from building a Mozilla Plugin, let me continue with what you probably really want to do anyway, get to the damn window content.

Sure, having a little window that says "Hello World" may be nice and all, but it's not going to do anything really cool. No, for that, it'd be damn handy to get the contents of what's in the current browser. Except, which current browser do you mean?

For that matter, which window do you mean? You can have a browser anywhere, including in that little "Hello World" micro window, each with their own context. Well, turns out that (surprise) there are a number of ways to get the window you want.

First off, the FUEL way, since it's far cleaner than the other way.
var activeWindowDocument = Application.activeWindow.activeTab.document;
Like i said, far cleaner. The astute will note that there's a few telling elements to the above clean statement. The first being that while there's a Globally available Application, it has more than one .window beneath it. Good for you. The .windows property contains all of the browser windows available under Application, including any that might be squirreled away in an extension. (Ok, Application is globally defined for XUL apps, which include the default Firefox implementation and any extension you may be writing, but not any XPCOM modules you may be creating. If it's not available for whatever reason, you'll have to instantiate it from XPCOM via:
var Application = Components.classes["@mozilla.org/fuel/application;1"].getService(Components.interfaces.fuelIApplication);

It's also possible to walk the various tabs the user may have opened (or you've added) to get that content, and obviously, once you have activeWindowDocument, you've got full access to the DOM of whatever happens to be in that window. If you add a listener to the activeWindow:
Application.activeWindow.addEventListener('DOMContentLoaded',this.onPageRefresh);
onPageRefresh will get called after the DOM has finalized anytime a page is loaded, and since XUL applications run in protected space, you can make as many external calls back to your server as you like without asking for user permissions.

(Starting to feel paranoid about what extensions you load? Good. You should.)

The less clean manner is to take advantage of one of the older XUL globals and call getBrowser().contentWindow.document;. This instantiates and returns the global gBrowser which you can later refer to. Why is this less clean? Well for one, it's not really well documented, and considering that the Working with Windows in Chrome section offers the less than helpful way of getting the Application window:
var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindow);

You'd be looking for shortcuts too.

A few additional notes:
The XML pretty-printer is exceptionally fickle. If you modify the content of the page in any manner, it will revert the page to plain text. It does this because it hates you.

To be honest, i'm using a combination of these approaches because i'm under the gun and learning. Eventually, i plan on cleaning things up to the point where i'm only doing things the right way, but that's not happening this week.

Hey, delicious user, Save This Page
Blogs of note
personal that's my blog
(The Official Blog of the Internet)
memoirs of hydrogen guy matthew shepherd (quebec) rhapsodic.org j$ (right) Henriette's Herbal Blog fanatical apathy lynne ydw i iconophobia slumbering lungfish
geek Y!Cool Thing michael j radwin jeremy z
(The Official Website of the Internet)
dave's picks ultramookie Josh Woodward derek balling j$ (left) simon willison Yahoo! Search Blog
news ars technica search engine watch webmaster world.com
forums uh.net man-man killroy & tina

experimental

Firefox search plugins for Yahoo!

My Living Room media box config

The Official "Official" Registry of the Internet

Powered by WordPress
Hosted on Dreamhost.