aboutsummaryrefslogtreecommitdiff
path: root/config/qutebrowser/greasemonkey/google-meet.js
blob: 639608e0b2934c49b53599fbb4bc905f135b8ace (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
79
80
81
82
83
84
85
86
87
88
89
90
// ==UserScript==
// @name               Google meet mute indicator
// @namespace          phenax.github.io
// @version            0.0.0
// @description        Google meet mute indicator
// @author             Akshay Nair
// @match              *://meet.google.com/*
// ==/UserScript==

(() => {
  const log = console.log.bind(console, '[MeetingBtns]');
  const $$ = document.querySelectorAll.bind(document);

  const $indicator = document.createElement('div');
  Object.assign($indicator.style, {
    backgroundColor: 'red',
    width: '200px',
    height: '100px',
    position: 'fixed',
    zIndex: 90000,
    left: 0,
    top: 0,
    opacity: 0,
  });

  const getMuteBtns = () => Array.from($$('[data-is-muted][role="button"]'));

  const updateMuteState = () => {
    try {
      const $btns = getMuteBtns();
      const $mutedBtns = $btns.filter($btn => $btn.dataset.isMuted === 'true');
      Object.assign($indicator.style, {
        opacity: $mutedBtns.length === $btns.length ? 0 : 1,
      });
    } catch (e) { log('update error', e); }
  };

  const observer = new MutationObserver(diffs => {
    log('mutation observer');
    const muteChange = diffs.find(d => d.attributeName === 'data-is-muted');
    if (!!muteChange) {
      log('updating mute state');
      updateMuteState();
    }
  });

  const init = () => {
    const $muteBtns = getMuteBtns();
    log('initializing', $muteBtns);
    document.body.appendChild($indicator);

    $muteBtns.forEach($btn => {
      observer.observe($btn, { attributes: true, attributeFilter: ['data-is-muted'] });
      updateMuteState();
    });

    if (window.$$$muteObserver) window.$$$muteObserver.disconnect();
    window.$$$muteObserver = observer;
  };

  const timer = setInterval(() => {
    const $btns = getMuteBtns();
    const hasJoinedMeeting = $$('[data-meeting-code]').length === 0;
    log('checking if joined...', hasJoinedMeeting);
    if ($btns.length > 0 && hasJoinedMeeting) {
      setTimeout(() => init(), 1000);
      clearInterval(timer);
    }
  }, 500);


  /////////////////////// Push to talk

  // let isSpacePressed = false
  // const setSpacePressed = p => {
  //   isSpacePressed = p
  // }

  // window.addEventListener('keydown', e => {
  //   if (e.key === 'space') {
  //     setSpacePressed(true)
  //   }
  // })
  // window.addEventListener('keydown', e => {
  //   if (e.key === 'space') {
  //     setSpacePressed(false)
  //   }
  // })
})();