aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/rocketchat/webview.js
blob: 85b0c3268b5e9a35eac090023b3d84f2bbeae08a (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
55
module.exports = Ferdium => {
  const getMessages = () => {
    const directMessages = document.querySelectorAll('.rcx-badge');

    let directMessagesCount = 0;

    for (const directMessage of directMessages) {
      directMessagesCount += Ferdium.safeParseInt(directMessage.textContent);
    }

    const indirectMessagesCount = Math.round(
      document.querySelectorAll('.rcx-sidebar-item--highlighted').length,
    );

    Ferdium.setBadge(directMessagesCount, indirectMessagesCount);
  };

  Ferdium.loop(getMessages);

  const getTeamIcon = function getTeamIcon() {
    const manifestElement = document.querySelector('link[rel="manifest"]');

    if (manifestElement == null) {
      return;
    }

    const manifestUrl = manifestElement.getAttribute('href');

    if (manifestUrl == null) {
      return;
    }

    const xmlhttp = new XMLHttpRequest();

    xmlhttp.addEventListener('readystatechange', function () {
      if (this.readyState != 4 || this.status != 200) {
        return;
      }

      const response = JSON.parse(this.responseText);

      if (response.icons.length > 0) {
        Ferdium.setAvatarImage(`${window.location.protocol}//${window.location.host}${response.icons[0].src}`);
      }
    });

    xmlhttp.open('GET', manifestUrl, true);
    xmlhttp.send();
  };

  setTimeout(() => {
    getTeamIcon();
  }, 4000);
};