aboutsummaryrefslogtreecommitdiffstats
path: root/bufferecho.js
blob: 10f48e984640a38f6a303ec470f12dbbc910bab1 (plain)
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:
a> 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389