-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (127 loc) · 5.05 KB
/
index.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
let pushJSON = (address, longurl, shorturl) => {
let request = new XMLHttpRequest();
request.open('POST', address);
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
let data = {
"l": longurl,
"s": shorturl
};
request.send(JSON.stringify(data));
};
let cinp = () => {
document.getElementById("erbox").innerHTML = "";
let cival = document.getElementById("custominput").value;
let res = JSON.parse(fetchJSON(endpoint + '/?q=s:' + cival))
if (res.length === 0){
return true;
}
res = res[0]["l"]
let data = res;
if (data != null) {
return false;
} else if (data == null) {
return true;
}
};
let geturl = () => {
let url = document.getElementById("urlinput").value;
return url;
};
let getrandom = () => {
let text = "";
let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
};
let genhash = () => {
if (document.getElementById("custominput").value == "") {
window.location.hash = getrandom();
check_is_unique();
} else {
window.location.hash = document.getElementById("custominput").value;
}
};
let check_is_unique = () => {
let url = window.location.hash.substr(1);
let res = JSON.parse(fetchJSON(endpoint + '/?q=s:' + url))[0];
let data = res;
if (data != null) {
genhash();
}
};
let copyer = (containerid) => {
let elt = document.getElementById(containerid);
if (document.selection) { // IE
if (elt.nodeName.toLowerCase() === "input") {
document.getElementById(containerid).select();
document.execCommand("copy");
} else {
let range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select();
document.execCommand("copy");
}
} else if (window.getSelection) {
if (elt.nodeName.toLowerCase() === "input") {
document.getElementById(containerid).select();
document.execCommand("copy");
} else {
let range_ = document.createRange();
range_.selectNode(document.getElementById(containerid));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range_);
document.execCommand("copy");
}
}
};
let send_request = (url) => {
let longurl = url;
let shorturl = window.location.hash.substr(1)
let address = endpoint + "/";
// console.log(address)
pushJSON(address, longurl, shorturl);
document.getElementById('shortenedURL').value = window.location.href;
document.getElementById('sucess').innerHTML = "Short URL Copied to Clipboard!";
copyer("shortenedURL");
};
let shorturl = () => {
let longurl = geturl();
let re = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/;
let cre = /^([a-zA-Z0-9 _-]+)$/;
let protocol_ok = re.test(longurl);
if (!protocol_ok) {
document.getElementById("erbox").style.color = "red";
document.getElementById("erbox").innerHTML = "❌ Invalid URL";
} else {
document.getElementById("erbox").innerHTML = "";
if (document.getElementById("custominput").value == "") {
genhash();
send_request(longurl);
} else {
if (cre.test(document.getElementById("custominput").value)) {
if (cinp()) {
document.getElementById("erbox").style.color = "black";
document.getElementById("erbox").innerHTML = " Custom Address Available ✔️";
genhash();
send_request(longurl);
} else {
document.getElementById("erbox").style.color = "red";
document.getElementById("erbox").innerHTML = "❌ Custom Address Already Used, Choose Another";
document.getElementById("custominput").placeholder = document.getElementById("custominput").value;
document.getElementById("custominput").value = "";
}
} else {
document.getElementById("erbox").style.color = "red";
document.getElementById("erbox").innerHTML = "Invalid Custom URL! Use only Alphanumerics and underscore!";
document.getElementById("custominput").placeholder = document.getElementById("custominput").value;
document.getElementById("custominput").value = "";
}
}
}
};
document.getElementById("sbtn").addEventListener("click", shorturl);
console.log(`
design by anurag singh
PLEASE DON'T TYPE ANYTHING BELLOW UNLESS YOU ARE A DEVELOPER!
`)