aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/office365-owa/webview.js
blob: 0309488c5ba7ccce222eef3b6ddc2921e0a97504 (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
module.exports = Ferdi => {
  function getMessages() {
    let directUnreadCount = 0;
    let indirectUnreadCount = 0;

    if (location.pathname.match(/\/owa/)) {
      // classic app
      directUnreadCount = parseInt(
        jQuery("span[title*='Inbox'] + div > span")
          .first()
          .text(),
        10,
      );
    } else {
      // new app
      const foldersElement = document.querySelector('div[role=tree]:nth-child(3)');
      if (foldersElement) {
        const allScreenReaders = foldersElement.querySelectorAll('span.screenReaderOnly');
        for (const child of allScreenReaders) {
          if ((child.innerText === 'unread' || child.innerText === 'item') && child.previousSibling) {
            directUnreadCount += parseInt(child.previousSibling.innerText, 10);
          }
        }
      }

      const groupsElement = document.querySelector('div[role=tree]:nth-child(4)');
      if (groupsElement) {
        const allScreenReaders = groupsElement.querySelectorAll('span.screenReaderOnly');
        for (const child of allScreenReaders) {
          if ((child.innerText === 'unread' || child.innerText === 'item') && child.previousSibling) {
            indirectUnreadCount += parseInt(child.previousSibling.innerText, 10);
          }
        }
      }
    }

    Ferdi.setBadge(directUnreadCount, indirectUnreadCount);
  }

  Ferdi.loop(getMessages);
};