2012-12-07 49 views
10

我想知道是否有JavaScript的方式來檢索控制檯歷史記錄。獲取控制檯歷史記錄

我的意思是控制檯歷史記錄是在開發工具控制檯中出現的。 例如,我想在html頁面中打印所有顯示在我的開發工具中的錯誤,警告,信息和日誌,而不打開它們。

如果我不清楚,請告訴我。

+0

http://www.whathaveyoutried.com/?它應該運行在網站上還是瀏覽器插件?你的目標瀏覽器是什麼?請將此信息添加到您的問題/標籤。 – rekire

+0

請詳細說明您正在嘗試做什麼。只有這樣我們才能幫助你。 –

回答

1

Chrome擴展曾經對自己的API,experimental.devtools.console

chrome.experimental.devtools.console.getMessages(function(messages) { }) 

此API已被刪除。

+9

鏈接已死並且'chrome.experimental => undefined' – Sigfried

+0

「實驗」是操作詞 – vothaison

1

無法通過JavaScript獲取控制檯數據。只有這樣,你才能做到這一點基本上是劫持所有的控制檯功能並存儲一份副本,而不是調用默認的日誌行。

1
console.history = []; 
var oldConsole = {}; 
for (var i in console) { 
    if (typeof console[i] == 'function') { 
     oldConsole[i] = console[i]; 
     var strr = '(function(){\ 
      console.history.push({func:\'' + i + '\',args : Array.prototype.slice.call(arguments)});\ 
      oldConsole[\'' + i + '\'].apply(console, arguments);\ 
     })'; 
     console[i] = eval(strr); 
    } 
} 

然後用console.history訪問歷史

+0

在Chrome上不起作用63 – Bright

7

我寫了這個簡單的跨瀏覽器庫,稱爲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(); 

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" 
    } 
} 
+1

這應該是公認的答案。 – Darkrum