diff options
Diffstat (limited to 'uncompressed/mastodon/webview.js')
-rw-r--r-- | uncompressed/mastodon/webview.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/uncompressed/mastodon/webview.js b/uncompressed/mastodon/webview.js new file mode 100644 index 0000000..dcf715e --- /dev/null +++ b/uncompressed/mastodon/webview.js | |||
@@ -0,0 +1,82 @@ | |||
1 | //'use strict'; | ||
2 | |||
3 | const { ipcRenderer } = require('electron'); | ||
4 | |||
5 | const BADGE_AUTO_CLEAR_DELAY = 10000; // delay time for badge clear auto | ||
6 | |||
7 | // MONKEY PATCH: | ||
8 | // fix 'Uncaught TypeError: c.addEventListener is not a function' | ||
9 | // from mastdon code | ||
10 | // see https://github.com/tootsuite/mastodon/blob/c1a41181c52216de9ebeecebf418e6d50172139b/app/javascript/mastodon/actions/notifications.js#L60 | ||
11 | if (!Notification.prototype.addEventListener) { | ||
12 | Notification.prototype.addEventListener = function(){}; | ||
13 | } | ||
14 | |||
15 | module.exports = (Franz, service_) => { | ||
16 | |||
17 | let service = service_; | ||
18 | |||
19 | // save service instance identify | ||
20 | const serviceId = service.id; | ||
21 | |||
22 | // check if this service is active | ||
23 | let activeUpdated = false; | ||
24 | let isActive = service.isActive; | ||
25 | |||
26 | ipcRenderer.on('-service-update', (sender, serviceNew) => { | ||
27 | service = serviceNew; | ||
28 | !activeUpdated && (activeUpdated = isActive != service.isActive); | ||
29 | isActive = service.isActive; | ||
30 | }); | ||
31 | |||
32 | //ipcRenderer.on('settings-update', (sender, settings) => { | ||
33 | // const nextIsActive = serviceId == settings.activeService; | ||
34 | // !activeUpdated && (activeUpdated = isActive != nextIsActive); | ||
35 | // isActive = nextIsActive; | ||
36 | //}); | ||
37 | |||
38 | let replyCount = 0; | ||
39 | let limitBadgeClear = false; | ||
40 | |||
41 | const getMessages = function getMessages() { | ||
42 | const activeUpdated_ = activeUpdated; activeUpdated = false; | ||
43 | |||
44 | // check and redirect to signin page when not loggdin | ||
45 | if (window.location && /\/about$/.test(window.location.pathname)) { | ||
46 | const hasSigninLink = !!document.querySelector('[href$="/auth/sign_in"]'); | ||
47 | if (hasSigninLink) { | ||
48 | window.location.pathname = '/auth/sign_in'; | ||
49 | return; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | // clear replay badge when ... | ||
54 | if (replyCount) { | ||
55 | // this service actived | ||
56 | if (activeUpdated_ && isActive) { | ||
57 | replyCount = 0; | ||
58 | } | ||
59 | // timeout | ||
60 | if (!isActive && limitBadgeClear) { | ||
61 | limitBadgeClear = false; | ||
62 | } | ||
63 | if (isActive && false !== limitBadgeClear && limitBadgeClear <= Date.now()) { | ||
64 | replyCount = 0; | ||
65 | limitBadgeClear = false; | ||
66 | } | ||
67 | } | ||
68 | |||
69 | Franz.setBadge(replyCount); | ||
70 | }; | ||
71 | |||
72 | Franz.loop(getMessages); | ||
73 | |||
74 | Franz.onNotify(notification => { | ||
75 | // increment reply count for badge | ||
76 | ++replyCount; | ||
77 | limitBadgeClear = Date.now() + BADGE_AUTO_CLEAR_DELAY; | ||
78 | // | ||
79 | return notification; | ||
80 | |||
81 | }); | ||
82 | }; \ No newline at end of file | ||