Skip to content

Instantly share code, notes, and snippets.

@ericsoco
Created April 18, 2020 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericsoco/57483b0b569545c79b830ed590f424df to your computer and use it in GitHub Desktop.
Save ericsoco/57483b0b569545c79b830ed590f424df to your computer and use it in GitHub Desktop.
// Based on Brian Grinstead's console.save()
// http://bgrins.github.io/devtools-snippets/#console-save
(function(console){
console.saveCsv = function(data, filename){
if(!data) {
console.error('Console.saveCsv: No data')
return;
}
if(!filename) filename = 'console.csv'
if(Array.isArray(data)){
data = data.reduce((csv, row) => {
csv += (row.join(',') + '\n');
return csv;
}, '');
}
console.log(data);
var blob = new Blob([data], {type: 'text/csv'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/csv', 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment