This repository has been archived by the owner on May 2, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03cfb68
commit 6b3c909
Showing
1 changed file
with
131 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,180 +1,181 @@ | ||
window.browser = (() => window.msBrowser || | ||
window.browser || | ||
window.chrome)(); | ||
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"); | ||
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 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; | ||
erbox.innerHTML = '' | ||
let cival = custominput.value | ||
let res = JSON.parse(fetchJSON(`${endpoint}/${cival}`)) | ||
let data = res.result | ||
if (data != null) { | ||
return false; | ||
return false | ||
} else if (data == null) { | ||
return true; | ||
return true | ||
} | ||
}; | ||
} | ||
let geturl = () => { | ||
let url = document.getElementById("urlinput").value; | ||
return url; | ||
}; | ||
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 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(); | ||
if (custominput.value == '') { | ||
window.location.hash = getrandom() | ||
check_is_unique() | ||
} else { | ||
window.location.hash = custominput.value; | ||
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; | ||
let url = window.location.hash.substr(1) | ||
let res = JSON.parse(fetchJSON(`${endpoint}/${url}`)) | ||
let data = res.result | ||
if (data != null) { | ||
genhash(); | ||
genhash() | ||
} | ||
}; | ||
} | ||
let copyer = (containerid) => { | ||
let elt = document.getElementById(containerid); | ||
let elt = document.getElementById(containerid) | ||
if (document.selection) { // IE | ||
if (elt.nodeName.toLowerCase() === "input") { | ||
elt.select(); | ||
document.execCommand("copy"); | ||
if (elt.nodeName.toLowerCase() === 'input') { | ||
elt.select() | ||
document.execCommand('copy') | ||
} else { | ||
let range = document.body.createTextRange(); | ||
range.moveToElementText(elt); | ||
range.select(); | ||
document.execCommand("copy"); | ||
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"); | ||
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 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); | ||
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"; | ||
}; | ||
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)); | ||
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); | ||
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"; | ||
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 🎉"; | ||
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); | ||
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"; | ||
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"; | ||
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", () => { | ||
} | ||
sbtn.addEventListener('click', shorturl) | ||
document.querySelector('#go-to-options').addEventListener('click', () => { | ||
if (browser.runtime.openOptionsPage) { | ||
browser.runtime.openOptionsPage(); | ||
browser.runtime.openOptionsPage() | ||
} else { | ||
window.open(browser.runtime.getURL('options.html')); | ||
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"]'); | ||
}) | ||
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); | ||
document.documentElement.setAttribute('data-theme', currentTheme) | ||
} | ||
//fetch(endpoint + "/key", { | ||
// fetch(endpoint + "/key", { | ||
// method: 'DELETE', | ||
//}); | ||
//let r = JSON.parse(fetchJSON(endpoint)).result; | ||
//document.getElementById("count").innerHTML = Object.keys(r).length + " urls minimalized"; | ||
// }); | ||
// let r = JSON.parse(fetchJSON(endpoint)).result; | ||
// document.getElementById("count").innerHTML = Object.keys(r).length + " urls minimalized"; |