Created
December 23, 2025 07:52
-
-
Save janaSunrise/a9c785f383f558c5805f1bed1c975807 to your computer and use it in GitHub Desktop.
Save data to a file from the browser console.
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
| console.save = function save(data, filename = "console.json") { | |
| if (data === undefined) { | |
| console.error("console.save: No data provided"); | |
| return; | |
| } | |
| let content; | |
| let mimeType = "application/json"; | |
| try { | |
| if (typeof data === "string") { | |
| content = data; | |
| mimeType = "text/plain"; | |
| } else { | |
| content = JSON.stringify(data, null, 4); | |
| } | |
| } catch (err) { | |
| console.error("console.save: Failed to serialize data", err); | |
| return; | |
| } | |
| const blob = new Blob([content], { type: mimeType }); | |
| const url = URL.createObjectURL(blob); | |
| const a = document.createElement("a"); | |
| a.href = url; | |
| a.download = filename; | |
| a.style.display = "none"; | |
| document.body.appendChild(a); | |
| a.click(); | |
| document.body.removeChild(a); | |
| URL.revokeObjectURL(url); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment