Created
March 4, 2022 15:43
-
-
Save mcornella/96c53dcea327ed3d243951ad96a277d5 to your computer and use it in GitHub Desktop.
Playing with IndexedDB on WhatsApp Web
This file contains hidden or 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
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