Skip to content

Instantly share code, notes, and snippets.

@tech234a
Last active January 8, 2024 00:32
Show Gist options
  • Save tech234a/0e678de8665ad0bff959fb34721f442b to your computer and use it in GitHub Desktop.
Save tech234a/0e678de8665ad0bff959fb34721f442b to your computer and use it in GitHub Desktop.
Export indexeddb for a website to JSON. Use by pasting into the browser console of the website. Should work in Chromium-based browsers. Note: this code was generated using ChatGPT and may contain bugs.
// WARNING: this code was generated using ChatGPT. Use at your own risk.
function exportIndexedDBToJSON() {
indexedDB.databases().then(async (databases) => {
for (const dbInfo of databases) {
const dbName = dbInfo.name;
try {
const db = await new Promise((resolve, reject) => {
const request = indexedDB.open(dbName, dbInfo.version);
request.onsuccess = (event) => resolve(event.target.result);
request.onerror = (event) => reject(event.error);
});
const objectStoreNames = Array.from(db.objectStoreNames);
const databaseData = {};
for (const storeName of objectStoreNames) {
const transaction = db.transaction([storeName], 'readonly');
const objectStore = transaction.objectStore(storeName);
const records = await new Promise((resolve, reject) => {
const getAllRequest = objectStore.getAll();
getAllRequest.onsuccess = (event) => resolve(event.target.result);
getAllRequest.onerror = (event) => reject(event.error);
});
records.forEach((record) => {
for (const key in record) {
const value = record[key];
if (value instanceof Blob) {
const reader = new FileReader();
reader.onload = (blobEvent) => {
record[key] = blobEvent.target.result.split(',')[1];
};
reader.readAsDataURL(value);
}
}
});
databaseData[storeName] = records;
console.log(`Exported data from ${dbName}.${storeName}:`, records);
}
// Log the exported data with object store names
if (Object.keys(databaseData).length > 0) {
console.log(`Exported data from ${dbName}:`, databaseData);
// Create a Blob containing the JSON data
const blob = new Blob([JSON.stringify(databaseData, null, 2)], { type: 'application/json' });
// Create a download link
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = `${dbName}_export.json`;
// Trigger the download
downloadLink.click();
}
} catch (error) {
console.error(error);
}
}
});
}
exportIndexedDBToJSON();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment