Skip to content
noahsug edited this page Oct 2, 2012 · 25 revisions

Example

The following script creates a custom “/dance” command that outputs “(>'-')> <('-'<) ^(' - ')^ <('-'<) (>'-')>” when typed.

send('hook_command', 'dance');
onMessage = function(e) {
 dance = "(>'-')> <('-'<) ^(' - ')^ <('-'<) (>'-')>";
 send(e.context, 'command', 'say', dance);
 propagate(e, 'none');
};

Implementation

Loading

Scripts are loaded for the first time from the local file system using chrome.fileSystem. Once a script is loaded it is saved to a persistent local app storage and automatically loaded each time the program starts. Scripts can be managed through a script preferences window.

Sandboxing

The script source code is passed to a sandboxed iframe, which calls eval(). This prevents the script from accessing the IRC window object and chrome.* APIs. See full details here.

Running

The script and IRC client communicate using window.postMessage(). The contents of the passed messages are described below.

Chrome IRC Scripting API

Events

To listen to events, define an onMessage function

onMessage = function (event) { // code here };

Events have the following fields

  • type - the kind of event (either “command”, “server” or “message”)
  • name - the name of the event (e.g. “kick”, “joined”, “nickinuse”)
  • context - where the event happened
    • server - (e.g. “irc.freenode.net”)
    • channel - (e.g. “#bash”)
  • args - a variable length array of any arguments (e.g. the text to print on /say)

There are four types of events

  • command - a command is entered (e.g. “/kick #sugarman”)
  • server - server related activity (e.g. we got kicked from a room)
  • message - anything that gets printed on the screen (e.g. the MOTD)
  • ui - anything that relates to the user interface (e.g. a notification was clicked on)

Commands

To send a command, use the send() function.

send(opt_context, type, name, args...)

Events are listened to by sending a hook event. send('hook_server', 'joined') registers the script to receive a server joined event every time the user joined a server.

Events can be sent back to the IRC client as if the user typed them or the server sent them. send(context, 'command', 'say', 'hello world!') is treated as if the user typed "/say hello world!"

Events can also be interrupted so that the client never receives them. propagate(receivedEvent, ‘none’) stops the event from being forwarded to the client alternatively, sending propagate(receivedEvent, ‘all’) forwards the event to the client Note: propagate should be called every time an event is received

Clone this wiki locally