Skip to content

Instantly share code, notes, and snippets.

@M-ZubairAhmed
Created May 23, 2023 12:34
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 M-ZubairAhmed/c348d70afe1adcf8d774c9ad983c671b to your computer and use it in GitHub Desktop.
Save M-ZubairAhmed/c348d70afe1adcf8d774c9ad983c671b to your computer and use it in GitHub Desktop.
Saving the redux store in file exposed on window
// Save the console function in the console once
(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' })
var mouseEvent = document.createEvent('MouseEvents')
var anchorElement = document.createElement('a')
anchorElement.download = filename
anchorElement.href = window.URL.createObjectURL(blob)
anchorElement.dataset.downloadurl = [
'text/json',
anchorElement.download,
anchorElement.href
].join(':')
mouseEvent.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
anchorElement.dispatchEvent(mouseEvent)
}
})(console)
// Usage for redux store
// console.save(window.store.getState(), 'redux-store-1.json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment