我目前正在使用 GM_setValue 和 GM_getValue 将数据存储在我在 Greasemonkey 中创建的用户脚本中。我希望能够轻松地将数据保存在 GM 正在从脚本本身内部存储所有数据的 sqlite 数据库中。
例如,在页面右上角创建一个显示“备份数据”的链接。单击时,它会直接下载 .sqlite 文件或它的 JSON.stringify() 值。
这有可能吗?我尝试采用此处的代码:Create text file in JavaScript但它很丑陋,是一项大量的黑客工作,并且需要使用 unsafeWindow.open() ,我无法想象当/如果我最终得到一个 100k 个字符长的 JSON 字符串时,它会很好地扩展
最佳答案
你可以用这种方式创建一个可下载的文件
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);
}
你可以调用这个函数
download('filename.sqlite', 'your-db');
关于javascript - 我如何在我的 greasemonkey 脚本中提供一个链接来备份 GM_---value 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38755589/