diff options
author | 2022-11-19 10:11:54 +0100 | |
---|---|---|
committer | 2022-11-19 09:11:54 +0000 | |
commit | 8e2492648b7d320f417ac416106b0baf8568f8ed (patch) | |
tree | aba95729623a2670374cd0e72748a73d009a364c /recipes/whatsapp/webview.js | |
parent | Yahoo Mail Recipe Icon Update (#225) (diff) | |
download | ferdium-recipes-8e2492648b7d320f417ac416106b0baf8568f8ed.tar.gz ferdium-recipes-8e2492648b7d320f417ac416106b0baf8568f8ed.tar.zst ferdium-recipes-8e2492648b7d320f417ac416106b0baf8568f8ed.zip |
fix: Use IndexedDB for updating whatsapp unread message count (#240)
Co-authored-by: Victor Bonnelle <victor.bonnelle@protonmail.com>
Diffstat (limited to 'recipes/whatsapp/webview.js')
-rw-r--r-- | recipes/whatsapp/webview.js | 64 |
1 files changed, 37 insertions, 27 deletions
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 @@ | |||
1 | const _path = _interopRequireDefault(require('path')); | 1 | const _path = _interopRequireDefault(require('path')); |
2 | 2 | ||
3 | function _interopRequireDefault(obj) { | 3 | function _interopRequireDefault(obj) { |
4 | return obj && obj.__esModule ? obj : { default: obj }; | 4 | return obj && obj.__esModule ? obj : {default: obj}; |
5 | } | ||
6 | |||
7 | let getMessages = () => { | ||
8 | /* stub until db is connected*/ | ||
5 | } | 9 | } |
6 | 10 | ||
7 | module.exports = Ferdium => { | 11 | module.exports = Ferdium => { |
8 | const getMessages = () => { | 12 | const request = window.indexedDB.open("model-storage"); |
9 | let count = 0; | 13 | request.onsuccess = () => { |
10 | let indirectCount = 0; | 14 | const db = request.result; |
11 | 15 | ||
12 | const parentChatElem = [ | 16 | getMessages = () => { |
13 | ...document.querySelectorAll('div[aria-label]'), | 17 | let unreadCount = 0; |
14 | ].sort((a, b) => (a.offsetHeight < b.offsetHeight ? 1 : -1))[0]; | 18 | let unreadMutedCount = 0; |
15 | if (!parentChatElem) { | 19 | |
16 | return; | 20 | const txn = db.transaction('chat', 'readonly'); |
17 | } | 21 | const store = txn.objectStore('chat'); |
18 | 22 | ||
19 | const unreadSpans = parentChatElem.querySelectorAll('span[aria-label]'); | 23 | const query = store.getAll(); |
20 | for (const unreadElem of unreadSpans) { | 24 | |
21 | const countValue = Ferdium.safeParseInt(unreadElem.textContent); | 25 | query.onsuccess = (event) => { |
22 | if (countValue > 0) { | 26 | for (const chat of event.target.result) { |
23 | if ( | 27 | if (chat.unreadCount > 0) { |
24 | !unreadElem.parentNode.previousSibling || | 28 | if (chat.muteExpiration === 0) { |
25 | unreadElem.parentNode.previousSibling.querySelectorAll( | 29 | unreadCount += chat.unreadCount; |
26 | '[data-icon=muted]', | 30 | } else { |
27 | ).length === 0 | 31 | unreadMutedCount += chat.unreadCount; |
28 | ) { | 32 | } |
29 | count += countValue; | 33 | } |
30 | } else { | ||
31 | indirectCount += countValue; | ||
32 | } | 34 | } |
33 | } | ||
34 | } | ||
35 | 35 | ||
36 | Ferdium.setBadge(count, indirectCount); | 36 | Ferdium.setBadge(unreadCount, unreadMutedCount); |
37 | }; | ||
38 | |||
39 | query.addEventListener('error', (event) => { | ||
40 | console.error("Loading data from database failed:", event); | ||
41 | }) | ||
42 | } | ||
37 | }; | 43 | }; |
38 | 44 | ||
45 | request.addEventListener('error', (event) => { | ||
46 | console.error("Opening model-storage database failed:", event); | ||
47 | }) | ||
48 | |||
39 | // inject webview hacking script | 49 | // inject webview hacking script |
40 | Ferdium.injectJSUnsafe(_path.default.join(__dirname, 'webview-unsafe.js')); | 50 | Ferdium.injectJSUnsafe(_path.default.join(__dirname, 'webview-unsafe.js')); |
41 | 51 | ||