Skip to content

Instantly share code, notes, and snippets.

@kirjavascript
Last active July 26, 2024 12:15
Show Gist options
  • Save kirjavascript/a9a953b933e02dc637628dbf9e0a3751 to your computer and use it in GitHub Desktop.
Save kirjavascript/a9a953b933e02dc637628dbf9e0a3751 to your computer and use it in GitHub Desktop.
dump-idb
javascript:fetch(%22https%3A%2F%2Fgist.githubusercontent.com%2Fkirjavascript%2Fa9a953b933e02dc637628dbf9e0a3751%2Fraw%2Fdump-idb.js%3F%22%2BMath.random()).then(res%3D%3Eres.text()).then(text%3D%3E(0%2Ceval)(text)).catch(console.error)
(async function() {
// Function to download data as a file
function download(data, filename, type) {
const file = new Blob([data], { type: type });
const a = document.createElement('a');
const url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
// Function to get all data from an object store
async function getAllData(db, storeName) {
return new Promise((resolve, reject) => {
const transaction = db.transaction(storeName, 'readonly');
const objectStore = transaction.objectStore(storeName);
const request = objectStore.getAll();
request.onsuccess = (event) => {
resolve(event.target.result);
};
request.onerror = (event) => {
reject(event.target.error);
};
});
}
// Function to get the names of all object stores in a database
async function getObjectStoreNames(db) {
return Array.from(db.objectStoreNames);
}
// Main function to dump the IndexedDB
async function dumpIndexedDB() {
const idbData = {};
// Open all databases
const databases = await indexedDB.databases();
for (const { name, version } of databases) {
const dbOpenRequest = indexedDB.open(name, version);
dbOpenRequest.onupgradeneeded = (event) => {
event.target.transaction.abort();
};
const db = await new Promise((resolve, reject) => {
dbOpenRequest.onsuccess = (event) => {
resolve(event.target.result);
};
dbOpenRequest.onerror = (event) => {
reject(event.target.error);
};
});
idbData[name] = {};
const storeNames = await getObjectStoreNames(db);
// Fetch data from all object stores
for (const storeName of storeNames) {
const data = await getAllData(db, storeName);
idbData[name][storeName] = data;
}
db.close();
}
return idbData;
}
try {
const idbData = await dumpIndexedDB();
const json = JSON.stringify(idbData, null, 2);
download(json, 'idb_dump.txt', 'text/plain');
} catch (error) {
console.error('Failed to dump IndexedDB:', error);
alert('Error dumping IndexedDB: ' + error.message);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment