此脚本打印 IndexedDB 的内容。
当我在网站控制台中运行此脚本时,它会显示:
我尝试使用 IDBDatabase(),但它说“非法构造函数”。
你能指出我的错误吗?
代码如下:
const dumpIndexedDB = (dbName) => {
const DB_VERSION = 1;
const req = indexedDB.open(dbName, DB_VERSION);
req.onsuccess = function () {
const db = req.result;
const objectStoreNames = db.objectStoreNames || [];
console.log(`[*] Database: ${dbName}`);
Array.from(objectStoreNames).forEach((storeName) => {
const txn = db.transaction(storeName, "readonly");
const objectStore = txn.objectStore(storeName);
console.log(`\t[+] ObjectStore: ${storeName}`);
// Print all entries in objectStore with name `storeName`
objectStore.getAll().onsuccess = (event) => {
const items = event.target.result || [];
items.forEach((item) => console.log(`\t\t[-] `, item));
};
});
};
};
indexedDB
.databases()
.then((dbs) => dbs.forEach((db) => dumpIndexedDB(db.name)));