Quantcast
Channel: code-diesel » firefox
Viewing all articles
Browse latest Browse all 2

Peeking inside FireFox using MozRepl

0
0

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
 
repl>

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:

repl>

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.

<?php
 
require_once('socketHelper.php');
 
/* Commands to send to  MozRepl */
 
$commands=<<<EOD
repl.whereAmI()
content.location.href = 'http://google.com'
repl.quit()
EOD;
 
$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

<?php
 
/* socketHelper.php */
 
class SocketHelper
{
 
    private $address = "127.0.0.1";
    private $port    = "4242";
    private $socket  = null;
 
    public function connect()
    {
        $this->socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
        if(!$this->socket){
            socket_strerror($this->socket)."\n";
            return false;
        }
 
        $result=socket_connect($this->socket,$this->address,$this->port);
 
        if(!$result){
            socket_strerror($result)."\n";
            socket_close($this->socket);
            return false;
        }
 
        $this->read();
        return true;
    }
 
    /** Send a command to MozRepl */
    public function send_command($command){
        $command.="\n";
        socket_write($this->socket,$command);
        return $this->read();
    }
 
 
    /* 
        Read from the Socket until we get a "repl>" prompt, 
        or loop forever.
     */
    private function read(){
        $response = '';
        while(1){
            $chunk = socket_read($this->socket,65536,PHP_BINARY_READ);
            if($chunk === false){
                echo "Error reading from socket\n";
                break;
            }
            if($chunk === "") break; //No more data
 
            if(preg_match('|^(.*)\s*repl\d*>\s*$|s',$chunk,$match)){
                $response .= $match[1];
                break;
            }
 
            $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.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images