]> git.r.bdr.sh - rbdr/junction/blame_incremental - extension/junction.js
Build with Babel
[rbdr/junction] / extension / junction.js
... / ...
CommitLineData
1const 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
60 const execution = await internals.getRoot().tabs.executeScript(activeTabs[0].id, {
61 file: '/build/content_script.js'
62 });
63
64 if (!execution || !execution[0]) {
65 internals.onDisconnect();
66 }
67 }
68 ,
69
70 hangUp() {
71
72 internals.getRoot().browserAction.disable();
73 internals.port.postMessage({
74 action: 'hangUp'
75 });
76 },
77
78 isInCall() {
79
80 return !!(internals.port);
81 },
82
83 setIcon(iconSet) {
84
85 internals.getRoot().browserAction.setIcon({
86 path: internals.icons[iconSet]
87 });
88 },
89
90 getRoot() {
91
92 return window.browser || window.chrome;
93 },
94
95 // Chrome doesn't yet implement the promise based tabs.query :'(
96
97 getActiveTabs() {
98
99 const query = {
100 currentWindow: true,
101 active: true
102 };
103
104 if (internals.promisesSupported) {
105 return internals.getRoot().tabs.query(query);
106 }
107
108 return new Promise((resolve, reject) => {
109
110 internals.getRoot().tabs.query(query, (tabs) => {
111
112 return resolve(tabs);
113 });
114 });
115 },
116};
117
118internals.getRoot().browserAction.onClicked.addListener(internals.onClick);
119internals.getRoot().runtime.onConnect.addListener(internals.onConnect);