From 8e2492648b7d320f417ac416106b0baf8568f8ed Mon Sep 17 00:00:00 2001 From: Raphael Jenni Date: Sat, 19 Nov 2022 10:11:54 +0100 Subject: fix: Use IndexedDB for updating whatsapp unread message count (#240) Co-authored-by: Victor Bonnelle --- recipes/whatsapp/package.json | 2 +- recipes/whatsapp/webview.js | 64 +++++++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 28 deletions(-) (limited to 'recipes/whatsapp') diff --git a/recipes/whatsapp/package.json b/recipes/whatsapp/package.json index a708db4..e4701a5 100644 --- a/recipes/whatsapp/package.json +++ b/recipes/whatsapp/package.json @@ -1,7 +1,7 @@ { "id": "whatsapp", "name": "WhatsApp", - "version": "3.4.6", + "version": "3.4.7", "license": "MIT", "config": { "serviceURL": "https://web.whatsapp.com", diff --git a/recipes/whatsapp/webview.js b/recipes/whatsapp/webview.js index 77c4ebb..8a5f830 100644 --- a/recipes/whatsapp/webview.js +++ b/recipes/whatsapp/webview.js @@ -1,41 +1,51 @@ const _path = _interopRequireDefault(require('path')); function _interopRequireDefault(obj) { - return obj && obj.__esModule ? obj : { default: obj }; + return obj && obj.__esModule ? obj : {default: obj}; +} + +let getMessages = () => { + /* stub until db is connected*/ } module.exports = Ferdium => { - const getMessages = () => { - let count = 0; - let indirectCount = 0; - - const parentChatElem = [ - ...document.querySelectorAll('div[aria-label]'), - ].sort((a, b) => (a.offsetHeight < b.offsetHeight ? 1 : -1))[0]; - if (!parentChatElem) { - return; - } + const request = window.indexedDB.open("model-storage"); + request.onsuccess = () => { + const db = request.result; + + getMessages = () => { + let unreadCount = 0; + let unreadMutedCount = 0; + + const txn = db.transaction('chat', 'readonly'); + const store = txn.objectStore('chat'); - const unreadSpans = parentChatElem.querySelectorAll('span[aria-label]'); - for (const unreadElem of unreadSpans) { - const countValue = Ferdium.safeParseInt(unreadElem.textContent); - if (countValue > 0) { - if ( - !unreadElem.parentNode.previousSibling || - unreadElem.parentNode.previousSibling.querySelectorAll( - '[data-icon=muted]', - ).length === 0 - ) { - count += countValue; - } else { - indirectCount += countValue; + const query = store.getAll(); + + query.onsuccess = (event) => { + for (const chat of event.target.result) { + if (chat.unreadCount > 0) { + if (chat.muteExpiration === 0) { + unreadCount += chat.unreadCount; + } else { + unreadMutedCount += chat.unreadCount; + } + } } - } - } - Ferdium.setBadge(count, indirectCount); + Ferdium.setBadge(unreadCount, unreadMutedCount); + }; + + query.addEventListener('error', (event) => { + console.error("Loading data from database failed:", event); + }) + } }; + request.addEventListener('error', (event) => { + console.error("Opening model-storage database failed:", event); + }) + // inject webview hacking script Ferdium.injectJSUnsafe(_path.default.join(__dirname, 'webview-unsafe.js')); -- cgit v1.2.3-54-g00ecf