aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/rocketchat/webview.js
blob: 42409355ab41431d8dd24b6aa4dff4b74d6473d3 (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
56
57
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.onreadystatechange = function () {
    if (this.readyState != 4 || this.status != 200) {
      return;
    }

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

    if (response.icons.length >= 1) {
      Ferdi.ipcRenderer.sendToHost(
        'avatar',
        `${window.location.protocol}//${window.location.host}${response.icons[0].src}`,
      );
    }
  };

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

module.exports = Ferdi => {
  const getMessages = function getMessages() {
    const directMessages = document.querySelectorAll('.rcx-badge');

    let directMessagesCount = 0;

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

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

    Ferdi.setBadge(directMessagesCount, indirectMessagesCount);
  };

  Ferdi.loop(getMessages);

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