aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/rocketchat/webview.js
blob: 67f50f13416e203952d2024dd72cdf044a40db78 (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
58
59
60
61
62
63
64
function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

const _path = _interopRequireDefault(require('path'));

module.exports = Ferdium => {
  const getMessages = () => {
    const title = document.querySelector('title');
    const matches = title.textContent.match(/^\((\S*?)\)/);

    if (matches) {
      const count = Ferdium.safeParseInt(matches[1], 10);
      if (count) {
        Ferdium.setBadge(count);
      } else {
        Ferdium.setBadge(0, 1);
      }
    } else {
      Ferdium.setBadge(0);
    }
  };

  Ferdium.loop(getMessages);

  Ferdium.injectCSS(_path.default.join(__dirname, 'service.css'));

  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);
};