Created
November 30, 2008 18:24
-
-
Save satyr/30495 to your computer and use it in GitHub Desktop.
Evaluates code and displays/copies/sets the result.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const | |
LLEval = 'http://blog.livedoor.jp/dankogai/archives/51190474.html', | |
Aliases = <pre><![CDATA[({ | |
ut: Utils, | |
cu: CmdUtils, | |
ap: Application, | |
cw: Utils.currentChromeWindow, | |
gs: function() CmdUtils.getSelection(), | |
dm: function(x)(displayMessage({icon: Icon, title: 'eval', text: x}), x), | |
ss: function(x)(CmdUtils.setSelection(x), x), | |
lg: function(x)(Utils.log(x), x), | |
cp: function(x) CmdUtils.copyToClipboard(x), | |
})]]></pre>, | |
Env = eval(Aliases.toString()); | |
var LLangs = Bin.llangs(), LLangSuggs = []; | |
llup(); | |
function llup(){ | |
if(LLangs && new Date - Bin.lastLLUp() < 24 * 60 * 6e4) return llss(); | |
$.get('http://api.dan.co.jp/lleval.cgi?q=1', 0, function llget(jsonp){ | |
LLangs = Bin.llangs(Utils.json.decode(/{[^]+}/(jsonp))); | |
delete LLangs.error; | |
llss(); | |
Bin.lastLLUp(+new Date); | |
}, 'text'); | |
} | |
function llss(){ | |
LLangSuggs = let(i = 0)[ | |
{ text: cmd, data: ext, index: i++, key: cmd +'\t'+ ext, | |
summary: <><code>{cmd}</code> <small>(.{ext})</small></> +'' } | |
for each([ext, cmd] in new Iterator(LLangs))]; | |
} | |
(function evalCommand(o){ | |
o.help = ''+ <ul/>.appendChild( | |
(<li>Execute to display/copy/set the result.</li>) + | |
(o.help || <></>)); | |
o.arguments || (o.arguments = {object_js: noun_arb_text}); | |
o.execute || (o.execute = eval_execute); | |
o.icon || (o.icon = 'moz-icon://.js?size=32'); | |
o.author = 'satyr'; | |
o.license = 'MIT'; | |
CmdUtils.CreateCommand(o); | |
return arguments.callee; | |
})({ | |
names: ['eval', '!'], | |
description: 'Evaluates JavaScript.', | |
preview: Aliases.toXMLString(), | |
previewDelay: 0, | |
})({ | |
names: ['evalp', '!!'], | |
description: 'Evaluates JavaScript previewing the result.', | |
preview: function eval_preview(pb, args){ | |
var x = eval_eval(args); | |
pb.innerHTML = <pre/>.appendChild( | |
x instanceof Error | |
? (<em>{x}</em>) : | |
typeof x === 'xml' | |
? x | |
: (<strong>{x}</strong>)); | |
}, | |
previewDelay: 333, | |
})({ | |
name: 'lleval', | |
icon: 'http://www.dan.co.jp/favicon.ico', | |
arguments: { | |
object_code: noun_arb_text, | |
format: { | |
label: 'lang', | |
default: function lleval_default() LLangSuggs[Bin.lastLLIndex() || 0], | |
suggest: function lleval_suggest(txt, htm, cb, sx){ | |
if(sx || !txt) return []; | |
var ss = CmdUtils.grepSuggs(txt, LLangSuggs, 'key'); | |
if(ss.length) Bin.lastLLIndex(ss[0].index); | |
return ss; | |
}, | |
}, | |
}, | |
description: 'Abuses '+ 'lleval.cgi'.link(LLEval) + '.', | |
help: <li>Supported: {[x for(x in LLangs)].join(' ')}</li>, | |
execute: function lleval_execute(args){ | |
this._eval(args, function llcbe({stdout, stderr}){ | |
var res = stdout || stderr; | |
done(res); | |
displayMessage(res, this); | |
}); | |
}, | |
preview: function lleval_preview(pb, args){ | |
this._eval(args, function llcbp(res){ | |
var pre = <pre>{res.stdout || ''}</pre>, table = <table/>; | |
delete res.stdout; | |
for(var k in res) | |
table.appendChild(<tr><th>{k}</th><td>{res[k]}</td></tr>); | |
pb.innerHTML = this._style + (pre + table); | |
}, pb); | |
}, | |
previewDelay: 666, | |
_eval: function({object: {text: code}, format: {data: lang}}, fun, pb){ | |
if(!code) return; | |
var me = this, params = { | |
url: 'http://api.dan.co.jp/lleval.cgi', | |
data: {c: '_', s: code, l: lang}, | |
success: function lleval_success(jsonp){ | |
fun.call(me, Utils.json.decode(/{[^]+}/(jsonp))); | |
}, | |
error: this._ajaxerr, | |
}; | |
pb ? CmdUtils.previewAjax(pb, params) : $.ajax(params); | |
}, | |
_ajaxerr: function(x){ | |
displayMessage('Ajax Error: '+ x.status +' '+ x.statusText, this); | |
}, | |
_style: '<style>'+ <![CDATA[ | |
pre {padding-left:1em} | |
table {margin:0 1em 1em} | |
th {text-align:left} | |
]]> +'</style>', | |
}); | |
function eval_execute(args){ | |
Utils.log(new Date().toLocaleString()); | |
var x = eval_eval(args); | |
done(x); | |
displayMessage(uneval(x), this); | |
} | |
function eval_eval({object: {text}}){ | |
try { with(Env) return eval(text) } | |
catch(e){ return e } | |
} | |
function done(res){ | |
if(typeof res === 'xml') var xml = res.toXMLString(); | |
var txt = xml || res; | |
if(CmdUtils.getSelection()) | |
CmdUtils.setSelection(xml || Utils.escapeHtml(res), {text: txt}); | |
else | |
CmdUtils.copyToClipboard(txt); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment