diff options
author | 2021-10-24 10:38:34 +0200 | |
---|---|---|
committer | 2021-10-24 10:38:34 +0200 | |
commit | 4a6fcf1a35f6d26f7c0d04c9769ca101df8f6ce8 (patch) | |
tree | ce52dc6acfa24a50c3bfbf238c4ec059f5229e0c /recipes/telegram/webview.js | |
parent | Add setDialogTitle feature to api, WhatsApp and Telegram (#750) (diff) | |
download | ferdium-recipes-4a6fcf1a35f6d26f7c0d04c9769ca101df8f6ce8.tar.gz ferdium-recipes-4a6fcf1a35f6d26f7c0d04c9769ca101df8f6ce8.tar.zst ferdium-recipes-4a6fcf1a35f6d26f7c0d04c9769ca101df8f6ce8.zip |
feat: support telegram count for both webk and webz (#751)
Diffstat (limited to 'recipes/telegram/webview.js')
-rw-r--r-- | recipes/telegram/webview.js | 67 |
1 files changed, 57 insertions, 10 deletions
diff --git a/recipes/telegram/webview.js b/recipes/telegram/webview.js index 7d2c8e3..d63282e 100644 --- a/recipes/telegram/webview.js +++ b/recipes/telegram/webview.js | |||
@@ -1,5 +1,3 @@ | |||
1 | // Code copied from: https://gitlab.com/gortega4/ferdi_recipes | ||
2 | |||
3 | const _path = _interopRequireDefault(require('path')); | 1 | const _path = _interopRequireDefault(require('path')); |
4 | 2 | ||
5 | function _interopRequireDefault(obj) { | 3 | function _interopRequireDefault(obj) { |
@@ -7,27 +5,76 @@ function _interopRequireDefault(obj) { | |||
7 | } | 5 | } |
8 | 6 | ||
9 | module.exports = Ferdi => { | 7 | module.exports = Ferdi => { |
10 | const getMessages = () => { | 8 | const telegramVersion = document |
11 | let direct = 0; | 9 | .querySelector('meta[name="application-name"]') |
12 | let indirect = 0; | 10 | ?.getAttribute('content'); |
13 | const elements = document.querySelectorAll('.rp'); | 11 | |
12 | const isWebZ = telegramVersion?.includes('WebZ'); | ||
13 | const isWebK = telegramVersion?.includes('WebK'); | ||
14 | |||
15 | // There are two different Telegram versions for internal competition | ||
16 | // Read more: https://bugs.telegram.org/c/4002/public | ||
17 | const webZCount = () => { | ||
18 | let directCount = 0; | ||
19 | let groupCount = 0; | ||
20 | |||
21 | const directCountSelector = document.querySelectorAll( | ||
22 | '.chat-list .ListItem.private .Badge.unread:not(.muted)', | ||
23 | ); | ||
24 | const groupCountSelector = document.querySelectorAll( | ||
25 | '.chat-list .ListItem.group .Badge.unread:not(.muted)', | ||
26 | ); | ||
27 | |||
28 | for (const badge of directCountSelector) { | ||
29 | directCount += Ferdi.safeParseInt(badge.textContent); | ||
30 | } | ||
31 | |||
32 | for (const badge of groupCountSelector) { | ||
33 | groupCount += Ferdi.safeParseInt(badge.textContent); | ||
34 | } | ||
35 | |||
36 | Ferdi.setBadge(directCount, groupCount); | ||
37 | }; | ||
38 | |||
39 | const webKCount = () => { | ||
40 | let directCount = 0; | ||
41 | let groupCount = 0; | ||
42 | |||
43 | const elements = document.querySelectorAll('.rp:not(.is-muted)'); | ||
44 | |||
14 | for (const element of elements) { | 45 | for (const element of elements) { |
15 | const subtitleBadge = element.querySelector('.dialog-subtitle-badge'); | 46 | const subtitleBadge = element.querySelector('.dialog-subtitle-badge'); |
47 | |||
16 | if (subtitleBadge) { | 48 | if (subtitleBadge) { |
17 | const parsedValue = Ferdi.safeParseInt(subtitleBadge.textContent); | 49 | const parsedValue = Ferdi.safeParseInt(subtitleBadge.textContent); |
50 | |||
18 | if (element.dataset.peerId > 0) { | 51 | if (element.dataset.peerId > 0) { |
19 | direct += parsedValue; | 52 | directCount += parsedValue; |
20 | } else { | 53 | } else { |
21 | indirect += parsedValue; | 54 | groupCount += parsedValue; |
22 | } | 55 | } |
23 | } | 56 | } |
24 | } | 57 | } |
25 | 58 | ||
26 | Ferdi.setBadge(direct, indirect); | 59 | Ferdi.setBadge(directCount, groupCount); |
60 | }; | ||
61 | |||
62 | const getMessages = () => { | ||
63 | if (isWebZ) { | ||
64 | webZCount(); | ||
65 | } else if (isWebK) { | ||
66 | webKCount(); | ||
67 | } | ||
27 | }; | 68 | }; |
28 | 69 | ||
29 | const getActiveDialogTitle = () => { | 70 | const getActiveDialogTitle = () => { |
30 | const element = document.querySelector('.top .peer-title'); | 71 | let element; |
72 | |||
73 | if (isWebZ) { | ||
74 | element = document.querySelector('.chat-list .ListItem .title > h3'); | ||
75 | } else if (isWebK) { | ||
76 | element = document.querySelector('.top .peer-title'); | ||
77 | } | ||
31 | 78 | ||
32 | Ferdi.setDialogTitle(element ? element.textContent : ''); | 79 | Ferdi.setDialogTitle(element ? element.textContent : ''); |
33 | }; | 80 | }; |