Peeking inside FireFox using MozRepl

I usually use Selenium for testing web applications or some quick screen scraping jobs. It is more than adequate for my purpose as most of the time I work on the back-end code. But if you spend most of your time working on Ajax, scraping and other JavaScript jobs, then you need more powerful tools at your disposal. The one I think you will find useful is MozRepl. With it you can connect to Firefox and other Mozilla apps, explore and modify them from the inside, execute Javascript, peek into HTML pages, examine functions and variables, all while FireFox is running.

MozRepl – a read-eval-print loop for Firefox – is a Firefox extension that acts like a TCP/IP socket server within Firefox. You can then connect to the socket using any client application and send commands to Firefox. Each command you send to Firefox using MozRepl is executed as if it was run from the browser, which gives you the ability to do anything that you would normally do using Javascript.

Installing MozRepl

Before proceeding you need to first install MozRepl which comes as a Firefox extension. Head over to github to download and install. Once you have installed it, go to Firefox Tools menu and select MozLab → Start MozRepl. MozRepl will now be listening for connections on the default port 4242.

Connecting to MozRepl

Now that we have MozRepl up and running we can easily connect to it using Telnet. By default MozRepl listens on port 4242, which however you can change to your liking from the MozRepl menu.

C:\> telnet localhost 4242

You will get the following response if you are correctly connected to MozRepl.

Welcome to MozRepl.
 - If you get stuck at the "...>" prompt, enter a semicolon (;) 
at the beginning of the line to force evaluation.
 - If you get errors after every character you type, see http://github.c
om/bard/mozrepl/wikis/troubleshooting (short version: stop using 
Microsoft telnet, use netcat or putty instead)
Current working context: chrome://browser/content/browser.xul
Current input mode: syntax

If you have trouble connecting using Windows telnet or encounter some inconsistent behavior, you can use Putty instead. A typical putty connection setup is shown below, note the ‘telnet’ radio selection:

Also make sure you check the ‘Implicit CR in every LF’ check box.

Once you are connected to the server you will be presented with the following prompt:


Lets try a standard greeting.

repl> window.alert("Hello World!")

MozRepl is now ready to accepts your commands. Lets try a simple one given below. The first line is the command we send to MozRepl to get the title of the current open Firefox window, while the second line is the response.

repl> document.title
"Tutorial - mozrepl - GitHub - Mozilla Firefox"

Lets try changing the document title of the current Firefox window:

repl> document.title = "hello"

Use the following to get all the function and variables in Firefox:

repl> repl.look()

This is just a sampler, you can do a whole lot of things using MozRepl, check this video for some more examples.

Connecting to MozRepl with PHP

As MozRepl creates a server on port 4242, we can easily connect to it via PHP and send commands to it. A small example is given below which you will need to run from the command line. The following code sends 3 commands to the MozRepl server and prints the responses. It uses the socketHelper class to do the dirty work of socket processing.

/* Commands to send to  MozRepl */
content.location.href = ''
$firefox_socket = new SocketHelper;
if(!$firefox_socket->connect()) exit;
foreach(explode("\n",$commands) as $command){
    if($command=='')continue; //Skip blank lines
    echo $firefox_socket->send_command($command);

Code for socketHelper class

/* socketHelper.php */
class SocketHelper
    private $address = "";
    private $port    = "4242";
    private $socket  = null;
    public function connect()
            return false;
            return false;
        return true;
    /** Send a command to MozRepl */
    public function send_command($command){
        return $this->read();
        Read from the Socket until we get a "repl>" prompt, 
        or loop forever.
    private function read(){
        $response = '';
            $chunk = socket_read($this->socket,65536,PHP_BINARY_READ);
            if($chunk === false){
                echo "Error reading from socket\n";
            if($chunk === "") break; //No more data
                $response .= $match[1];
            $response .= $chunk;
        return $response;

Security Warning

Because MozRepl can directly poke inside FireFox it can practically work with anything – your bookmarks, cookies, cache etc, so security should be your prime concern. So do not ever allow outside connections in MozRepl (Tools menu → MozLab → Allow outside connections), unless you are sure what you are doing.

I’m still investigating the things I can do with MozRepl. If you have done some cool things of your own using it, please post your ideas here.

4 Responses

  1. meemo says:

    Hats off.. Really nice post….

  2. Selenium is way to hard to understand. I was looking for a simple solution and mozrepl might be just perfect.

    I appreciate your effort to write this nice tutorial.

  3. Picture tutorials are the best. You cannot fail if you take them step by step.

  4. I’ve been using this through the CPAN module to program in perl, with delicious results. Below is an example script which grabs the top 10 links from google for any search term (hint: do this every day, and you can build up a picture of how results change over time).

    Note however – you will need to find an older 1.1.2 MozRepl .xpi file for this, there’s some weirdness with the new one, and in CPAN, you’ll need to “force install” to get it actually working (remember to have MozRepl running, and started, before installing from CPAN).


    use WWW::Mechanize::Firefox;

    my $mech = WWW::Mechanize::Firefox->new();

    my $term=shift; $term=~s/ /\+/g;

    my $url=’’.$term.’&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-a&channel=sb';


    print $_->text . ” -> ” . $_->url . “\n” for $mech->links;