This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbrowser_action.js
180 lines (158 loc) · 4.67 KB
/
browser_action.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const background = chrome.extension.getBackgroundPage();
const servers = background.servers;
const serverSelect = document.getElementById("server-select");
const roomnameInput = document.getElementById("roomname");
const recentRoomsDatalist = document.getElementById("recent_rooms_list");
const recentRoomsInput = document.getElementById("recent_rooms_input");
const randomButton = document.getElementById("random");
const submitButton = document.getElementById("submit");
const showButton = document.getElementById("show");
const link = document.querySelector("a");
const wishlist =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const recentRoomNames = JSON.parse(localStorage.getItem("recentRooms")) || [];
serverSelect.innerHTML = servers.map((server, index) => {
return `<option value="${index}">${server}</option>`;
});
const handleInConference = () => {
submitButton.value = "Close";
document.querySelectorAll("input, select").forEach((input) => {
if (input.type === "submit") return;
input.disabled = true;
});
document.body.classList.add("in-conference");
};
const handleNotInConference = () => {
submitButton.value = "Enter";
document.querySelectorAll("input, select").forEach((input) => {
if (input.type === "submit") return;
input.disabled = false;
});
document.body.classList.remove("in-conference");
};
const generate = (length = 12) =>
Array(length)
.fill("")
.map(
() =>
wishlist[
Math.floor(
(crypto.getRandomValues(new Uint32Array(1))[0] / (0xffffffff + 1)) *
wishlist.length
)
]
)
.join("");
const setServerValue = (value) => {
// Range check
value = parseInt(value);
if (value >= 0 && value < servers.length) {
setValue(serverSelect, value);
} else {
setValue(serverSelect, 0);
}
};
const setValue = (input, value) => {
input.value = value;
if (typeof input.oninput === "function") {
input.oninput({ target: input });
}
};
randomButton.onclick = (e) => {
setValue(roomnameInput, generate(12));
};
link.onclick = (e) => {
e.preventDefault();
const tmpString = "Copied!";
navigator.clipboard
.writeText(
`https://jitsipop.tk/#/${servers[background.selectedServerIndex]}/${
roomnameInput.value
}`
)
.then(
() => {
if (link.innerText !== tmpString) {
const oldString = link.innerText;
link.innerText = tmpString;
setTimeout(() => {
link.innerText = oldString;
}, 1000);
}
},
(err) => {
console.error("Async: Could not copy text: ", err);
}
);
};
// Set values from local storage
setValue(
roomnameInput,
(recentRoomNames.length && recentRoomNames[0].roomName) || ""
);
setServerValue(localStorage.getItem("serverSelect"));
if (recentRoomNames.length) {
recentRoomsDatalist.innerHTML = recentRoomNames.map((d) => {
const date = new Date(d.timestamp);
return `<option value="${
d.roomName
}">${date.toLocaleDateString()} ${date.toLocaleTimeString()}</option>`;
});
recentRoomsInput.oninput = (e) => {
if (e.inputType === "insertText") {
roomnameInput.focus();
setValue(roomnameInput, roomnameInput.value + e.data);
} else if (e.target.value !== "") {
setValue(roomnameInput, e.target.value);
}
e.target.value = "";
};
} else {
recentRoomsInput.style.display = "none";
}
document.querySelector("form").onsubmit = (e) => {
e.preventDefault();
if (document.body.classList.contains("in-conference")) {
background.disconnect();
} else {
background.selectedServerIndex = parseInt(serverSelect.value);
background.openPopout(roomnameInput.value);
}
window.close();
};
document.addEventListener("keydown", (e) => {
if (e.keyCode == "13") {
e.preventDefault();
if (
!background.mainAppWindowObject ||
background.mainAppWindowObject.closed
) {
// Don't click the submit button when we're in a conference,
// it would close the main window
submitButton.click();
}
}
});
showButton.onclick = (e) => {
e.preventDefault();
background.focusAllWindows();
window.close();
};
chrome.runtime.onMessage.addListener(function (message) {
if (message.type === "videoConferenceJoined") {
handleInConference();
} else if (message.type === "videoConferenceLeft") {
handleNotInConference();
}
});
window.addEventListener("storage", function (e) {
if (e.key === "serverSelect") {
setServerValue(e.newValue);
}
});
if (background.mainAppWindowObject && !background.mainAppWindowObject.closed) {
handleInConference();
} else {
handleNotInConference();
}
document.body.style.display = "block";