add custom shortnames

parent 813f8343
......@@ -26,9 +26,19 @@
<div class="input-group mb-3 input-group-lg">
<input type="text" class="form-control" type="text" placeholder="Paste a link to shorten it" autocomplete="off" autocorrect="off" autocapitalize="off" id="urltext" aria-describedby="urlclick">
<div class="input-group-append">
<button class="btn btn-danger" type="button" id="urlclick">Shorten</button>
<button class="btn btn-danger" type="button" id="urlclick">Shorten</button>
</div>
</div>
<span class="btn btn-info" id="customshort">Custom Short</span>
<div class="input-group mb-3 input-group-lg" id="customMenu" style="display: none;">
<input class="form-control" type="text" placeholder="Type your custom shortname" autocomplete="off" autocorrect="off" autocapitalize="off" id="customshorturl" />
<div class="input-group-append" id="shortBtn">
<button class="btn btn-info" type="button" id="customurlclick">Shorten</button>
</div>
</div>
<span class="text-danger" id="error"></span>
<div class="input-group mb-3 input-group-lg">
<input class="form-control" type="text" id="shorturl" style="display:none;" />
</div>
......
......@@ -3,6 +3,8 @@
document.getElementById("urlclick").addEventListener("click", short);
document.getElementById("apiclick").addEventListener("click", saveapi);
document.getElementById("clearapi").addEventListener("click", clearApi);
document.getElementById("customshort").addEventListener("click", customMenu);
document.getElementById("customurlclick").addEventListener("click", customShort);
var apikey = localStorage.getItem('apikey');
......@@ -43,6 +45,7 @@ function shortUrl(apikey, long_url) {
document.getElementById("shorturl").value = this.responseText;
document.getElementById("shorturl").setAttribute("style","display:block;text-align: center;font-weight: bold;");
document.getElementById("info").innerHTML = "Link is copied to clipboard";
document.getElementById("customshort").setAttribute("style","display:none;");
copyToClipboard();
}
if (this.status == 422) {
......@@ -58,6 +61,50 @@ function shortUrl(apikey, long_url) {
xhttp.send("long_url="+long_url+"&apikey="+apikey);
}
function customMenu(){
document.getElementById("customshort").setAttribute("style","display:none;");
document.getElementById("customMenu").setAttribute("style","display;");
document.getElementById("urlclick").setAttribute("style","display:none;");
}
function customShort() {
var long_url = document.getElementById("urltext").value;
var shorturl = document.getElementById("customshorturl").value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.status == 200 || this.status == 201) {
document.getElementById("customshorturl").value = this.responseText;
document.getElementById("customshorturl").setAttribute("style","text-align: center;font-weight: bold;");
document.getElementById("shortBtn").setAttribute("style","display:none;");
document.getElementById("error").setAttribute("style","display:none;");
document.getElementById("info").innerHTML = "Link is copied to clipboard";
copyCutomToClipboard();
}
if (this.status == 401) {
document.getElementById("info").innerHTML = "No valid API key";
}
if (this.status == 422) {
document.getElementById("info").innerHTML = "No valid entries";
}
if (this.status == 423) {
document.getElementById("error").innerHTML = "This shortname isn't available anymore.";
}
}
var apiserver = localStorage.getItem('apiserver');
xhttp.open("POST", apiserver+"/custom", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("long_url="+long_url+"&apikey="+apikey+"&short="+shorturl);
}
function copyCutomToClipboard() {
var copyText = document.getElementById("customshorturl");
copyText.select();
document.execCommand("copy");
}
function copyToClipboard() {
var copyText = document.getElementById("shorturl");
copyText.select();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment