Skip to content
This repository has been archived by the owner on Aug 19, 2021. It is now read-only.

Custom interactors

bard edited this page Sep 13, 2010 · 18 revisions

By default, MozRepl listens for JavaScript code, evaluates it, and prints the result of the evaluation.

An experimental feature allows you to replace this scheme with your own, so that you can listen for any kind of input and return results in any format you want, while still operating inside the XUL application and the repl object.

As an example, try the following toy inspector.

Create a file (e.g. /home/user/.mozrepl.js), copy and paste the content below, and set extensions.mozrepl.initUrl to the file URI (file:///home/user/.mozrepl.js).


    interactors.inspect = {
        onStart: function() {
            this._prompt();
        },
    
        getPrompt: function() {
            return '\nLocation: ' + represent(this._workContext) + '\ni> ';
        },
    
        handleInput:  function(input) {
            var [command, arg] = input.replace(/^\s+|\s+$/g, '').split(/\s+/, 2);
    
            switch(command) {
            case 'q':
                this.setInteractor('javascript');
                break;
    
            case 'l':
                this.look();
                break;
    
            case 's':
                this.print(this._workContext[arg].toString());
                break;
    
            case 'e':
                this.enter(this._workContext[arg]);
                break;
    
            case 'b':
                this.back();
                break;
    
            case 'h':
                this.print('l        : look around in current context');
                this.print('e <name> : enter object pointed to by <name>');
                this.print('s <name> : show object poined by <name> using toString()');
                this.print('b        : go back to previous context');
                this.print('h        : this help text');
                this.print('q        : quit inspector, go back to javascript interactor');
                break;
    
            default:
                this.print('Uknown command.');
                break;
            }
            this._prompt();
        }
    };

Then connect to the REPL and try the following:


    repl> repl.setInteractor('inspect')
    Location: [object ChromeWindow] — {repl: {…}, NS_ASSERT: function() {…}, 
location: {…}, PluralForm: {…}, DownloadUtils: {…}, __SSi: "window1222382445235", 
statusBar: {…}, ...}
    i> h
    l        : look around in current context
    e <name> : enter object pointed to by <name>
    s <name> : show object poined by <name> using toString()
    b        : go back to previous context
    h        : this help text
    q        : quit inspector, go back to javascript interactor

    Location: [object ChromeWindow] — {repl: {…}, NS_ASSERT: function() {…}, 
location: {…}, PluralForm: {…}, DownloadUtils: {…}, __SSi: "window1222382445235", 
statusBar: {…}, ...}
    i> s BrowserHome
    function BrowserHome() {
        var homePage = gHomeButton.getHomePage();
        loadOneOrMoreURIs(homePage);
    }

You can also make a custom interactor the default one by setting the extensions.mozrepl.defaultInteractor to the interactor name.