prhwzg
Last Updated: March 02, 2016
·
2.912K
· ollieglass
Ecebd64d9032bb92d186c2fe0e0879d8

Add console.save to Chrome

Need to export some data from the browser console?

Open Sources -> Snippets in the console, right click to make a new file. Call it consoleSave.js

Picture

Paste the JavaScript code below into the snippet (or get the latest version from http://bgrins.github.io/devtools-snippets/#console-save)

Right click on consoleSave.js and run the snippet.

Voila, now you can call console.save() to
download objects from the console!

Picture

(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)
Say Thanks
Respond

1 Response
Add your response

20819
1935132 1166902285616 7408025 n

Nice. I'll try this out. It is always good when you're debugging js issues to be able to save the console output. Now I won't have to copy paste all of them. Thanks!

over 1 year ago ·