jsbridge – Python to Javascript bridge for Mozilla Applications

Installation

jsbridge requires setuptools. If you do not have them installed already you will want to.:

$ curl -O http://peak.telecommunity.com/dist/ez_setup.py
$ python ez_setup.py

If you are running Python 2.5 or earlier you will also need simplejson.:

$ easy_install simplejson

On Windows you will also need pywin32 for mozrunner which can be downloaded here.

mozrunner is another dependency you will need for jsbridge:

$ easy_install mozrunner

Now you can install jsbridge.

The source code is publicly available on github.

The process for code contributions is for users to fork the repository on github, push modifications to their public fork, and then send mikeal a pull request.

jsbridge — Python to JavaScript bridge interface

jsbridge.extension_path

Absolute path to the jsbridge XULRunner extension directory.

This should be used by mozrunner when starting XULRunner applications:

import mozrunner

profile = mozrunner.FirefoxProfile(plugins=[jsbridge.extension_path])
runner = mozrunner.FirefoxRunner(profile=profile, cmdargs=["-jsbridge", "24242"])
runner.start()
jsbridge.create_network(host, port)

Returns a BackChannel instance and a Bridge instance for the given host and port configuration:

back_channel, bridge = create_network("127.0.0.1", 24242)
jsbridge.wait_and_create_network(host, port[, timeout])

Wait for the the given host/port to become available for connection or timeout and then run create_network().

host and port are the values to be passed to create_network().

Default timeout is 10. timeout defines wait threshold in seconds.

class jsbridge.Bridge(host, port)

Python to JavaScript TCP interface.

host and port are str() and int() values respectively.

class jsbridge.BackChannel(host, port)

Back channel callback TCP interface.

host and port are string and integer values respectively.

add_listener(callback[, uuid[, eventType]])

Add listener for events of a specified uuid or eventType.

Any callable() can be used for callback argument. Should accept a single argument the result object.

Callbacks can be specific to an individual event uuid or to all events of a given eventType.

add_global_listener(callback)

Add a listener to all events.

Any callable() can be used as callback, should accept two positional arguments; eventType and result.

class jsbridge.JSObject(bridge, name)

Python representation of a live JavaScript object.

Requires an instance of Bridge and a str() of JavaScript to resolve the object on the other side of the bridge.

Once you have a JSObject instance you can treat it like a normal Python object and all attribute set/get operations and function calls will happen seamlessly through the bridge:

>>> runner.start()
>>> back_channel, bridge = wait_and_create_network("127.0.0.1", 24242)
>>> window_string = "Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('')"
>>> browser_window = JSObject(bridge, window_string)
>>> browser_window.title
u"Welcome to Firefox"
>>> browser_window.title = "Remember; no matter where you go, there you are. - B. Banzai"
class jsbridge.CLI

Command Line Interface.

Inherits from mozrunner.CLI and overrides relevant methods to start jsbridge.

Also adds new command line options to for Python shell and debug modes.

Examples

Starting jsbridge in your own script:

import mozrunner
import jsbridge

profile = mozrunner.FirefoxProfile(plugins=[jsbridge.extension_path])
runner = mozrunner.FirefoxRunner(profile=profile, cmdargs=["-jsbridge", "24242"])
runner.start()

back_channel, bridge = wait_and_create_network("127.0.0.1", 24242)

Table Of Contents

This Page