我尝试使用 JSON.stringify(object)
,但它并没有影响整个结构和层次结构。
另一方面,console.log(object)
会这样做,但我无法保存它。
在 console.log
输出中,我可以将所有子项一一展开,然后选择和复制/粘贴,但结构太大了。
最佳答案
更新: You can now just right click
Right click > Save as in the Console panel to save the logged messages to a file.
原答案:
您可以使用下面显示的这个 devtools 片段来创建一个 console.save 方法。它根据输入创建一个 FileBlob,然后自动下载它。
(function(console){
console.save = function(data, filename){
if(!data) {
console.error('Console.save: No data')
return;
}
if(!filename) filename = 'console.json'
if(typeof data === "object"){
data = JSON.stringify(data, undefined, 4)
}
var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e)
}
})(console)
来源:http://bgrins.github.io/devtools-snippets/#console-save
https://stackoverflow.com/questions/11849562/