aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/slack/webview.js
blob: 924784df07aa82ebace2ff999492c53512493e4e (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const _path = _interopRequireDefault(require('path'));

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

const SELECTOR_CHANNELS_UNREAD =
  '.p-channel_sidebar__channel--unread:not(.p-channel_sidebar__channel--muted)';

module.exports = Ferdi => {
  const getMessages = () => {
    const directMessages = document.querySelectorAll(
      `${SELECTOR_CHANNELS_UNREAD} .p-channel_sidebar__badge, .p-channel_sidebar__link--unread:not([data-sidebar-link-id="Punreads"]):not([data-sidebar-link-id="Pdrafts"]):not([data-sidebar-link-id="Pdms"])`,
    ).length;
    const allMessages =
      document.querySelectorAll(SELECTOR_CHANNELS_UNREAD).length -
      directMessages;
    Ferdi.setBadge(directMessages, allMessages);
  };

  const getActiveDialogTitle = () => {
    const element = document.querySelector(
      '.p-channel_sidebar__channel--selected .p-channel_sidebar__name',
    );

    Ferdi.setDialogTitle(
      element && element.firstChild ? element.firstChild.textContent : null,
    );
  };

  const loopFunc = () => {
    getMessages();
    getActiveDialogTitle();
  };

  Ferdi.loop(loopFunc);

  const getTeamIcon = function getTeamIcon(count = 0) {
    let countTeamIconCheck = count;
    let bgUrl = null;
    const teamMenu = document.querySelector(
      '#team-menu-trigger, .p-ia__sidebar_header__team_name',
    );

    if (teamMenu) {
      teamMenu.click();
      const icon = document.querySelector('.c-team_icon');

      if (icon) {
        bgUrl = window
          .getComputedStyle(icon, null)
          .getPropertyValue('background-image');
        bgUrl = /^url\((["']?)(.*)\1\)$/.exec(bgUrl);
        bgUrl = bgUrl ? bgUrl[2] : '';
      }

      setTimeout(() => {
        document.querySelector('.ReactModal__Overlay').click();
      }, 10);
    }

    countTeamIconCheck += 1;

    if (bgUrl) {
      Ferdi.setAvatarImage(bgUrl);
    } else if (countTeamIconCheck <= 5) {
      setTimeout(() => {
        getTeamIcon(countTeamIconCheck + 1);
      }, 2000);
    }
  };

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

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