Skip to content

Instantly share code, notes, and snippets.

@dalmaer
Created Mar 25, 2009
Embed
What would you like to do?
Super-primitive Bespin integration
CmdUtils.CreateCommand({
name: "bespin",
icon: "https://bespin.mozilla.com/favicon.ico",
homepage: "http://bespin.mozilla.com/",
author: {name: "Atul Varma", email: "avarma@mozilla.com",
homepage: "http://www.toolness.com"},
license: "MIT",
description: "Super-primitive Bespin integration.",
help: "You can run any Bespin command-line command via Ubiquity; just make sure you're on a tab with the Bespin editor when you use the command.",
takes: {"command": /.*/},
preview: function(pblock, dobj) {
var fullCommand = dobj.text;
var parts = fullCommand.split(" ");
var inputCmd;
if (parts.length)
inputCmd = parts[0];
var esc = Utils.escapeHtml;
// Hmm, hopefully this isn't too horribly insecure.
var win = XPCSafeJSObjectWrapper(context.focusedWindow.wrappedJSObject);
var commandLine = win.bespin.get('commandLine');
if (win.bespin && commandLine) {
var commands = commandLine.commands;
var html = "";
for (name in commands) {
if (!inputCmd || name.indexOf(inputCmd) == 0) {
var cmd = commands[name];
var takes = "";
if (cmd.takes)
cmd.takes.order.forEach(
function(arg) { takes += "[" + esc(arg) + "] "; }
);
html += esc(name) + " " + takes + ' <span style="color: gray">' + esc(cmd.preview) + "</span><br>";
}
}
pblock.innerHTML = html;
} else
pblock.innerHTML = "<b>You do not have Bespin in the current tab/window.</b>";
},
execute: function(dobj) {
var fullCommand = dobj.text;
var parts = fullCommand.split(" ");
if (!parts.length) {
displayMessage("No command entered!");
return;
}
// Dynamically generate a <script> tag and inject it into the page.
// (I tried just using an XPCSafeJSObjectWrapper to call the command,
// but some commands seemed to have issues with it.)
var name = parts[0];
args = parts.slice(1);
var argList = Utils.encodeJson(args).slice(1, -1);
var script = context.focusedWindow.document.createElement("script");
var code = "bespin.get('commandLine').executeCommand('" + fullCommand + "');";
script.textContent = code;
context.focusedWindow.document.body.appendChild(script);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment