]> git.r.bdr.sh - rbdr/junction/blob - extension/content_script.js
Add content script support on firefox
[rbdr/junction] / extension / content_script.js
1 (() => {
2
3 const internals = {
4 port: null,
5
6 onMessage(message) {
7 internals[message.action](message.data);
8 },
9
10 async joinAudioCall(data) {
11
12 try {
13 const mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true });
14 internals.createAudioElement(data.tada);
15 }
16 catch (err) {
17
18 internals.port.postMessage({
19 action: 'error'
20 });
21 internals.port.disconnect();
22 internals.createAudioElement(data.tada);
23 }
24 },
25
26 hangUp() {
27 document.querySelectorAll('.junction-call-audio').forEach((audioElement) => audioElement.remove());
28 internals.port.disconnect();
29 },
30
31 createAudioElement(source, type = 'audio/wav') {
32
33 const audioElement = document.createElement('audio');
34 audioElement.setAttribute('class', 'junction-call-audio');
35 audioElement.src = source;
36 audioElement.autoplay = 'autoplay';
37 audioElement.type = type;
38 document.querySelector('body').appendChild(audioElement);
39 }
40 };
41
42 internals.port = chrome.runtime.connect({ name:"content" });
43 internals.port.onMessage.addListener(internals.onMessage);
44 })();
45
46 // Indicates to the background script that we executed correctly
47 true;