Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Chris Beardsley
Added: Aug 17, 2018 10:35 AM
Views: 25
Tags: no tags
  1. (function(console){
  2.  
  3.     console.save = function(data, filename){
  4.  
  5.         if(!data) {
  6.             console.error('Console.save: No data')
  7.             return;
  8.         }
  9.  
  10.         if(!filename) filename = 'console.json'
  11.  
  12.         if(typeof data === "object"){
  13.             data = JSON.stringify(data, undefined, 4)
  14.         }
  15.  
  16.         var blob = new Blob([data], {type: 'text/json'}),
  17.             e    = document.createEvent('MouseEvents'),
  18.             a    = document.createElement('a')
  19.  
  20.         a.download = filename
  21.         a.href = window.URL.createObjectURL(blob)
  22.         a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':')
  23.         e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
  24.         a.dispatchEvent(e)
  25.     }
  26. })(console)

1 comment

Chris Beardsley 3 months ago
console.save(variable, "name.js")

Write a comment