Skip to content

Instantly share code, notes, and snippets.

@mcornella
Created March 4, 2022 15:43
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 mcornella/96c53dcea327ed3d243951ad96a277d5 to your computer and use it in GitHub Desktop.
Save mcornella/96c53dcea327ed3d243951ad96a277d5 to your computer and use it in GitHub Desktop.
Playing with IndexedDB on WhatsApp Web
function openDatabase(...args) {
return new Promise((resolve, reject) => {
const conn = indexedDB.open(...args)
conn.onsuccess = e => resolve({ db: e.target.result })
conn.onerror = e => reject(e.target.result)
})
}
function getContactTransaction({ db }) {
const store = db.transaction(['contact']).objectStore('contact')
return new Promise((resolve, reject) => {
const request = store.get('<intl-phone-number>@s.whatsapp.net')
request.onsuccess = e => resolve({ db, contact: e.target.result })
request.onerror = e => reject(e.target.result)
})
}
function updateContactTransaction({ db, contact }) {
const store = db.transaction(['contact'], 'readwrite').objectStore('contact')
return new Promise((resolve, reject) => {
const updatedContact = {
id: contact.id,
isAddressBookContact: 1,
isContactSyncCompleted: 1,
pushname: "Name",
name: "Name",
shortName: "Name",
type: "in"
}
const request = store.put(updatedContact)
request.onsuccess = e => resolve(e.target.result)
request.onerror = e => reject(e.target.result)
})
}
void openDatabase('model-storage', 810)
.then(getContactTransaction)
.then(updateContactTransaction)
.then(updatedKey => console.log(updatedKey))
.catch(err => console.error(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment