aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/redditchat/webview.js
blob: 14f7f6cf34e0057083c3ccb5ec6303493cef47bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module.exports = Ferdi => {
  // Regular expression for (*) or (1), will extract the asterisk or the number
  // eslint-disable-next-line no-useless-escape
  const titleRegEx = /^\(([\*\d])\)/;
  const getMessages = function unreadCount() {
    let directCount = 0;
    let indirectCount = 0;

    const matchArr = document.title.match(titleRegEx);
    if (matchArr) {
      if (matchArr[1] === '*') {
        indirectCount = 1;
      } else {
        directCount = Number(matchArr[1]);
      }
    }

    Ferdi.setBadge(directCount, indirectCount);
  };

  Ferdi.loop(getMessages);
};