获取控制台历史记

Fra*_*ort 15 javascript google-chrome-devtools

我想知道javascript中是否有一种方法可以检索控制台历史记录.

我的意思是控制台历史记录是开发工具控制台中出现的内容.例如,我想在html页面中打印我的开发工具中显示的所有错误,警告,信息和日志,而不打开它们.

如果我不清楚,请告诉我.

San*_*ven 14

我为此编写了一个简单的跨浏览器库,名为console.history.它可以在GitHub上找到:https: //git.io/console

该库基本上做的是捕获所有调用console.[log/warn/error/debug/info]并将它们存储在console.history数组中.作为奖励,还添加了完整的堆栈跟踪.

测试文件test.js包含:

function outer() {
  inner();
}

function inner() {
  var array = [1,2,3];
  var object = {"foo": "bar", "key": "value"};
  console.warn("Something went wrong, but we're okay!", array, object);
}

outer();
Run Code Online (Sandbox Code Playgroud)

参赛作品console.history将是:

{
  "type": "warn",
  "timestamp": "Thu, 01 Sep 2016 15:38:28 GMT",
  "arguments": {
    "0": "Something went wrong, but we're okay!",
    "1": [1, 2, 3],
    "2": {
      "foo": "bar",
      "key": "value"
    }
  },
  "stack": {
    "0": "at inner (http://localhost:1337/test/test.js:6:11)",
    "1": "at outer (http://localhost:1337/test/test.js:2:3)",
    "2": "at http://localhost:1337/test/test.js:9:1"
  }
}
Run Code Online (Sandbox Code Playgroud)


NVI*_*NVI 5

Chrome 扩展程序为此提供了一个 API,experimental.devtools.console

chrome.experimental.devtools.console.getMessages(function(messages) {  })
Run Code Online (Sandbox Code Playgroud)

此 API 已被删除。

  • 链接已失效,`chrome.experimental => undefined` (11认同)

Sun*_*mbi 5

这是一种获取 chrome 控制台历史记录的方法(尽管不是 javascript 方法)-

  1. 在 DevTools 中,取消停靠到单独的窗口(使用 Ctrl+Shift+P 搜索它)
  2. Ctrl+Shift+J 到 DevTool 您的 DevTools
  3. 转到应用程序选项卡 -> 本地存储 -> devtools://devtools
  4. 双击或编辑consoleHistory的值,然后复制它

参考:https://chema.medio.click/en/dev/reviewing-the-console-command-history-in-chromes-devtools/