This repository has been archived by the owner on May 2, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
181 lines (180 loc) · 5.45 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
window.browser = (() => window.msBrowser ||
window.browser ||
window.chrome)()
browser.tabs.query({
active: true,
currentWindow: true
}, tabs => {
document.getElementById('urlinput').value = tabs[0].url
})
const erbox = document.getElementById('erbox')
const custominput = document.getElementById('custominput')
const output = document.getElementById('output')
const rotate = document.getElementById('rotate')
const status = document.getElementById('status')
const alias = document.getElementById('alias')
const sucess = document.getElementById('sucess')
const shortenedURL = document.getElementById('shortenedURL')
const sbtn = document.getElementById('sbtn')
let pushJSON = (url, data) => {
let request = new XMLHttpRequest()
request.open('POST', url)
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8')
request.send(JSON.stringify(data))
}
let cinp = () => {
erbox.innerHTML = ''
let cival = custominput.value
let res = JSON.parse(fetchJSON(`${endpoint}/${cival}`))
let data = res.result
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 (custominput.value == '') {
window.location.hash = getrandom()
check_is_unique()
} else {
window.location.hash = custominput.value
}
}
let check_is_unique = () => {
let url = window.location.hash.substr(1)
let res = JSON.parse(fetchJSON(`${endpoint}/${url}`))
let data = res.result
if (data != null) {
genhash()
}
}
let copyer = (containerid) => {
let elt = document.getElementById(containerid)
if (document.selection) { // IE
if (elt.nodeName.toLowerCase() === 'input') {
elt.select()
document.execCommand('copy')
} else {
let 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 {
let range_ = document.createRange()
range_.selectNode(elt)
window.getSelection().removeAllRanges()
window.getSelection().addRange(range_)
document.execCommand('copy')
}
}
}
let send_request = (url) => {
let myurl = url
let address = `${endpoint}/${window.location.hash.substr(1)}`
let hash = window.location.hash.substr(1)
// console.log(address)
pushJSON(address, myurl)
output.style.display = 'block'
shortenedURL.value = `https://mnmlurl.ml/#${hash}`
copyer('shortenedURL')
sucess.innerHTML = 'Short url copied to clipboard 🚀'
rotate.classList.remove('spinning')
rotate.style.display = 'none'
status.innerHTML = 'Shorten'
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
let shorturl = async () => {
erbox.innerHTML = ''
sucess.innerHTML = ''
rotate.style.display = 'inline-block'
rotate.setAttribute('class', 'spinning')
status.innerHTML = ''
output.style.display = 'none'
await sleep(1000)
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) {
erbox.innerHTML = 'Invalid url 😒'
rotate.classList.remove('spinning')
rotate.style.display = 'none'
status.innerHTML = 'Shorten'
sucess.innerHTML = ''
output.style.display = 'none'
} else {
erbox.innerHTML = ''
if (custominput.value == '') {
genhash()
send_request(longurl)
alias.innerHTML = 'Shortened 🎉'
} else {
if (cre.test(custominput.value)) {
if (cinp()) {
alias.innerHTML = 'Alias available ✨'
rotate.classList.remove('spinning')
rotate.style.display = 'none'
status.innerHTML = 'Shorten'
genhash()
send_request(longurl)
} else {
erbox.innerHTML = '😒 Alias already in use, choose another'
custominput.placeholder = custominput.value
custominput.value = ''
rotate.classList.remove('spinning')
rotate.style.display = 'none'
status.innerHTML = 'Shorten'
sucess.innerHTML = ''
output.style.display = 'none'
}
} else {
erbox.innerHTML = '😮 Invalid custom alias, use only alphanumerics & underscore'
custominput.placeholder = custominput.value
custominput.value = ''
rotate.classList.remove('spinning')
rotate.style.display = 'none'
status.innerHTML = 'Shorten'
sucess.innerHTML = ''
output.style.display = 'none'
}
}
}
}
sbtn.addEventListener('click', shorturl)
document.querySelector('#go-to-options').addEventListener('click', () => {
if (browser.runtime.openOptionsPage) {
browser.runtime.openOptionsPage()
} else {
window.open(browser.runtime.getURL('options.html'))
}
})
const currentTheme = localStorage.getItem('theme') ? localStorage.getItem('theme') : null
const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]')
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme)
}
// fetch(endpoint + "/key", {
// method: 'DELETE',
// });
// let r = JSON.parse(fetchJSON(endpoint)).result;
// document.getElementById("count").innerHTML = Object.keys(r).length + " urls minimalized";