Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension: Local Storage, how to export

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.

like image 319
ZAR Avatar asked Apr 18 '14 19:04

ZAR


People also ask

How do I export Chrome extensions?

Export Chrome Extensions as CRX Files If you want to export Chrome extensions manually, you have to enable 'Developer mode' in the browser and pack the extension in a CRX file. CRX is a file that Chrome automatically downloads and installs when you add an extension.

Where is Chrome local storage stored?

Google Chrome records Web storage data in a SQLite file in the user's profile. The subfolder containing this file is " \AppData\Local\Google\Chrome\User Data\Default\Local Storage " on Windows, and " ~/Library/Application Support/Google/Chrome/Default/Local Storage " on macOS.

Can Chrome extensions use localStorage?

One way to share data between a background script and the other scripts that make up the extension is to save the data to a location which is accessible to all the scripts in the extension. We can use the browser localStorage API or chrome. storage which is specific to Chrome extensions.


1 Answers

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.

like image 187
Rob W Avatar answered Sep 20 '22 10:09

Rob W