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