【问题标题】:Accessing echo globally (Jquery Terminal)全局访问回声(Jquery 终端)
【发布时间】:2015-02-08 17:13:30
【问题描述】:

我的页面上嵌入了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

如果我手动输入 Chrome 控制台:

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

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

【问题讨论】:

    标签: jquery jquery-terminal


    【解决方案1】:

    插件返回终端实例,因此您应该执行以下操作:

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

    如果你想访问当前终端,你可以使用:

    var term = $.terminal.active();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      相关资源
      最近更新 更多