Last active
August 28, 2020 13:34
-
-
Save niradler/2cf33ddf6a045ecb05fe5959c55a55f1 to your computer and use it in GitHub Desktop.
backup memex data using chrome snippet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dbName = 'memex'; | |
var version = 1; | |
var conn = indexedDB.open(dbName, version) // change the name and version as needed | |
function exportToJson(idbDatabase) { | |
return new Promise((resolve, reject) => { | |
const exportObject = {} | |
if (idbDatabase.objectStoreNames.length === 0) { | |
resolve(JSON.stringify(exportObject)) | |
} else { | |
const transaction = idbDatabase.transaction( | |
idbDatabase.objectStoreNames, | |
'readonly' | |
) | |
transaction.addEventListener('error', reject) | |
for (const storeName of idbDatabase.objectStoreNames) { | |
const allObjects = [] | |
transaction | |
.objectStore(storeName) | |
.openCursor() | |
.addEventListener('success', event => { | |
const cursor = event.target.result | |
if (cursor) { | |
// Cursor holds value, put it into store data | |
allObjects.push(cursor.value) | |
cursor.continue() | |
} else { | |
// No more values, store is done | |
exportObject[storeName] = allObjects | |
// Last store was handled | |
if ( | |
idbDatabase.objectStoreNames.length === | |
Object.keys(exportObject).length | |
) { | |
resolve(JSON.stringify(exportObject)) | |
} | |
} | |
}) | |
} | |
} | |
}) | |
} | |
conn.onsuccess = e => { | |
var database = e.target.result | |
exportToJson(database).then(console.log).catch(console.error) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment