]> git.r.bdr.sh - rbdr/junction/blame - extension/junction.js
Add content script support on firefox
[rbdr/junction] / extension / junction.js
CommitLineData
1ed219c8
RBR
1const internals = {
2
a94a5407 3 promisesSupported: !!(window.browser),
4757c461 4 port: null,
1ed219c8
RBR
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
4757c461 26 onConnect(port) {
1ed219c8 27
4757c461
RBR
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();
1ed219c8 38 internals.setIcon('hangUp');
1ed219c8
RBR
39 },
40
4757c461 41 onMessage(message) {
1ed219c8 42
4757c461
RBR
43 if (message.action === 'error') {
44 internals.getRoot().browserAction.setBadgeText({ text: 'x' }, () => {});
45 }
46 },
47
48 onDisconnect() {
49 internals.getRoot().browserAction.setBadgeText({ text: '' }, () => {});
1ed219c8 50 internals.setIcon('call');
4757c461
RBR
51 internals.port = null;
52 internals.getRoot().browserAction.enable();
1ed219c8
RBR
53 },
54
4757c461 55 async joinAudioCall() {
1ed219c8 56
4757c461
RBR
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 });
1ed219c8
RBR
74 },
75
76 isInCall() {
4757c461
RBR
77
78 return !!(internals.port);
1ed219c8
RBR
79 },
80
81 setIcon(iconSet) {
82
a94a5407 83 internals.getRoot().browserAction.setIcon({
1ed219c8
RBR
84 path: internals.icons[iconSet]
85 });
a94a5407
RBR
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 },
1ed219c8
RBR
114};
115
a94a5407 116internals.getRoot().browserAction.onClicked.addListener(internals.onClick);
4757c461 117internals.getRoot().runtime.onConnect.addListener(internals.onConnect);