-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
58 lines (47 loc) · 1.39 KB
/
main.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
var endpoint = "https://www.jsonstore.io/8ba4fd855086288421f770482e372ccb5a05d906269a34da5884f39eed0418a1";
function geturl(){
var url = document.getElementById("urlinput").value;
var protocol_ok = url.startsWith("http://") || url.startsWith("https://") || url.startsWith("ftp://");
if(!protocol_ok){
newurl = "http://"+url;
return newurl;
}else{
return url;
}
}
function getrandom() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function genhash(){
if (window.location.hash == ""){
window.location.hash = getrandom();
}
}
function send_request(url) {
this.url = url;
$.ajax({
'url': endpoint + "/" + window.location.hash.substr(1),
'type': 'POST',
'data': JSON.stringify(this.url),
'dataType': 'json',
'contentType': 'application/json; charset=utf-8'
})
}
function shorturl(){
var longurl = geturl();
genhash();
send_request(longurl);
}
var hashh = window.location.hash.substr(1)
if (window.location.hash != "") {
$.getJSON(endpoint + "/" + hashh, function (data) {
data = data["result"];
if (data != null) {
window.location.href = data;
}
});
}