forked from vimpr/vimperator-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bufferecho.js
48 lines (47 loc) · 1.67 KB
/
bufferecho.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* ==VimperatorPlugin==
* @name bufferecho.js
* @description Display results of JavaScript to a buffer(browser) instead of commandline-buffer
* @description-ja JavaScript実行結果をコマンドライン・バッファではなくバッファ(ブラウザ)に表示
* @version 0.1
* ==/VimperatorPlugin==
*/
liberator.plugins.buffer_echo = (function(){
var title = "bufferecho results";
var prefix = 'data:text/html,';
function execute(str){
var result;
try {
result = (function(){ return window.eval("with(liberator) {" + str + "}") })();
} catch (e) {
result = e.name + ":\n" + e.message;
}
return result;
}
commands.addUserCommand(['bufferecho','becho'],'Display results of JavaScript to a buffer(browser)',
function(args){
liberator.plugins.buffer_echo.open(args.string, args.bang);
},{
completer: function(context) completion.javascript(context)
},true
);
var manager = {
append: function(htmlString){
var body = util.evaluateXPath('/html/body').snapshotItem(0);
body.innerHTML += htmlString;
},
open: function(str, forceNewTab) {
var result = execute(str);
if (typeof(result) == "object") result = util.objectToString(result,true);
var data = '<div><h1>' + util.escapeHTML(str) + '</h1><pre>' + result + '</pre></div>';
if (buffer.title == title && !forceNewTab){
this.append(data);
return;
}
var where = buffer.URL == "about:blank" ? liberator.CURRENT_TAB : liberator.NEW_TAB;
liberator.open([prefix + '<title>'+title+'</title>' + data], where);
}
};
return manager;
})();
// vim:sw=4 ts=4 et: