-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (57 loc) · 1.89 KB
/
app.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
const urlParams = new URLSearchParams(location.search);
let roomId = urlParams.get("id");
//To generate unique URL id
if (!roomId) {
roomId = Math.floor(Math.random() * 100000 + 100000);
window.location.search = `id=${roomId}`;
}
//Get WebSocket APIs
const wsurl = `wss://s4287.nyc3.piesocket.com/v3/${roomId}?api_key=FBNWDiIv2CzDLLZ4ykNRcdT71tF2mRbQ1BQWvd67¬ify_self=1`;
const socket = new WebSocket(wsurl);
const debounce = (func, timer = 1000) => {
let timeId = null;
return (...args) => {
if (timeId) {
clearTimeout(timeId);
}
timeId = setTimeout(() => {
func(...args);
}, timer);
};
};
//Getting the text from textarea
const textArea = document.querySelector("textarea");
socket.onopen = () => { };
socket.onmessage = (e) => {
textArea.value = e.data;
};
textArea.addEventListener("input", debounce((e) => {
socket.send(e.target.value);
})
);
//To copy the url
function copyToClipboard(text) {
var input = document.body.appendChild(document.createElement("input"));
input.value = window.location.href;
input.focus();
input.select();
document.getElementById("copymsg").style.display = "inline";
document.execCommand('copy');
setTimeout(function () {
document.getElementById("copymsg").style.display = "none";
}, 700);
input.parentNode.removeChild(input);
}
//To use "tab" key in textbox
document.getElementById('textbox').addEventListener('keydown', function (e) {
if (e.key == 'Tab') {
e.preventDefault();
var start = this.selectionStart;
var end = this.selectionEnd;
// set textarea value to: text before caret + tab + text after caret
this.value = this.value.substring(0, start) + "\t" + this.value.substring(end);
// put caret at right position again
this.selectionStart =
this.selectionEnd = start + 1;
}
});