aboutsummaryrefslogtreecommitdiff
path: root/extension/junction.js
blob: 1d5f8a50b26c5ef8546f43245ba771862fac0aa2 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const internals = {
  promisesSupported: !!window.browser,
  injectedScript: {},
  port: null,
  currentUrl: null,

  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.initializeContentScript();
  },

  joinAudioCall() {
    internals.port.postMessage({
      action: "joinAudioCall",
      data: {
        currentUrl: internals.currentUrl,
        tada: internals.getRoot().runtime.getURL("sounds/tada.wav"),
      },
    });
    internals.getRoot().browserAction.enable();
    internals.setIcon("hangUp");
  },

  onConnect(port) {
    internals.port = port;
    port.onDisconnect.addListener(internals.onDisconnect);
    port.onMessage.addListener(internals.onMessage);
    internals.joinAudioCall();
  },

  onMessage(message) {
    if (message.action === "error") {
      internals.getRoot().browserAction.setBadgeText({ text: "x" }, () => {});
    }
  },

  onDisconnect() {
    internals.getRoot().browserAction.setBadgeText({ text: "" }, () => {});
    internals.setIcon("call");
    internals.currentUrl = null;
    internals.port = null;
    internals.getRoot().browserAction.enable();
  },

  async initializeContentScript() {
    internals.getRoot().browserAction.disable();
    const activeTabs = await internals.getActiveTabs();

    internals.currentUrl = activeTabs[0].url;
    const id = activeTabs[0].id;
    if (!internals.injectedScript[id]) {
      const execution = await internals.getRoot().tabs.executeScript(
        activeTabs[0].id,
        {
          file: "/build/content_script.js",
        },
        () => {
          internals.injectedScript[id] = true;
        },
      );

      if (execution && !execution[0]) {
        internals.onDisconnect();
      }
    } else {
      internals.getRoot().tabs.connect(activeTabs[0].id);
    }
  },

  hangUp() {
    internals.getRoot().browserAction.disable();
    internals.port.postMessage({
      action: "hangUp",
    });
  },

  isInCall() {
    return !!internals.port;
  },

  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) => {
      internals.getRoot().tabs.query(query, (tabs) => {
        return resolve(tabs);
      });
    });
  },
};

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