How can I provide a link inside my greasemonkey script to back up the GM _--- value database?

I am currently using GM_setValue and GM_getValue to store data in usercript, which I did in Greasemonkey. I would like to be able to easily save data in sqlite database. GM stores all the data inside the script itself.

For exmaple, create a link that says "Backup data" in the upper right corner of the page. When you click on it, the .sqlite file or the JSON.stringify () 'd value will be loaded directly.

Is it possible? I tried to take the code from here: Create a text file in JavaScript , but it is ugly as a sin, a huge hacking job and requires the use of unsafeWindow.open (), which I cannot imagine, will scale well when / if I end up getting JSON string with a length of 100 thousand characters

+4
source share
1 answer

u can create a downloadable file 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');
+2
source

Source: https://habr.com/ru/post/1650098/


All Articles