]> git.r.bdr.sh - rbdr/junction/blame - extension/content_script.js
Add safari code
[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
b9a2baf2 29 async joinAudioCall({ currentUrl, tada }) {
80172072 30 try {
06b7617a
RBR
31 const mediaStream = await startMedia();
32 const playTada = initializeTada(tada);
4757c461 33
06b7617a 34 const socket = (internals.socket = io(internals.kSocketUrl, {
b9a2baf2 35 transports: ["websocket"],
06b7617a 36 }));
d9e5fa1a 37
06b7617a
RBR
38 socket.on("error", internals.handleError);
39 socket.on("connect_error", internals.handleError);
02071d8e 40
06b7617a
RBR
41 socket.on("connect", () => {
42 console.debug("Connected to signaling server, group: ", currentUrl);
43 playTada();
44 socket.emit("join", {
b9a2baf2 45 room: currentUrl,
02071d8e 46 });
80172072 47 });
d4fb48eb 48
06b7617a
RBR
49 socket.on("disconnect", () => {
50 console.debug("disconnected from signaling server");
80172072 51 });
d4fb48eb 52
06b7617a
RBR
53 socket.on("addPeer", ({ peerId }) => {
54 addPeer({
55 peerId,
56 mediaStream,
57 onOffer: (data) => socket.emit("relayOffer", data),
58 socket
b9a2baf2 59 });
06b7617a 60 playTada();
80172072 61 });
d4fb48eb 62
06b7617a
RBR
63 socket.on("offerReceived", async ({ peerId, offer }) => {
64 socket.emit("relayAnswer", await answerPeerOffer({ peerId, offer }));
284fc661
RBR
65 });
66
06b7617a
RBR
67 socket.on("answerReceived", (data) => processPeerAnswer(data));
68 socket.on("ICECandidateReceived", (data) => addIceCandidate(data));
69 socket.on("removePeer", (data) => removePeer(data));
b9a2baf2 70 } catch (err) {
80172072 71 internals.port.postMessage({
b9a2baf2 72 action: "error",
80172072
RBR
73 });
74 internals.port.disconnect();
75 }
76 },
4757c461 77
80172072 78 hangUp() {
06b7617a
RBR
79 resetPeers();
80 stopMedia();
80172072
RBR
81 internals.socket.close();
82 internals.port.disconnect();
b9a2baf2 83 },
80172072 84};
d9e5fa1a 85
b9a2baf2 86internals.port = chrome.runtime.connect({ name: "content" });
80172072 87internals.port.onMessage.addListener(internals.onMessage);
4757c461 88
06b7617a 89console.debug("Content Script Loaded");
4757c461
RBR
90
91// Indicates to the background script that we executed correctly
92true;