]> git.r.bdr.sh - rbdr/junction/blame - extension/content_script.js
Inject script only once
[rbdr/junction] / extension / content_script.js
CommitLineData
b9a2baf2 1import { io } from "socket.io-client";
06b7617a
RBR
2import { initializeTada } from "./tada";
3import {
4 addPeer,
5 addIceCandidate,
6 answerPeerOffer,
7 processPeerAnswer,
8 removePeer,
9 resetPeers,
10} from "./peers";
11import { startMedia, stopMedia } from "./media";
d9e5fa1a 12
80172072 13const internals = {
b9a2baf2
RBR
14 kSocketUrl: "https://junction.tranquil.services",
15 kIceServers: [{ urls: "stun:stun.l.google.com:19302" }],
d4fb48eb 16
80172072
RBR
17 port: null,
18 socket: null,
d4fb48eb 19
80172072
RBR
20 onMessage(message) {
21 internals[message.action](message.data);
22 },
d4fb48eb 23
06b7617a
RBR
24 onError(error) {
25 // TODO: How do we want to handle errors?
26 console.error(error.stack || error);
27 },
28
5685e249
RBR
29 connect() {
30 internals.port = chrome.runtime.connect({ name: "content" });
31 internals.port.onMessage.addListener(internals.onMessage);
32 },
33
b9a2baf2 34 async joinAudioCall({ currentUrl, tada }) {
80172072 35 try {
06b7617a
RBR
36 const mediaStream = await startMedia();
37 const playTada = initializeTada(tada);
4757c461 38
06b7617a 39 const socket = (internals.socket = io(internals.kSocketUrl, {
b9a2baf2 40 transports: ["websocket"],
06b7617a 41 }));
d9e5fa1a 42
06b7617a
RBR
43 socket.on("error", internals.handleError);
44 socket.on("connect_error", internals.handleError);
02071d8e 45
06b7617a
RBR
46 socket.on("connect", () => {
47 console.debug("Connected to signaling server, group: ", currentUrl);
48 playTada();
49 socket.emit("join", {
b9a2baf2 50 room: currentUrl,
02071d8e 51 });
80172072 52 });
d4fb48eb 53
06b7617a
RBR
54 socket.on("disconnect", () => {
55 console.debug("disconnected from signaling server");
80172072 56 });
d4fb48eb 57
3f2e36ea 58 socket.on("addPeer", ({ peerId, shouldCreateOffer }) => {
06b7617a
RBR
59 addPeer({
60 peerId,
3f2e36ea 61 shouldCreateOffer,
06b7617a
RBR
62 mediaStream,
63 onOffer: (data) => socket.emit("relayOffer", data),
64 socket
b9a2baf2 65 });
06b7617a 66 playTada();
80172072 67 });
d4fb48eb 68
06b7617a
RBR
69 socket.on("offerReceived", async ({ peerId, offer }) => {
70 socket.emit("relayAnswer", await answerPeerOffer({ peerId, offer }));
284fc661
RBR
71 });
72
06b7617a
RBR
73 socket.on("answerReceived", (data) => processPeerAnswer(data));
74 socket.on("ICECandidateReceived", (data) => addIceCandidate(data));
75 socket.on("removePeer", (data) => removePeer(data));
b9a2baf2 76 } catch (err) {
80172072 77 internals.port.postMessage({
b9a2baf2 78 action: "error",
80172072
RBR
79 });
80 internals.port.disconnect();
81 }
82 },
4757c461 83
80172072 84 hangUp() {
06b7617a
RBR
85 resetPeers();
86 stopMedia();
80172072
RBR
87 internals.socket.close();
88 internals.port.disconnect();
b9a2baf2 89 },
80172072 90};
d9e5fa1a 91
5685e249
RBR
92internals.connect();
93chrome.runtime.onConnect.addListener(() => {
94 internals.connect();
95});
4757c461 96
06b7617a 97console.debug("Content Script Loaded");
4757c461
RBR
98
99// Indicates to the background script that we executed correctly
100true;