-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (134 loc) · 4.12 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
const urlinput = document.getElementById('urlinput')
const custominput = document.getElementById('custominput')
const sbtn = document.getElementById('sbtn')
const status = document.getElementById('status')
const erbox = document.getElementById('erbox')
const output = document.getElementById('output')
const alias = document.getElementById('alias')
const shortenedURL = document.getElementById('shortenedURL')
const sucess = document.getElementById('sucess')
const qr = document.getElementById('qr')
const pushJSON = (url, data) => {
const request = new XMLHttpRequest()
request.open('POST', url, true)
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8')
request.send(JSON.stringify(data))
}
const cinp = () => {
const cival = custominput.value
const res = JSON.parse(fetchJSON(`${endpoint}/${cival}`))
const data = res.result
if (data != null) {
return false
} else if (data == null) {
return true
}
}
const geturl = () => {
const url = urlinput.value
return url
}
const getrandom = () => {
let text = ''
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for (let i = 0; i < 5; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length))
}
return text
}
const genhash = () => {
if (custominput.value == '') {
window.location.hash = getrandom()
check_is_unique()
} else {
window.location.hash = custominput.value
}
}
const check_is_unique = () => {
const url = window.location.hash.substr(1)
const res = JSON.parse(fetchJSON(`${endpoint}/${url}`))
const data = res.result
if (data != null) {
genhash()
}
}
const copyer = (containerid) => {
const elt = document.getElementById(containerid)
if (document.selection) { // IE
if (elt.nodeName.toLowerCase() === 'input') {
elt.select()
document.execCommand('copy')
} else {
const range = document.body.createTextRange()
range.moveToElementText(elt)
range.select()
document.execCommand('copy')
}
} else if (window.getSelection) {
if (elt.nodeName.toLowerCase() === 'input') {
elt.select()
document.execCommand('copy')
} else {
const range_ = document.createRange()
range_.selectNode(elt)
window.getSelection().removeAllRanges()
window.getSelection().addRange(range_)
document.execCommand('copy')
}
}
}
const createFrame = src => `<a href='${src}' target='_blank'><img src='${src}' alt='QR code'></a>`
const send_request = (url) => {
const myurl = url
const address = `${endpoint}/${window.location.hash.substr(1)}`
pushJSON(address, myurl)
urlinput.value = ''
custominput.value = ''
status.innerHTML = 'shorten'
output.style.display = 'block'
shortenedURL.value = window.location.href
copyer('shortenedURL')
sucess.innerHTML = 'short url copied to clipboard'
qr.innerHTML = createFrame(`https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${shortenedURL.value}`)
}
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const shorturl = async () => {
status.innerHTML = 'shortening...'
erbox.style.display = 'none'
output.style.display = 'none'
await sleep(250)
const longurl = geturl()
const re = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/
const cre = /^([a-zA-Z0-9_-]+)$/
const protocol_ok = re.test(longurl)
if (!protocol_ok) {
status.innerHTML = 'shorten'
erbox.style.display = 'block'
erbox.innerHTML = 'invalid url'
} else {
if (custominput.value == '') {
genhash()
send_request(longurl)
alias.innerHTML = 'shortened'
} else {
if (cre.test(custominput.value)) {
if (cinp()) {
genhash()
send_request(longurl)
alias.innerHTML = 'alias available'
} else {
custominput.value = ''
status.innerHTML = 'shorten'
erbox.style.display = 'block'
erbox.innerHTML = 'alias already in use, choose another'
}
} else {
custominput.value = ''
status.innerHTML = 'shorten'
erbox.style.display = 'block'
erbox.innerHTML = 'invalid optional custom alias, use only alphanumerics & underscore'
}
}
}
}
sbtn.addEventListener('click', shorturl)