aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/threads/webview.js
blob: 3092d2191a16debf8b0e54d00129be42f448e5e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

const _path = _interopRequireDefault(require('path'));

module.exports = (Ferdium, settings) => {
  // adapted from the franz-custom-website recipe, for opening
  // links according to  the user's preference (Ferdium/ext.browser)
  document.addEventListener(
    'click',
    event => {
      const link = event.target.closest('a');
      const button = event.target.closest('button');

      if (link || button) {
        const url = link
          ? link.getAttribute('href')
          : button.getAttribute('title');

        // check if the URL is relative or absolute
        if (url.startsWith('/')) {
          return;
        }

        // check if we have a valid URL that is not a script nor an image:
        if (url && url !== '#' && !Ferdium.isImage(link)) {
          event.preventDefault();
          event.stopPropagation();

          if (settings.trapLinkClicks === true) {
            window.location.href = url;
          } else {
            Ferdium.openNewWindow(url);
          }
        }
      }
    },
    true,
  );

  const getMessages = () => {
    const element = document.querySelector('a[href^="/direct/inbox"] span');
    Ferdium.setBadge(
      element && element.textContent
        ? Ferdium.safeParseInt(element.textContent)
        : 0,
    );
  };

  Ferdium.loop(getMessages);

  // https://github.com/ferdium/ferdium-recipes/blob/9d715597a600710c20f75412d3dcd8cdb7b3c39e/docs/frontend_api.md#usage-4
  // Helper that activates DarkReader and injects your darkmode.css at the same time
  Ferdium.handleDarkMode(isEnabled => {
    const url = new URL(window.location.href);
    const { searchParams } = url;
    const isDarkModeParam = searchParams.get('theme');
    let changedParams = false;

    if (isEnabled) {
      isDarkModeParam
        ? null
        : (searchParams.set('theme', 'dark'), (changedParams = true));
    } else {
      isDarkModeParam
        ? (searchParams.delete('theme', 'dark'), (changedParams = true))
        : null;
    }

    changedParams
      ? ((url.search = searchParams.toString()),
        (window.location.href = url.toString()))
      : null;
  });

  Ferdium.injectCSS(_path.default.join(__dirname, 'service.css'));
};