Skip to content

Instantly share code, notes, and snippets.

@PCouaillier
Created January 21, 2019 14:44
Show Gist options
  • Save PCouaillier/9bf091038f26d7673f96f7055521f619 to your computer and use it in GitHub Desktop.
Save PCouaillier/9bf091038f26d7673f96f7055521f619 to your computer and use it in GitHub Desktop.
exportVar
function exportVar(varToExport, filename) {
if (varToExport === undefined) { throw new Error('undefined var'); }
if (!filename) {
filename = 'exportedVar.json';
}
const fileBlob = new File([JSON.stringify(varToExport)], filename);
const dowloader = document.createElement('a');
dowloader.setAttribute('download', filename);
const url = URL.createObjectURL(fileBlob);
dowloader.setAttribute('href', url);
document.body.appendChild(dowloader);
dowloader.click();
requestIdleCallback(() => {
URL.revokeObjectURL(url);
document.body.removeChild(dowloader);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment