-
Notifications
You must be signed in to change notification settings - Fork 14
/
script.js
287 lines (249 loc) · 9.55 KB
/
script.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const pubnub = new PubNub({
publishKey: "pub-c-a31ed9a8-8e6e-4c94-9229-3a2f94d4cf02", // Replace with your PubNub publish key
subscribeKey: "sub-c-8a324682-fdc4-43c3-8308-f76b6410eb1d", // Replace with your PubNub subscribe key
});
const usernameInput = document.getElementById("username");
const joinBtn = document.getElementById("joinBtn");
const questionSection = document.getElementById("questionSection");
const answerSection = document.getElementById("answerSection");
const questionInput = document.getElementById("question");
const askBtn = document.getElementById("askBtn");
const answerInput = document.getElementById("answer");
const answerBtn = document.getElementById("answerBtn");
const messageLog = document.getElementById("messageLog");
const shareLink = document.getElementById("shareLink");
let initStorage = {
role: "",
username: "",
messageLog: [],
latestMessage: 0, // latestMessage will be use to check if the storage is expired
};
let storage = null;
let currentChannel = "";
let role = "res";
let lastMessageTimestamp = 0;
const MESSAGE_RATE_LIMIT_MS = 30000;
const EXPIRE_TIME = 86400000; // 24 hours
// If the URL contains a channel and role, handle the questioner's flow
const queryParams = new URLSearchParams(window.location.search);
const isQuestioner = queryParams.has("channel") && queryParams.get("role") === "ques";
// Generate a random channel name
function generateRandomChannel() {
return "channel_" + Math.random().toString(36).substring(2, 10);
}
// Join the randomly generated channel as responder
joinBtn.addEventListener("click", () => {
const username = usernameInput.value;
if (!username) {
alert("Please enter your username");
return;
}
currentChannel = generateRandomChannel();
role = "res"; // Default to responder
// Subscribe to the random channel
pubnub.subscribe({ channels: [currentChannel] });
// Display answer input for responder
questionSection.style.display = "none";
answerSection.style.display = "block";
// reset storage for new channel
storage = { ...initStorage };
messageLog.innerText = ""; // clear message log
shareLink.innerHTML = getShareableLink(currentChannel);
history.replaceState(null, "", `?channel=${currentChannel}&role=res`);
// Add event listener for the copy button
document.getElementById("copyLinkBtn").addEventListener("click", copyShareableLink);
// save the username and role to storage
storage.username = username;
storage.role = role;
// Notify the channel that the responder has joined
pubnub.publish({
channel: currentChannel,
message: { type: "user_connected", username: username, role: role },
});
});
// Generate and display shareable link for questioner
function getShareableLink(currentChannel) {
const shareableLink = `${window.location.origin}?channel=${currentChannel}&role=ques`;
return `
<div>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-linecap="round" stroke-width="1.5"><path d="M10.046 14c-1.506-1.512-1.37-4.1.303-5.779l4.848-4.866c1.673-1.68 4.25-1.816 5.757-.305s1.37 4.1-.303 5.78l-2.424 2.433"/><path d="M13.954 10c1.506 1.512 1.37 4.1-.303 5.779l-2.424 2.433l-2.424 2.433c-1.673 1.68-4.25 1.816-5.757.305s-1.37-4.1.303-5.78l2.424-2.433" opacity=".5"/></g></svg>
<span data-translate="shareLink">${translations[currentLanguage].shareLink}</span>
</div>
<div class="share-link-container">
<a class="shlink" target="_blank" href="${shareableLink}">${shareableLink}</a>
<button id="copyLinkBtn" class="btn copy-btn" data-translate="copyButton">${translations[currentLanguage].copyButton}</button>
</div>
`;
}
function isRateLimited() {
const currentTime = Date.now();
return currentTime - lastMessageTimestamp < MESSAGE_RATE_LIMIT_MS;
}
// Ask a question (for questioner)
askBtn.addEventListener("click", () => {
if (isRateLimited()) {
alert("Please wait before sending another message.");
return;
}
const question = questionInput.value;
if (!question) {
alert("Please enter a question.");
return;
}
// send message
pubnub.publish({
channel: currentChannel,
message: { type: "ask_question", question: question },
});
lastMessageTimestamp = Date.now();
});
// Send an answer (for responder)
answerBtn.addEventListener("click", () => {
if (isRateLimited()) {
alert("Please wait before sending another message.");
return;
}
const answer = answerInput.value;
if (!answer) {
alert("Please enter an answer.");
return;
}
pubnub.publish({
channel: currentChannel,
message: { type: "send_answer", answer: answer },
});
lastMessageTimestamp = Date.now();
});
// Listen for incoming messages
pubnub.addListener({
message: function (event) {
const msg = event.message;
const lang = currentLanguage;
if (msg.type === "ask_question") {
document.getElementById("displayQuestion").innerText = msg.question;
if (!isQuestioner) lastMessageTimestamp = 0; // responder can answer the question
return;
}
// send responder answer
if (msg.type === "send_answer") {
const answer = msg.answer;
const answerMessage = `Answer: ${answer}\n`;
messageLog.innerText += answerMessage;
// save message log using key for later translation
storage.messageLog.push(msg);
if (isQuestioner) lastMessageTimestamp = 0; // questioner can ask more questions
}
// user connected
if (msg.type === "user_connected" && !isQuestioner) {
const { username, role } = msg;
const connectedMessage =
translations[lang].userJoined
.replace("{username}", username)
.replace("{role}", role === "ques" ? translations[lang].questioner : translations[lang].responder) + "\n";
messageLog.innerText += connectedMessage;
// save message log using key for later translation
storage.messageLog.push(msg);
}
// store lastestMessage
storage.latestMessage = Date.now();
localStorage.setItem(currentChannel + role, JSON.stringify(storage));
},
});
if (isQuestioner) {
joinBtn.style.display = "none";
currentChannel = queryParams.get("channel");
role = "ques";
// Subscribe to the channel
pubnub.subscribe({ channels: [currentChannel] });
// Display question input for questioner
questionSection.style.display = "block";
answerSection.style.display = "none";
// Notify the channel that the questioner has joined
pubnub.publish({
channel: currentChannel,
message: { type: "user_connected", username: "ques", role: role },
});
} else {
// Show the explanatory message for questioners
document.getElementById("explanatory-message").style.display = "block"; // Show the explanatory message
}
// Initialize language on page load
document.addEventListener("DOMContentLoaded", () => {
clearExpiredChannel();
getStorageLogs();
});
// clear every local storage key that expired
function clearExpiredChannel() {
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (!key.startsWith("channel")) continue; // skip non channel keys
const storage = JSON.parse(localStorage.getItem(key));
if (storage.latestMessage == null || Date.now() - storage.latestMessage > EXPIRE_TIME) {
localStorage.removeItem(key);
}
}
}
// get storage logs for the current channel
function getStorageLogs() {
const channel = queryParams.get("channel");
const channelRole = queryParams.get("role");
storage = JSON.parse(localStorage.getItem(channel + channelRole));
if (storage == null) {
storage = { ...initStorage };
return;
}
if (channel != null && role != null) {
// set back all the data
usernameInput.value = storage.username;
role = storage.role;
answerSection.style.display = !isQuestioner ? "block" : "none";
questionSection.style.display = isQuestioner ? "block" : "none";
// Append shareable link
if (!isQuestioner) {
shareLink.innerHTML = getShareableLink(channel);
document.getElementById("copyLinkBtn").addEventListener("click", copyShareableLink); // Add event listener for the copy button
}
updateMessages(storage);
currentChannel = channel;
}
}
// Append messages from storage
function updateMessages(storage) {
if (!storage) return;
messageLog.innerText = ""; // clear message log
storage.messageLog.forEach((message) => {
if (message.type === "user_connected") {
const lang = document.documentElement.lang;
const connectedMessage =
translations[lang].userJoined
.replace("{username}", message.username)
.replace("{role}", message.role === "ques" ? translations[lang].questioner : translations[lang].responder) + "\n";
messageLog.innerText += connectedMessage;
}
if (message.type === "send_answer") {
const answer = message.answer;
const answerMessage = `Answer: ${answer}\n`;
messageLog.innerText += answerMessage;
}
});
}
// Add this new function to handle copying the link
function copyShareableLink() {
const linkElement = document.querySelector("#shareLink .shlink");
const link = linkElement.href;
navigator.clipboard
.writeText(link)
.then(() => {
const copyBtn = document.getElementById("copyLinkBtn");
const originalText = copyBtn.textContent;
copyBtn.textContent = translations[currentLanguage].copied || "Copied!";
setTimeout(() => {
copyBtn.textContent = originalText;
}, 2000);
})
.catch((err) => {
console.error("Failed to copy text: ", err);
});
}
// Expose necessary functions and variables to the global scope
window.updateMessages = updateMessages;