]> git.r.bdr.sh - rbdr/junction/blame - extension/junction.js
Update dependencies
[rbdr/junction] / extension / junction.js
CommitLineData
1ed219c8
RBR
1const internals = {
2
a94a5407 3 promisesSupported: !!(window.browser),
4757c461 4 port: null,
02071d8e 5 currentUrl: null,
1ed219c8
RBR
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.joinAudioCall();
25 },
26
4757c461 27 onConnect(port) {
1ed219c8 28
4757c461
RBR
29 internals.port = port;
30 port.onDisconnect.addListener(internals.onDisconnect);
31 port.onMessage.addListener(internals.onMessage);
32 port.postMessage({
33 action: 'joinAudioCall',
34 data: {
02071d8e 35 currentUrl: internals.currentUrl,
4757c461
RBR
36 tada: internals.getRoot().runtime.getURL('sounds/tada.wav')
37 }
38 });
39 internals.getRoot().browserAction.enable();
1ed219c8 40 internals.setIcon('hangUp');
1ed219c8
RBR
41 },
42
4757c461 43 onMessage(message) {
1ed219c8 44
4757c461
RBR
45 if (message.action === 'error') {
46 internals.getRoot().browserAction.setBadgeText({ text: 'x' }, () => {});
47 }
48 },
49
50 onDisconnect() {
51 internals.getRoot().browserAction.setBadgeText({ text: '' }, () => {});
1ed219c8 52 internals.setIcon('call');
02071d8e 53 internals.currentUrl = null;
4757c461
RBR
54 internals.port = null;
55 internals.getRoot().browserAction.enable();
1ed219c8
RBR
56 },
57
4757c461 58 async joinAudioCall() {
1ed219c8 59
4757c461
RBR
60 internals.getRoot().browserAction.disable();
61 const activeTabs = await internals.getActiveTabs();
4757c461 62
02071d8e
RBR
63 internals.currentUrl = activeTabs[0].url;
64 const execution = await internals.getRoot().tabs.executeScript(activeTabs[0].id, {
65 file: '/build/content_script.js'
66 }, () => {
6393c915 67
02071d8e 68 if (internals.getRoot().runtime.lastError) {
6393c915
RBR
69 internals.onDisconnect();
70 }
02071d8e
RBR
71 });
72
73 if (execution && !execution[0]) {
74 internals.onDisconnect();
75 }
6393c915
RBR
76 }
77 ,
4757c461
RBR
78
79 hangUp() {
80
81 internals.getRoot().browserAction.disable();
82 internals.port.postMessage({
83 action: 'hangUp'
84 });
1ed219c8
RBR
85 },
86
87 isInCall() {
4757c461
RBR
88
89 return !!(internals.port);
1ed219c8
RBR
90 },
91
92 setIcon(iconSet) {
93
a94a5407 94 internals.getRoot().browserAction.setIcon({
1ed219c8
RBR
95 path: internals.icons[iconSet]
96 });
a94a5407
RBR
97 },
98
99 getRoot() {
100
101 return window.browser || window.chrome;
102 },
103
104 // Chrome doesn't yet implement the promise based tabs.query :'(
105
106 getActiveTabs() {
107
108 const query = {
109 currentWindow: true,
110 active: true
111 };
112
113 if (internals.promisesSupported) {
114 return internals.getRoot().tabs.query(query);
115 }
116
80172072 117 return new Promise((resolve) => {
a94a5407
RBR
118
119 internals.getRoot().tabs.query(query, (tabs) => {
120
121 return resolve(tabs);
122 });
123 });
124 },
1ed219c8
RBR
125};
126
a94a5407 127internals.getRoot().browserAction.onClicked.addListener(internals.onClick);
4757c461 128internals.getRoot().runtime.onConnect.addListener(internals.onConnect);