aboutsummaryrefslogtreecommitdiff
path: root/extension/src/junction.js
blob: e78a02bc0755301ca59def111a7a84050b265467 (plain)
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
91
const internals = {

  promisesSupported: !!(window.browser),
  isInCallState: false,

  icons: {
    call: {
      16: 'icons/action-16.png',
      32: 'icons/action-32.png'
    },

    hangUp: {
      16: 'icons/hang_up-16.png',
      32: 'icons/hang_up-32.png'
    }
  },

  onClick() {
    if (internals.isInCall()) {
      return internals.hangUp();
    }

    return internals.joinAudioCall();
  },

  async joinAudioCall() {

    internals.isInCallState = true;
    internals.setIcon('hangUp');
    const activeTabs = await internals.getActiveTabs();

    console.log(activeTabs[0].url); // placeholder while we connect backend.
    internals.createAudioElement(internals.getRoot().runtime.getURL('sounds/tada.wav'));
  },

  hangUp() {

    document.querySelectorAll('audio').forEach((audioElement) => audioElement.remove());
    internals.setIcon('call');
    internals.isInCallState = false;
  },

  createAudioElement(source, type = 'audio/wav') {

    const audioElement = document.createElement('audio');
    audioElement.src = source;
    audioElement.autoplay = 'autoplay';
    audioElement.type = type;
    document.querySelector('body').appendChild(audioElement);
  },

  isInCall() {
    return internals.isInCallState; // this should be replaced with actually checking the built stuff
  },

  setIcon(iconSet) {

    internals.getRoot().browserAction.setIcon({
      path: internals.icons[iconSet]
    });
  },

  getRoot() {

    return window.browser || window.chrome;
  },

  // Chrome doesn't yet implement the promise based tabs.query :'(

  getActiveTabs() {

    const query = {
      currentWindow: true,
      active: true
    };

    if (internals.promisesSupported) {
      return internals.getRoot().tabs.query(query);
    }

    return new Promise((resolve, reject) => {

      internals.getRoot().tabs.query(query, (tabs) => {

        return resolve(tabs);
      });
    });
  },
};

internals.getRoot().browserAction.onClicked.addListener(internals.onClick);