What’s the simplest way to launch Firefox, load a 3rd party website (which I’m authorised to “automate”), and run some “privileged” APIs against that site? (e.g: nsIProgressListener, nsIWindowMediator, etc).
I’ve tried a two approaches:
-
Create a tabbed browser using XULrunner, “plumbing” all the appropriate APIs required for the 3rd party site to open new windows, follow 302 redirects, etc. Doing it this way, it’s an aweful lot of code, and requires (afaict) that the user installs the app, or runs Firefox with -app. It’s also extremely fragile. :-/
-
Launch Firefox passing URL of the 3rd party site, with MozRepl already listening. Then shortly after startup, telnet from the “launch” script to MozRepl, use mozIJSSubScriptLoader::loadSubScript to load my code, then execute my code from MozRepl in the context of the 3rd party site — this is the way I’m currently doing it.
With the first approach, I’m getting lots of security issues (obviously) to work around, and it seems like I’m writing 10x more browser “plumbing” code then automation code.
With the second approach, I’m seeing lots of “timing issues”, i.e:
- the 3rd party site is somehow prevented from loading by MozRepl (or the execution of the privileged code I supply)???, or
- the 3rd party site loads, but code executed by MozRepl doesn’t see it load, or
- the 3rd party site loads, and MozRepl isn’t ready to take requests (despite other JavaScript running in the page, and port 4242 being bound by the Firefox process),
- etc.
I thought about maybe doing something like this:
Modify the MozRepl source in some way to load privileged JavaScript from a predictable place in the filesystem at start-up (or interact with Firefox command-line arguments) and execute it in the context of the 3rd party website.
… or even write another similar add-on which is more dedicated to the task.
Any simpler ideas?
Update:
After a lot of trial-and-error, answered my own question (below).
I found the easiest way was to write a purpose-built Firefox extension!
Step 1. I didn’t want to do a bunch of unnecessary XUL/addon related stuff that wasn’t necessary; A “Bootstrapped” (or re-startless) extension needs only an
install.rdffile to identify the addon, and abootstrap.jsfile to implement the bootstrap interface.Bootstrapped Extension: https://developer.mozilla.org/en-US/docs/Extensions/Bootstrapped_extensions
Good example: http://blog.fpmurphy.com/2011/02/firefox-4-restartless-add-ons.html
The bootstrap interface can be implemented very simply:
You compile the extension by putting
install.rdfandbootstrap.jsinto the top-level of a new zip file, and rename the zip file extension to.xpi.Step 2. To have a repeatable environment for production & testing, I found the easiest way was to launch Firefox with a profile dedicated to the automation task:
firefox -ProfileManagertesting-profile) and then exit the profile manager.profiles.iniin your user’s mozilla config (so that it won’t interfere with normal browsing).firefox -profile /path/to/testing-profileabout:blanktab open, then exit Firefox.tar cvf testing-profile-snapshot.tar /path/to/testing-profileFrom that point onward, every time I run the automation, I unpack
testing-profile-snapshot.tarover the existingtesting-profilefolder and runfirefox -profile /path/to/testing-profile about:blankto use the “pristine” profile.Step 3. So now when I launch Firefox with the
testing-profileit will “include” the external code at/PATH/TO/EXTERNAL/CODE.json each start-up.NOTE: I found that I had to move the
/PATH/TO/EXTERNAL/folder elsewhere during step 2 above, as the external JavaScript code would be cached (!!! – undesirable during development) inside the profile (i.e: changes to the external code wouldn’t be seen on next launch).The external code is privileged and can use any of the Mozilla platform APIs. There is however an issue of timing. The moment-in-time at which the external code is included (and hence executed) is one at which no Chrome window objects (and so no
DOMWindowobjects) yet exist.So then we need to wait around until there’s a useful
DOMWindowobject:Step 4. All of that code executes in the “global” scope. If you later need to load other JavaScript files (e.g: jQuery), call
loadSubscriptexplicitly within thenull(global!) scopeNow we can use
jQueryon anyDOMWindowby passing<DOMWindow>.documentas the second parameter to the selector call!