【问题标题】:reading the firebug console in javascript在 javascript 中读取萤火虫控制台
【发布时间】:2010-10-10 17:22:39
【问题描述】:

我正在寻找一种方法来读取记录到 firebug 控制台的最新命令。

例如,我可以做一些事情

console.debug('The most current request URI is /sweatsocks');

然后另一段(伪)代码可以然后

if (mostRecentConsoleEntry().endsWith('/sweatsocks')) {
  // do some stuff
}

调试语句的上下文将在被测代码中,控制台检查将在 selenium 脚本中完成。这可以让我观察深埋在 js 函数中的信息以及在运行时构建的东西。

【问题讨论】:

  • “最新的请求 URI 是 /sweatsocks”——这是我听过的最粗暴的 URI。

标签: javascript selenium firebug


【解决方案1】:

您可以覆盖console.log 函数以添加您需要的任何额外功能。

var oldLog = console.log;
var lastLog;
console.log = function () {
    // do whatever you need to do here: store the logs into a different variable, etc
    // eg:
    lastLog = arguments;

    // then call the regular log command
    oldLog.apply(console, arguments);
};

这不是最安全的解决方案,因为console 允许 printf 样式语法:

console.log("%d + %d = %s", 1, 3, "four");

...但这对你来说可能是一个开始。

【讨论】:

  • 这就是我希望我知道的 :) +1
  • 它在 FF3 和 Firebug 1.3.3 中不起作用,因为 console.log 是只读的。此外,您不允许向控制台对象添加属性。
  • apply 的语法是 .apply(thisobject,array),所以如果它改变了控制台对象,它应该是:console.oldLog.apply(console.oldLog,arguments);但我建议使用自执行函数来存储私有变量。
  • printf 风格的语法实际上仍然可以正常工作,为什么不呢?
  • @nickf - 这(或更完整的控制台对象实现)会停止将 Firefox 模块记录到控制台吗?我在 resource:///modules/sessionstore/SessionStore.jsm 上出现内存不足错误,并希望我可以使用 Javascript 定期探测控制台是否存在此错误,以便在浏览器最终崩溃时向我发出警告(因为我没有t 经常关闭它,并且是一个重度互联网用户)它不会从那时起使用会话数据进行恢复(我所看到的是什么)?
【解决方案2】:

不要尝试覆盖console.debug,实现console.debug加上你需要的功能。

var debugCalls = [ ];
function myDebug(errorMessage){
  console.debug(errorMessage); //maintain original functionality
  debugCalls[debugCalls.length]  = errorMessage;
  //the previous argument to myDebug is debugCalls[debugCalls.length]

  //you may also want to call an ajax function to report this error
  mailError(errorMessage);
}

【讨论】:

    【解决方案3】:

    您能否重写console.log(),并将所有日志附加到一个数组中?然后启动原始的console.log() 并重复它正在做的事情以在控制台上获取您的调试输出?

    【讨论】:

      【解决方案4】:

      这是我整理的更详细的版本:

      /**
       * Console log with memory
       *
       * Example:
       *
       *     console.log(1);
       *     console.history[0]; // [1]
       *
       *     console.log(123, 456);
       *     console.history.slice(-1)[0]; // [123, 456]
       *
       *     console.log('third');
       *     // Setting the limit immediately trims the array,
       *     // just like .length (but removes from start instead of end).
       *     console.history.limit = 2;
       *     console.history[0]; // [123, 456], the [1] has been removed
       *
       * @author Timo Tijhof, 2012
       */
      console.log = (function () {
          var log  = console.log,
              limit = 10,
              history = [],
              slice = history.slice;
      
          function update() {
              if (history.length > limit) {
                  // Trim the array leaving only the last N entries
                  console.history.splice(0, console.history.length - limit);
              }
          }
      
          if (console.history !== undefined) {
              return log;
          }
      
          Object.defineProperty(history, 'limit', {
              get: function () { return limit; },
              set: function (val) {
                  limit = val;
                  update();
              }
          });
      
          console.history = history;
      
          return function () {
              history.push(slice.call(arguments));
              update();
              return log.apply(console, arguments);
          };
      
      }());
      

      【讨论】:

      • 更新:添加了 Object.defineProperty 逻辑,以便设置 console.history.limit 立即修剪它,而不是在下一次 log() 调用之后(就像内部数组长度一样)
      【解决方案5】:

      您可能想要实现一个队列。扩展德文的答案:(类似这样)

      var window.log = [];
      
      logger function(msg) {
        var log_length = 10;
        console.log(msg);
        window.log.push(msg);
        if(window.log.length > log_length) {
          window.log.shift()
        }
      }
      

      见:
      How do you implement a Stack and a Queue in JavaScript?
      http://aymanh.com/9-javascript-tips-you-may-not-know#string-concatenation-vs-arrayjoin

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-12
        • 2013-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-07
        相关资源
        最近更新 更多