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