]>
Commit | Line | Data |
---|---|---|
1 | const internals = { | |
2 | promisesSupported: !!window.browser, | |
3 | injectedScript: false, | |
4 | port: null, | |
5 | currentUrl: null, | |
6 | ||
7 | icons: { | |
8 | call: { | |
9 | 16: "icons/action-16.png", | |
10 | 32: "icons/action-32.png", | |
11 | }, | |
12 | ||
13 | hangUp: { | |
14 | 16: "icons/hang_up-16.png", | |
15 | 32: "icons/hang_up-32.png", | |
16 | }, | |
17 | }, | |
18 | ||
19 | onClick() { | |
20 | if (internals.isInCall()) { | |
21 | return internals.hangUp(); | |
22 | } | |
23 | ||
24 | return internals.initializeContentScript(); | |
25 | }, | |
26 | ||
27 | joinAudioCall() { | |
28 | internals.port.postMessage({ | |
29 | action: "joinAudioCall", | |
30 | data: { | |
31 | currentUrl: internals.currentUrl, | |
32 | tada: internals.getRoot().runtime.getURL("sounds/tada.wav"), | |
33 | }, | |
34 | }); | |
35 | internals.getRoot().browserAction.enable(); | |
36 | internals.setIcon("hangUp"); | |
37 | }, | |
38 | ||
39 | onConnect(port) { | |
40 | internals.port = port; | |
41 | port.onDisconnect.addListener(internals.onDisconnect); | |
42 | port.onMessage.addListener(internals.onMessage); | |
43 | internals.joinAudioCall(); | |
44 | }, | |
45 | ||
46 | onMessage(message) { | |
47 | if (message.action === "error") { | |
48 | internals.getRoot().browserAction.setBadgeText({ text: "x" }, () => {}); | |
49 | } | |
50 | }, | |
51 | ||
52 | onDisconnect() { | |
53 | internals.getRoot().browserAction.setBadgeText({ text: "" }, () => {}); | |
54 | internals.setIcon("call"); | |
55 | internals.currentUrl = null; | |
56 | internals.port = null; | |
57 | internals.getRoot().browserAction.enable(); | |
58 | }, | |
59 | ||
60 | async initializeContentScript() { | |
61 | internals.getRoot().browserAction.disable(); | |
62 | const activeTabs = await internals.getActiveTabs(); | |
63 | ||
64 | internals.currentUrl = activeTabs[0].url; | |
65 | if (!internals.injectedScript) { | |
66 | const execution = await internals.getRoot().tabs.executeScript( | |
67 | activeTabs[0].id, | |
68 | { | |
69 | file: "/build/content_script.js", | |
70 | }, | |
71 | () => { | |
72 | internals.injectedScript = true; | |
73 | }, | |
74 | ); | |
75 | } else { | |
76 | internals.getRoot().tabs.connect(activeTabs[0].id) | |
77 | } | |
78 | ||
79 | if (execution && !execution[0]) { | |
80 | internals.onDisconnect(); | |
81 | } | |
82 | }, | |
83 | hangUp() { | |
84 | internals.getRoot().browserAction.disable(); | |
85 | internals.port.postMessage({ | |
86 | action: "hangUp", | |
87 | }); | |
88 | }, | |
89 | ||
90 | isInCall() { | |
91 | return !!internals.port; | |
92 | }, | |
93 | ||
94 | setIcon(iconSet) { | |
95 | internals.getRoot().browserAction.setIcon({ | |
96 | path: internals.icons[iconSet], | |
97 | }); | |
98 | }, | |
99 | ||
100 | getRoot() { | |
101 | return window.browser || window.chrome; | |
102 | }, | |
103 | ||
104 | // Chrome doesn't yet implement the promise based tabs.query :'( | |
105 | ||
106 | getActiveTabs() { | |
107 | const query = { | |
108 | currentWindow: true, | |
109 | active: true, | |
110 | }; | |
111 | ||
112 | if (internals.promisesSupported) { | |
113 | return internals.getRoot().tabs.query(query); | |
114 | } | |
115 | ||
116 | return new Promise((resolve) => { | |
117 | internals.getRoot().tabs.query(query, (tabs) => { | |
118 | return resolve(tabs); | |
119 | }); | |
120 | }); | |
121 | }, | |
122 | }; | |
123 | ||
124 | internals.getRoot().browserAction.onClicked.addListener(internals.onClick); | |
125 | internals.getRoot().runtime.onConnect.addListener(internals.onConnect); |