Skip to content

Instantly share code, notes, and snippets.

@andrei0x309
Created March 7, 2024 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrei0x309/9af01e74e26b7cd10686e9ddec1ecfa6 to your computer and use it in GitHub Desktop.
Save andrei0x309/9af01e74e26b7cd10686e9ddec1ecfa6 to your computer and use it in GitHub Desktop.
Add something to index DB, Browser don't allow direct modification of index DB from dev tools so you need to run a script like this.
const addToIndexedDB = async (dbName, version, storeName, keyPath, key, value) => {
try {
// Open a connection to the database
const dbRequest = indexedDB.open(dbName, version);
dbRequest.onupgradeneeded = (event) => {
const db = event.target.result;
// Create an object store if it doesn't exist
db.createObjectStore(storeName, { keyPath });
};
// Handle success and failure
dbRequest.onsuccess = (event) => {
console.log("Data added successfully:", event.target.result);
const db = dbRequest.result
const transaction = db.transaction(storeName, "readwrite");
const store = transaction.objectStore(storeName);
// Add the key-value pair to the object store
const request = store.put( value, key);
};
dbRequest.onerror = (event) => {
console.error("Error adding data:", event.target.error);
};
await transaction.complete; // Ensure transaction completion
} catch (error) {
console.error("Error accessing IndexedDB:", error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment