I'm currently using GM_setValue and GM_getValue to store data in a userscript I made in Greasemonkey. I'd like to be able to easily save the data in the sqlite database GM is storing all of the data in, from within the script itself.
For exmaple, create a link that says "Backup data" in the upper right corner of the page. When clicked, it would download either the .sqlite file directly, or the JSON.stringify()'d value of it.
Is this something that's possible? I tried to adopt the code from here: Create text file in JavaScript but it's ugly as sin, a massive hack job, and requires the use of unsafeWindow.open() which I can't imagine will scale well when/if I end up having a JSON string 100k characters long
u can create a downloadable file in this way
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
you can call this function
download('filename.sqlite', 'your-db');