aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/tt-rss/webview.js
blob: e0581ced32c9e3bbae3003fa450705c8983d8c15 (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
function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

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

module.exports = Ferdium => {
  const _notificationCategory = new URL(window.location.href).searchParams.get(
    'ferdiumNotificationCategory',
  );

  const countMessagesUnderCategory = notificationCategory => {
    const elements = document.querySelectorAll('.dijitTreeIsRoot');
    let directMessages = 0;
    let indirectMessages = 0;
    for (const element of elements) {
      const label = element.querySelectorAll('.dijitTreeLabel')[0];
      const unreadNode = element.querySelectorAll('.unread')[0];
      const unreadAmount = Ferdium.safeParseInt(unreadNode.textContent);
      if (label.textContent === notificationCategory) {
        directMessages += unreadAmount;
      } else {
        indirectMessages += unreadAmount;
      }
    }
    Ferdium.setBadge(directMessages, indirectMessages);
  };

  const _countMessagesFromTitle = () => {
    const match = document.title.match(/^\((\d+)\) Tiny Tiny RSS$/);
    const count = match !== null && match.length > 0 ? match[1] : 0;
    return Ferdium.safeParseInt(count);
  };

  const countMessages = () => {
    Ferdium.setBadge(_countMessagesFromTitle());
  };

  const getMessages = function getMessages() {
    if (_notificationCategory) {
      countMessagesUnderCategory(_notificationCategory);
    } else {
      countMessages();
    }
  };

  const loopFunc = () => {
    getMessages();
  };

  Ferdium.loop(loopFunc);

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