Chrome扩展程序:本地存储,如何导出 [英] Chrome Extension: Local Storage, how to export
问题描述
我有一个chrome扩展名,可以将大量数据保存到chrome.storage.local.我正在尝试找到简单的方法来导出此数据并将其打包到文件中.我不受任何文件类型的限制(JSON,CSV等),我只需要能够将内容导出到一个独立的(可发送的)文件中.该扩展程序仅在本地运行,用户将有权访问所有本地文件.
I have a chrome extension that saves a bunch of data to chrome.storage.local. I'm trying to find easy ways to export this data and package it into a file. I'm not constrained on what type of file it is (JSON, CSV, whatever), I just need to be able to export the contents into a standalone (and send-able) file. The extension is only run locally and the user would have access to all local files.
推荐答案
首先,您需要获取所有数据.
然后序列化结果.
最后,将其作为下载提供给用户.
First, you need to get all data.
Then serialize the result.
Finally, offer it as a download to the user.
chrome.storage.local.get(null, function(items) { // null implies all items
// Convert object to a string.
var result = JSON.stringify(items);
// Save as file
var url = 'data:application/json;base64,' + btoa(result);
chrome.downloads.download({
url: url,
filename: 'filename_of_exported_file.json'
});
});
要使用 chrome.downloads.download
方法,您需要声明<除了清单文件中的storage
权限之外,还具有c2>权限.
To use the chrome.downloads.download
method, you need to declare the "downloads"
permission in addition to the storage
permission in the manifest file.
这篇关于Chrome扩展程序:本地存储,如何导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!