Thursday 5 February 2009

A firefox extension allowing to run operating system commands

CommandRun is a firefox extension which allows to run operating system commands from javascript.
At least it is tested with firefox 3.0.5 on Linux.

It allows javascript code like this:

CommandRun.run("/usr/bin/touch",["/tmp/commandruntest"]);

which will create the file /tmp/commandruntest or update its timestamp.

Allowed commands must be defined in the preferences. The preference "extensions.commandrun.allowedcommands" expects a JSON array containing the allowed commands, e.g.

["/usr/bin/touch","/usr/bin/ls"]

Other commands are rejected.

So why did I develop this extension at all?
For an intranet web application I searched for a possibility to raise the firefox window from within javascript. The web application is a GWT application, that means it is a javascript client running in a browser. The nature of the application is that it may idle for a long time until it gets an event pushed by a server. The user of the application may do other things while she is waiting for an event to arrive, she may even work with other applications, reading mails, etc. The focus of the web application may be lost, firefox may be hidden by other windows. When an event arrives firefox should immediately raise to the front such that the user can react on the event.
I did not find a simple javascript solution to raise firefox to the front. If anybody knows, please tell me.
wmctrl is a linux tool which allows to raise windows (and do other things to the window manager).
The next step is then to find a possibility to call wmctrl from within javascript. And there comes the CommandRun extension.
(puh, long speech)

The extension is based on code snippets from two other extensions:
callout contains code which adds an object to a document's window with which new operations can be called. In my extension's case the CommandRun object is added which allows to invoke 'run'.
TerminalRun allows to execute selected text in a terminal. It contains code to create an operating system process.

The source can be downloaded via anonymous svn from http://subversion.banapple.de/public/commandrun/.

Additionally I dared to publish the extension on addons.mozilla.org where it can be downloaded with better update support. The current version 0.2.0 is only a start. It is enough for my needs but other people might want the output of the command available in javascript.