全局访问 echo(Jquery 终端)

Accessing echo globally (Jquery Terminal)

我的页面上嵌入了 jQuery.terminal,我可以通过在其中输入命令来发送命令。我正在处理的脚本中有许多 console.log 和 console.dir 命令,我希望终端也输出这些命令,但它没有。

我在想这可能就像这个 SO 问题上的人所做的那样;通过覆盖 console.log:

Embed JS Console within website

我想用 jQuery.terminal

做这样的事情
console.log=function(str){term.echo(str);};

我看不出它应该如何工作。由于我无法通过 Chrome 控制台访问终端(我找不到执行回显的部分)。

var log;

$('#term').terminal(function(command,term){
    if(command!==''){
        try{
            var result=window.eval(command);
            if(result!==undefined){
                term.echo(new String(result));
                }}
        catch(e){
            term.error(new String(e));
            }}
    else{
        term.echo('');
        }},{
    welcome:false,
    height:200,
    prompt:'> ',
    onInit:function(term){
        log=term;                 // now log should reference to term
        alert('hello');           // I don't see this alert
        }});

console.log('hello');     //only the browsers own console shows this message

然后如果我手动输入 Chromes 控制台:

log.echo('hi'); // Cannot read property 'echo' of undefined

我看到有一个 $.terminal 对象,但是我没有看到从这个 ether 访问 echo 的方法。我该如何正确地做到这一点,或者更确切地说,开发人员是否已经设置了一种我所缺少的定义方式来做到这一点?我不想弄乱逗号分隔的日志或目录对象。

插件return终端实例所以你应该做这样的事情:

var term = $('#term').terminal(function(command,term) {
 ...
}, {...});

console.log = function(str){ term.echo(str); };

另外,如果您想访问当前终端,您可以使用:

var term = $.terminal.active();