diff options
Diffstat (limited to 'recipes/threads/webview.js')
-rw-r--r-- | recipes/threads/webview.js | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/recipes/threads/webview.js b/recipes/threads/webview.js new file mode 100644 index 0000000..3092d21 --- /dev/null +++ b/recipes/threads/webview.js | |||
@@ -0,0 +1,78 @@ | |||
1 | function _interopRequireDefault(obj) { | ||
2 | return obj && obj.__esModule ? obj : { default: obj }; | ||
3 | } | ||
4 | |||
5 | const _path = _interopRequireDefault(require('path')); | ||
6 | |||
7 | module.exports = (Ferdium, settings) => { | ||
8 | // adapted from the franz-custom-website recipe, for opening | ||
9 | // links according to the user's preference (Ferdium/ext.browser) | ||
10 | document.addEventListener( | ||
11 | 'click', | ||
12 | event => { | ||
13 | const link = event.target.closest('a'); | ||
14 | const button = event.target.closest('button'); | ||
15 | |||
16 | if (link || button) { | ||
17 | const url = link | ||
18 | ? link.getAttribute('href') | ||
19 | : button.getAttribute('title'); | ||
20 | |||
21 | // check if the URL is relative or absolute | ||
22 | if (url.startsWith('/')) { | ||
23 | return; | ||
24 | } | ||
25 | |||
26 | // check if we have a valid URL that is not a script nor an image: | ||
27 | if (url && url !== '#' && !Ferdium.isImage(link)) { | ||
28 | event.preventDefault(); | ||
29 | event.stopPropagation(); | ||
30 | |||
31 | if (settings.trapLinkClicks === true) { | ||
32 | window.location.href = url; | ||
33 | } else { | ||
34 | Ferdium.openNewWindow(url); | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | }, | ||
39 | true, | ||
40 | ); | ||
41 | |||
42 | const getMessages = () => { | ||
43 | const element = document.querySelector('a[href^="/direct/inbox"] span'); | ||
44 | Ferdium.setBadge( | ||
45 | element && element.textContent | ||
46 | ? Ferdium.safeParseInt(element.textContent) | ||
47 | : 0, | ||
48 | ); | ||
49 | }; | ||
50 | |||
51 | Ferdium.loop(getMessages); | ||
52 | |||
53 | // https://github.com/ferdium/ferdium-recipes/blob/9d715597a600710c20f75412d3dcd8cdb7b3c39e/docs/frontend_api.md#usage-4 | ||
54 | // Helper that activates DarkReader and injects your darkmode.css at the same time | ||
55 | Ferdium.handleDarkMode(isEnabled => { | ||
56 | const url = new URL(window.location.href); | ||
57 | const { searchParams } = url; | ||
58 | const isDarkModeParam = searchParams.get('theme'); | ||
59 | let changedParams = false; | ||
60 | |||
61 | if (isEnabled) { | ||
62 | isDarkModeParam | ||
63 | ? null | ||
64 | : (searchParams.set('theme', 'dark'), (changedParams = true)); | ||
65 | } else { | ||
66 | isDarkModeParam | ||
67 | ? (searchParams.delete('theme', 'dark'), (changedParams = true)) | ||
68 | : null; | ||
69 | } | ||
70 | |||
71 | changedParams | ||
72 | ? ((url.search = searchParams.toString()), | ||
73 | (window.location.href = url.toString())) | ||
74 | : null; | ||
75 | }); | ||
76 | |||
77 | Ferdium.injectCSS(_path.default.join(__dirname, 'service.css')); | ||
78 | }; | ||