Save the file to a local front-end JSON

We in the Chrome Developer Tools Console tab page, you can enter a JavaScript variable and press enter to view the values ​​of these variables.

 

 

If I want to save to a local variable, after further analysis to prepare for, and now I share a simple approach to everyone.

 1 (function(console){
 2 console.save = function(data, filename){
 3 if(!data) {
 4 console.error('Console.save: No data')
 5 return;
 6 }
 7 if(!filename) filename = 'console.json'
 8 if(typeof data === "object"){
 9 data = JSON.stringify(data, undefined, 4)
10 }
11 var blob = new Blob([data], {type: 'text/json'}),
12 e = document.createEvent('MouseEvents'),
13 a = document.createElement('a')
14 a.download = filename
15 a.href = window.URL.createObjectURL(blob)
16 a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
17 e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
18 a.dispatchEvent(e)
19 }
20 })(console)
21 console.save(result, "result.json");

 

 

The paste the code to perform console page, to add a standard console objects save method. This method has two input parameters, the first is to be saved the cost of the JSON file JavaScript variables, the second parameter is the name of the local JSON file.

Or return to the example above, I carried out the above JavaScript code in the Chrome Developer Tools console page, followed by re-execute the following statement:

console.save(result, "result.json");

Enter, Chrome automatic pop-up window to save a JSON file:

It can save to a local. In this way eliminates the need to manually JavaScript variable sequence into JSON string and then save the cost of manual work to document, enhance programmer productivity.

Guess you like

Origin www.cnblogs.com/apex-wzw/p/12430386.html