咨询Firefox中获取所有IndexedDB名称的有效方法
Let's break down how to solve this issue, since the approach varies a bit between browsers and versions:
1. Modern Browsers (Chrome ≥47, Firefox ≥108) – Use the Standard API
The window.indexedDB.databases() method is now part of the official IndexedDB spec, but it's asynchronous (returns a Promise) – this might be why you ran into issues if you tried calling it like a synchronous function. Here's the correct usage:
async function fetchAllIndexedDBNames() { try { // Get the list of databases (returns an array of {name: string, version: number} objects) const dbList = await window.indexedDB.databases(); // Extract just the names const dbNames = dbList.map(db => db.name); console.log("All IndexedDB databases:", dbNames); return dbNames; } catch (error) { console.error("Failed to fetch IndexedDB databases:", error); return []; } } // Call the function fetchAllIndexedDBNames();
If you're getting the error window.indexedDB.databases() is not a function in Firefox, double-check your browser version – Firefox only added support for this method in version 108. Upgrading your browser should fix this.
2. Older Firefox Versions (<108) – Workarounds
Unfortunately, for older Firefox versions, there's no official, non-privileged way to enumerate all IndexedDB databases from a regular web page. The IndexedDB spec doesn't expose this capability for privacy/security reasons, and the old prefixed methods like webkitdatabases() or mozwebkitdatabases() were long ago deprecated and removed.
Your best options here are:
- Upgrade Firefox: This is the most straightforward fix, as modern Firefox fully supports the standard
databases()method. - Track Database Names Locally: If you control the application that creates the IndexedDB databases, store a list of created database names in
localStorageor a dedicated "metadata" IndexedDB. For example:// When creating a new database function createDB(dbName) { // Store the name in localStorage const existingDBs = JSON.parse(localStorage.getItem("indexedDBList")) || []; if (!existingDBs.includes(dbName)) { existingDBs.push(dbName); localStorage.setItem("indexedDBList", JSON.stringify(existingDBs)); } // Proceed with opening the database const request = window.indexedDB.open(dbName); // ... rest of your DB setup code } // Later, retrieve the list function getStoredDBList() { return JSON.parse(localStorage.getItem("indexedDBList")) || []; }
3. Why IDBObjectStore/IDBDatabase Didn't Help
Interfaces like IDBObjectStore and IDBDatabase are for interacting with already opened databases. You need to know the database name upfront to open it, so they can't be used to discover unknown database names.
内容的提问来源于stack exchange,提问作者Rajkumar

