Last active
April 10, 2025 03:19
-
-
Save shhider/c1c5d3a23544f8933ed58bd052cc0567 to your computer and use it in GitHub Desktop.
[console.save] download console output to file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(console) { | |
console.save = function(data, filename = 'data.json', jsonIndent = 2) { | |
// 检查是否传入了有效的数据 | |
if (!data) { | |
console.error('Console.save: No data provided.'); | |
return; | |
} | |
// 将数据转换为 JSON 字符串 | |
let json = JSON.stringify(data, null, jsonIndent); // 使用 4 个空格进行格式化 | |
// 创建一个 Blob 对象 | |
let blob = new Blob([json], { type: 'application/json' }); | |
// 创建一个指向 Blob 的 URL | |
let url = URL.createObjectURL(blob); | |
// 创建一个 <a> 标签用于触发下载 | |
let a = document.createElement('a'); | |
a.href = url; | |
a.download = filename; | |
// 触发点击事件以开始下载 | |
a.click(); | |
// 释放 URL 对象 | |
URL.revokeObjectURL(url); | |
}; | |
})(console); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment