]> git.r.bdr.sh - rbdr/junction/blame_incremental - extension/junction.js
Add editorconfig
[rbdr/junction] / extension / junction.js
... / ...
CommitLineData
1const internals = {
2 promisesSupported: !!window.browser,
3 port: null,
4 currentUrl: 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 internals.port = port;
28 port.onDisconnect.addListener(internals.onDisconnect);
29 port.onMessage.addListener(internals.onMessage);
30 port.postMessage({
31 action: "joinAudioCall",
32 data: {
33 currentUrl: internals.currentUrl,
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 if (message.action === "error") {
43 internals.getRoot().browserAction.setBadgeText({ text: "x" }, () => {});
44 }
45 },
46
47 onDisconnect() {
48 internals.getRoot().browserAction.setBadgeText({ text: "" }, () => {});
49 internals.setIcon("call");
50 internals.currentUrl = null;
51 internals.port = null;
52 internals.getRoot().browserAction.enable();
53 },
54
55 async joinAudioCall() {
56 internals.getRoot().browserAction.disable();
57 const activeTabs = await internals.getActiveTabs();
58
59 internals.currentUrl = activeTabs[0].url;
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 );
71
72 if (execution && !execution[0]) {
73 internals.onDisconnect();
74 }
75 },
76 hangUp() {
77 internals.getRoot().browserAction.disable();
78 internals.port.postMessage({
79 action: "hangUp",
80 });
81 },
82
83 isInCall() {
84 return !!internals.port;
85 },
86
87 setIcon(iconSet) {
88 internals.getRoot().browserAction.setIcon({
89 path: internals.icons[iconSet],
90 });
91 },
92
93 getRoot() {
94 return window.browser || window.chrome;
95 },
96
97 // Chrome doesn't yet implement the promise based tabs.query :'(
98
99 getActiveTabs() {
100 const query = {
101 currentWindow: true,
102 active: true,
103 };
104
105 if (internals.promisesSupported) {
106 return internals.getRoot().tabs.query(query);
107 }
108
109 return new Promise((resolve) => {
110 internals.getRoot().tabs.query(query, (tabs) => {
111 return resolve(tabs);
112 });
113 });
114 },
115};
116
117internals.getRoot().browserAction.onClicked.addListener(internals.onClick);
118internals.getRoot().runtime.onConnect.addListener(internals.onConnect);