Load, reload a firefox tab from python.

Gribouillis 0 Tallied Votes 920 Views Share

I discovered and installed a firefox addon called Remote Control. This addon starts a telnet server which lets you send javascript commands to the firefox web browser. Combined with python telnetlib module, it becomes very easy to reload or change the content of a tab in a running firefox from a python script. Note that the addon manages only one tab at a time.

#!/usr/bin/env python
# -*-coding: utf8-*-
# Title: remotecontrol.py
# Author: Gribouillis
# Created: 2012-02-03 14:51:45.755655 (isoformat date)
# License: Public Domain
# Use this code freely.

""" This module allows to change or reload the content of a tab in
    a running firefox instance equiped with the Remote Control addon
    (see https://addons.mozilla.org/en-US/firefox/addon/remote-control)
    
    Notice that after installing the addon, you must install a
    Remote Control icon in firefox add-on bar for this to work
    (right click in the menubar and choose Customize).

    See the example in the __main__ section below.
"""

import sys
import telnetlib

version_info = (0, 1)
version = ".".join(map(str, version_info))

class Control(object):
    def __init__(self, port=32000):
        self.host = 'localhost'
        self.port = port

    def reload(self):
        tn = telnetlib.Telnet(self.host, self.port)
        tn.write("reload\n")
        result = tn.read_until('\n')
        return result
        
    def load(self, url):
        tn = telnetlib.Telnet(self.host, self.port)
        tn.write('window.location="%s"' % url)
        result = tn.read_until('\n')
        return result

if __name__ == "__main__":
    "This part will run after selecting a tab for remote control in firefox"
    from time import sleep
    c = Control()
    print c.load("http://www.python.org")
    sleep(1)
    print c.reload()
    sleep(1)
    print c.load("http://www.daniweb.com")
Gribouillis 1,391 Programming Explorer Team Colleague

A slightly improved version with error handling, properly closed connection and a new href() method.

#!/usr/bin/env python
# -*-coding: utf8-*-
# Title: remotecontrol.py
# Author: Gribouillis
# Created: 2012-02-03 14:51:45.755655 (isoformat date)
# License: Public Domain
# Use this code freely.


""" This module allows to change or reload the content of a tab in
    a running firefox instance equiped with the Remote Control add-on
    (see https://addons.mozilla.org/en-US/firefox/addon/remote-control)
    
    Notice that after installing the add-on, you must install a
    Remote Control icon in firefox add-on bar for this to work
    (right click in the menubar and choose Customize).

    See the example in the __main__ section below.
"""

from functools import wraps
import socket
import sys
import telnetlib

version_info = (0, 2)
version = ".".join(map(str, version_info))

class RemoteControlError(Exception):
    pass

def with_telnet(meth):
    @wraps(meth)
    def wrapper(self, *args, **kwd):
        try:
            self.tn = telnetlib.Telnet(self.host, self.port)
        except socket.error, e:
            raise RemoteControlError(str(e))
        try:
            return meth(self, *args, **kwd)
        finally:
            self.tn.close()
            self.tn = None
    return wrapper

class RemoteControl(object):
    def __init__(self, port=32000):
        self.host = 'localhost'
        self.port = port
        self.tn = None

    @with_telnet
    def reload(self):
        self.tn.write("reload\n")
        return self.tn.read_until('\n')
    
    @with_telnet
    def load(self, url):
        self.tn.write("window.location='%s';\n" % url)
        result = self.tn.read_until('\n')         
        return result

    
    @with_telnet
    def href(self):
        self.tn.write('windows.location.href\n')
        return self.tn.read_until('\n')

if __name__ == "__main__":
    "This part will run after selecting a tab for remote control in firefox"
    from time import sleep
    duration = 2
    c = RemoteControl()
    print c.load("http://www.python.org")
    sleep(duration)
    print c.reload()
    sleep(duration)
    print c.load("http://www.daniweb.com")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.