aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/office365-owa/webview.js
blob: 17649fd507fa096e58e5f745044b09ac0f1e61e2 (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
module.exports = (Ferdi, settings) => {
  const collectCounts = selector => {
    let unreadCount = 0;
    const foldersElement = document.querySelector(selector);
    if (foldersElement) {
      const allScreenReaders = foldersElement.querySelectorAll(
        'span.screenReaderOnly',
      );
      for (const child of allScreenReaders) {
        if (child.previousSibling) {
          unreadCount += Ferdi.safeParseInt(child.previousSibling.textContent);
        }
      }
    }
    return unreadCount;
  };

  const getMessages = () => {
    let directUnreadCount = 0;
    let indirectUnreadCount = 0;

    if (/\/owa/.test(location.pathname)) {
      // classic app
      directUnreadCount = Ferdi.safeParseInt(
        document.querySelectorAll("span[title*='Inbox'] + div > span")[0]
          .textContent,
      );
    } else {
      // new app
      directUnreadCount =
        settings.onlyShowFavoritesInUnreadCount === true
          ? collectCounts('div[role=tree]:nth-child(2)')
          : collectCounts('div[role=tree]:nth-child(3)');

      indirectUnreadCount = collectCounts('div[role=tree]:nth-child(4)'); // groups
    }

    Ferdi.setBadge(directUnreadCount, indirectUnreadCount);
  };

  Ferdi.loop(getMessages);
};