How to copy the variables in the chrome console as they are, save and download them locally

Copy the following code to the console control, and press Enter to execute, and then only use the console.save (variable name) command to easily download the variables in the console to the local. 

(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)

 In fact, in the console, you can also use copy (variable name) to copy variables to the clipboard. The copy command is also very convenient

note:

In some cases, when you use the copy command to copy an array, you always get an empty array. It is recommended to use the object type. 

From my blog: https://blog.nice100.net/default/42.html

Guess you like

Origin blog.csdn.net/weixin_37281289/article/details/109550714