]> git.r.bdr.sh - rbdr/junction/blame_incremental - extension/junction.js
Add media and peer objects
[rbdr/junction] / extension / junction.js
... / ...
CommitLineData
1const internals = {
2
3 promisesSupported: !!(window.browser),
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.joinAudioCall();
25 },
26
27 onConnect(port) {
28
29 internals.port = port;
30 port.onDisconnect.addListener(internals.onDisconnect);
31 port.onMessage.addListener(internals.onMessage);
32 port.postMessage({
33 action: 'joinAudioCall',
34 data: {
35 currentUrl: internals.currentUrl,
36 tada: internals.getRoot().runtime.getURL('sounds/tada.wav')
37 }
38 });
39 internals.getRoot().browserAction.enable();
40 internals.setIcon('hangUp');
41 },
42
43 onMessage(message) {
44
45 if (message.action === 'error') {
46 internals.getRoot().browserAction.setBadgeText({ text: 'x' }, () => {});
47 }
48 },
49
50 onDisconnect() {
51 internals.getRoot().browserAction.setBadgeText({ text: '' }, () => {});
52 internals.setIcon('call');
53 internals.currentUrl = null;
54 internals.port = null;
55 internals.getRoot().browserAction.enable();
56 },
57
58 async joinAudioCall() {
59
60 internals.getRoot().browserAction.disable();
61 const activeTabs = await internals.getActiveTabs();
62
63 internals.currentUrl = activeTabs[0].url;
64 const execution = await internals.getRoot().tabs.executeScript(activeTabs[0].id, {
65 file: '/build/content_script.js'
66 }, () => {
67
68 if (internals.getRoot().runtime.lastError) {
69 internals.onDisconnect();
70 }
71 });
72
73 if (execution && !execution[0]) {
74 internals.onDisconnect();
75 }
76 }
77 ,
78
79 hangUp() {
80
81 internals.getRoot().browserAction.disable();
82 internals.port.postMessage({
83 action: 'hangUp'
84 });
85 },
86
87 isInCall() {
88
89 return !!(internals.port);
90 },
91
92 setIcon(iconSet) {
93
94 internals.getRoot().browserAction.setIcon({
95 path: internals.icons[iconSet]
96 });
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
117 return new Promise((resolve, reject) => {
118
119 internals.getRoot().tabs.query(query, (tabs) => {
120
121 return resolve(tabs);
122 });
123 });
124 },
125};
126
127internals.getRoot().browserAction.onClicked.addListener(internals.onClick);
128internals.getRoot().runtime.onConnect.addListener(internals.onConnect);