Skip to content

Instantly share code, notes, and snippets.

@arikui
Created February 23, 2009 07:12
Show Gist options
  • Save arikui/68842 to your computer and use it in GitHub Desktop.
Save arikui/68842 to your computer and use it in GitHub Desktop.
Language Evaluate Protocol
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
var Ci = Components.interfaces;
var Cc = Components.classes;
var Language = {
evaluators: {
"jscript": "cscript //E:JScript //Nologo",
"vbs" : "cscript //E:VBScript //Nologo",
"ruby" : "ruby",
"perl" : "perl",
},
evaluate: function(command, code){
var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
var bat = new TempFile("eval", "bat");
var script = new TempFile("eval", "code");
var out = new TempFile("eval", "out");
script.write(code);
code.length? bat.write([command, script.file.path, ">", out.file.path].join(" "))
: bat.write([command, ">", out.file.path].join(" "));
process.init(bat.file);
process.run(true, [], 0);
var res = out.read();
bat.remove(false);
out.remove(false);
if(script.exists)
script.remove(false);
return res;
}
};
function Protocol(){}
Protocol.prototype = {
classDescription: "Language Evaluate Protocol",
classID : Components.ID("F32C80B3-404B-4ce0-8590-B9DC25BDFA9A"),
contractID : "@mozilla.org/network/protocol;1?name=eval",
QueryInterface : XPCOMUtils.generateQI([Ci.nsIProtocolHandler]),
scheme : "eval",
defaultPort : -1,
protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE
| Ci.nsIProtocolHandler.URI_NOAUTH
| Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD,
allowPort: function(port, scheme){
return false;
},
newURI: function(spec, charset, baseURI){
var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
uri.spec = spec;
return uri;
},
newChannel: function(aURI){
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var comps = (new RegExp("^" + this.scheme + ":([^;]+);?(.*)$")).exec(aURI.spec);
var lang = decodeURIComponent(comps[1]);
var code = decodeURIComponent(comps[2]);
var evalu = Language.evaluators[lang] || lang;
var res = Language.evaluate(evalu, code);
log(res);
return ios.newChannel("data:text/plain;base64," + btoa(res) + "')", null, null);
}
}
function NSGetModule(compMgr, fileSpec){
return XPCOMUtils.generateModule([Protocol]);
}
/// util
function log(msg){
var win = Cc['@mozilla.org/appshell/window-mediator;1']
.getService(Ci.nsIWindowMediator)
.getMostRecentWindow('navigator:browser');
if(win.FirebugConsole && win.FirebugContext){
var console = new win.FirebugConsole(win.FirebugContext, win.content);
console["log"].apply(console, arguments);
return true;
}
if(win.Firebug && win.Firebug.Console){
win.Firebug.Console.logFormatted.call(win.Firebug.Console, Array.slice(arguments), win.FirebugContext, "log");
return true;
}
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage('' + msg);;
}
function TempFile(name, ext){
var file = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
file.append([name, (new Date()).getTime(), "tmp", ext].join("."));
this.file = file;
}
TempFile.prototype = {
read: function(){
var istream = Cc["@mozilla.org/network/file-input-stream;1"] .createInstance(Ci.nsIFileInputStream);
istream.init(this.file, 0x01, 0444, 0);
istream.QueryInterface(Ci.nsILineInputStream);
var line = {}, lines = [], hasmore;
do{
hasmore = istream.readLine(line);
lines.push(line.value);
} while(hasmore);
istream.close();
return lines.join("\n");
},
write: function(data){
if(!data.length)
return false;
var foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
foStream.init(this.file, 0x02 | 0x08 | 0x20, 0664, -1);
foStream.write(data, data.length);
foStream.close();
},
remove: function(){
this.file.remove(false);
this.file = null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment