Skip to content

Instantly share code, notes, and snippets.

@sergiitk
Last active January 20, 2025 20:08
Show Gist options
  • Save sergiitk/4b099ef3378f088c75566710be0342d7 to your computer and use it in GitHub Desktop.
Save sergiitk/4b099ef3378f088c75566710be0342d7 to your computer and use it in GitHub Desktop.
ext-export-import
// run in chrome -> sources -> snippets at
// chrome-extension://[extension-id]/window.html
async function storageGet(storageType, space = 2) {
let storage_fn;
if (storageType === 'local') {
storage_fn = chrome.storage.local;
} else if (storageType === 'sync') {
storage_fn = chrome.storage.sync;
} else {
throw new Error('Wrong storage type');
}
// https://developer.chrome.com/docs/extensions/reference/api/storage#method-StorageArea-get
const items = await storage_fn.get(null);
const result_json = JSON.stringify(items, null, space);
console.log(result_json);
}
await storageGet('sync', space=0);
// no guarantees things won't break, use at your discretion
// run in chrome -> sources -> snippets at
// chrome-extension://[extension-id]/window.html
async function storageSet(storageType, json) {
let storage_fn;
if (storageType === 'local') {
storage_fn = chrome.storage.local;
} else if (storageType === 'sync') {
storage_fn = chrome.storage.sync;
} else {
throw new Error('Wrong storage type');
}
if (!json || !Object.keys(json).length) {
throw new Error('Empty json input');
}
// https://developer.chrome.com/docs/extensions/reference/api/storage#method-StorageArea-set
// uncomment when reaady
// await storage_fn.set(json);
console.log("Done")
}
// paste json from the storageGet
const json = {};
await storageSet('sync', json);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment