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.
Asked
Active
Viewed 2.2k times
2 Answers
22
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'
});
});
To use the chrome.downloads.download
method, you need to declare the "downloads"
permission in addition to the storage
permission in the manifest file.

Rob W
- 341,306
- 83
- 791
- 678
-
Thanks for the answer. Apologies I'm a bit delayed in verifying. As soon as I'm able, and assuming this is the best route, I'll mark correct. Thank you! – ZAR Apr 20 '14 at 22:36
-
1I am getting chrome is not define what i should import in my service . file ? – Nirdesh Kumar Choudhary Dec 11 '18 at 05:26
-
3How to restore this JSON file to local storage? Thank – mangovn Mar 26 '20 at 21:18
0
You should look here: https://groups.google.com/a/chromium.org/forum/#!topic/chromium-extensions/AzO_taH2b7U
It shows exporting chrome local storage to JSON.
Hope it helps

unit998x
- 56
- 4
-
1That is not [`chrome.storage`](https://developer.chrome.com/extensions/storage), but `localStorage`. – Rob W Apr 19 '14 at 08:22
-
@unit998x, thanks for the response. I've seen this post before and it works fine to display the contents in JSON format. But this doesn't prompt the user to download the output. I've heard someone recommend to then using HTML5's file handling, but it would be easier if there is a chrome API to handle this (as it is already stored in chrome.storage). Thanks for the help! – ZAR Apr 20 '14 at 22:39