aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/hangoutschat/webview.js
blob: e2e5912ec3b077ab18549a71dcdc4e3a1d8962fe (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 => {
  // class corresponding to the red badge that is visible for direct messages
  const directMessageSelector = 'div.V6.CL.su.ahD.X9.Y2 span.akt span.XU';

  // class corresponding to the bold text that is visible for room messages
  const indirectMessageSelector = 'div.V6.CL.V2.X9.Y2 span.akt span.XU';

  const getMessages = () => {
    // get unread direct messages
    let directCount;
    let indirectCount;

    const directCountSelector = document.querySelector(directMessageSelector);
    if (directCountSelector) {
      directCount = Number(directCountSelector.textContent);
    }

    // get unread indirect messages
    const indirectCountSelector = document.querySelector(
      indirectMessageSelector,
    );
    if (indirectCountSelector) {
      indirectCount = Number(indirectCountSelector.textContent);
    }

    // set Ferdi badge
    Ferdi.setBadge(directCount, indirectCount);
  };

  Ferdi.loop(getMessages);

  document.addEventListener('click', e => {
    // @ts-ignore
    const { tagName, target, href } = e.target;

    if (tagName === 'A' && target === '_blank') {
      e.preventDefault();
      e.stopImmediatePropagation();
      window.open(href);
    }
  });
};