diff options
Diffstat (limited to 'recipes/thelounge/webview.js')
-rw-r--r-- | recipes/thelounge/webview.js | 78 |
1 files changed, 76 insertions, 2 deletions
diff --git a/recipes/thelounge/webview.js b/recipes/thelounge/webview.js index b950e8a..394c867 100644 --- a/recipes/thelounge/webview.js +++ b/recipes/thelounge/webview.js | |||
@@ -1,17 +1,91 @@ | |||
1 | |||
2 | function countsOfUnreadMessagesAfterMarker(unreadMarker) { | ||
3 | var children = unreadMarker.parentElement.childNodes; | ||
4 | var unread = 0; | ||
5 | var unreadHighlighted = 0; | ||
6 | |||
7 | for(var i = children.length-1; i >= 0; --i) { | ||
8 | if (children[i] === unreadMarker) { | ||
9 | break; | ||
10 | } | ||
11 | |||
12 | if (children[i].classList === undefined) { | ||
13 | continue; | ||
14 | } | ||
15 | |||
16 | if (children[i].classList.contains('msg')) { | ||
17 | unread++; | ||
18 | |||
19 | if (children[i].classList.contains('highlight')) { | ||
20 | unreadHighlighted++; | ||
21 | } | ||
22 | } | ||
23 | } | ||
24 | |||
25 | return [unread, unreadHighlighted]; | ||
26 | } | ||
27 | |||
1 | module.exports = Ferdi => { | 28 | module.exports = Ferdi => { |
29 | var unreadMessagesAtLastActivity = 0; | ||
30 | var unreadHighlightedMessagesAtLastActivity = 0; | ||
31 | |||
2 | const getMessages = () => { | 32 | const getMessages = () => { |
33 | // In order to get a correct tally of unread messages, we must | ||
34 | // consider both the badges on the various channels, plus the | ||
35 | // number of messages that appear after the 'unread' banner that | ||
36 | // appears in the page. In the latter case, we should ignore any | ||
37 | // messages that arrived before or while the app has focus. | ||
38 | |||
39 | let direct = 0; | ||
40 | var directElements = document.querySelectorAll('.badge.highlight'); | ||
41 | |||
42 | for (const directElement of directElements) { | ||
43 | if (directElement.textContent.length > 0) { | ||
44 | direct += Ferdi.safeParseInt(directElement.textContent); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | let indirect = 0; | ||
3 | const indirectElements = document.querySelectorAll( | 49 | const indirectElements = document.querySelectorAll( |
4 | '.badge:not(.highlight)', | 50 | '.badge:not(.highlight)', |
5 | ); | 51 | ); |
6 | const direct = document.querySelectorAll('.badge.highlight').length; | ||
7 | let indirect = 0; | ||
8 | for (const indirectElement of indirectElements) { | 52 | for (const indirectElement of indirectElements) { |
9 | if (indirectElement.textContent.length > 0) { | 53 | if (indirectElement.textContent.length > 0) { |
10 | indirect++; | 54 | indirect++; |
11 | } | 55 | } |
12 | } | 56 | } |
57 | |||
58 | const unreadMarkers = document.querySelectorAll('div.unread-marker'); | ||
59 | |||
60 | if (unreadMarkers.length > 0) { | ||
61 | var counts = countsOfUnreadMessagesAfterMarker(unreadMarkers[0]); | ||
62 | var unread = counts[0]; | ||
63 | var unreadHighlighted = counts[1]; | ||
64 | |||
65 | if (document.hasFocus()) { | ||
66 | unreadMessagesAtLastActivity = unread; | ||
67 | unreadHighlightedMessagesAtLastActivity = unreadHighlighted; | ||
68 | } | ||
69 | |||
70 | if (unread > unreadMessagesAtLastActivity) { | ||
71 | if (unreadHighlighted > 0 && unreadHighlighted > unreadHighlightedMessagesAtLastActivity) { | ||
72 | direct += (unreadHighlighted - unreadHighlightedMessagesAtLastActivity); | ||
73 | } else { | ||
74 | indirect++; | ||
75 | } | ||
76 | } | ||
77 | } else { | ||
78 | unreadMessagesAtLastActivity = 0; | ||
79 | unreadHighlightedMessagesAtLastActivity = 0; | ||
80 | } | ||
81 | |||
13 | Ferdi.setBadge(direct, indirect); | 82 | Ferdi.setBadge(direct, indirect); |
14 | }; | 83 | }; |
15 | 84 | ||
16 | Ferdi.loop(getMessages); | 85 | Ferdi.loop(getMessages); |
86 | |||
87 | // We need to monkey patch ServierWorker.postMessage so that notifications | ||
88 | // will work, and that needs to be done without context isolation: | ||
89 | const path = require('path'); | ||
90 | Ferdi.injectJSUnsafe(path.join(__dirname, 'webview-unsafe.js')); | ||
17 | }; | 91 | }; |