javascript - extension crash on exporting stringified/encodeURIComponent data to file -
it exporting extension data options page.
have array of objects, stored page screenshots encoded in base64, , other minor obj properties. i'm trying export them code:
exp.onclick = expdata; function expdata() { chrome.storage.local.get('extdata', function (result) { var datatosave = result.extdata; var strst = json.stringify(datatosave); downloadfn('extdata.txt', strst); }); } function downloadfn(filename, text) { var flink = document.createelement('a'); flink .setattribute('href', 'data:text/plain;charset=utf-8,' + encodeuricomponent(text)); flink .setattribute('download', filename); flink .click(); } on button click, data storage, stringify it, create fake link, set attributes , click it.
code works fine if resulting file under ~1.7 mb, above produce option page crash , extension gets disabled.
can console.log(strst) after json.stringify , works fine no matter of size, if don't pass download function..
there can fix code , avoid crash?...or there limitation size when using methods?
i solved this, xan suggested, switching chrome.downloads (it's permission, works fine)
did replacing code in downloadfn function, it's cleaner way
function downloadfn(filename, text) { var euctxt = encodeuricomponent(text); chrome.downloads.download({'url': 'data:text/plain;charset=utf-8,'+euctxt, 'saveas': false, 'filename': filename}); } note using url.createobjecturl(new blob([ text ])) produce same crashing of extension
edit: @dandavis pointed (and robw confirmed), converting blob works
(i had messed code producing crash)
better way of saving data locally, because on browser internal downloads page, dataurl downloads can clutter page , if file big (long url), crashes browser. presented actual urls (which raw saved data) while blob downloads id
function downloadfn(filename, text) { var vlink = document.createelement('a'), vblob = new blob([text], {type: "octet/stream"}), vurl = window.url.createobjecturl(vblob); vlink.setattribute('href', vurl); vlink.setattribute('download', filename); vlink.click(); }
Comments
Post a Comment