Automatically add https if no protocol specified for longurl

This commit is contained in:
SinTan1729 2022-11-11 18:52:22 -06:00
parent 99073bfd6c
commit 2c8076e2e1
2 changed files with 21 additions and 5 deletions

View File

@ -67,7 +67,8 @@
Shorten</legend> Shorten</legend>
<div class="pure-control-group"> <div class="pure-control-group">
<label for="longUrl">Long URL</label> <label for="longUrl">Long URL</label>
<input type="url" name="longUrl" id="longUrl" placeholder="Please enter a valid URL" required /> <input type="url" name="longUrl" id="longUrl" placeholder="Please enter a valid URL"
onblur="addProtocol(this)" required />
</div> </div>
<div class=" pure-control-group"> <div class=" pure-control-group">
<label for="shortUrl">Short URL (optional)</label> <label for="shortUrl">Short URL (optional)</label>

View File

@ -66,10 +66,25 @@ const TR = (row, site) => {
const copyShortUrl = async (link) => { const copyShortUrl = async (link) => {
const site = await getSiteUrl(); const site = await getSiteUrl();
try {
navigator.clipboard.writeText(`${site}/${link}`); navigator.clipboard.writeText(`${site}/${link}`);
addAlertBox(`Short URL ${link} copied to clipboard!`, "green"); addAlertBox(`Short URL ${link} was copied to clipboard!`, "green");
} catch (e) {
console.log(e);
addAlertBox("Could not copy short URL to clipboard, please do it manually.", "red");
}
}; };
const addProtocol = (input) => {
var url = input.value.trim();
if (url != "" && !~url.indexOf(":/")) {
url = "https://" + url;
}
input.value = url;
return input
}
const A = (s) => `<a href='${s}'>${s}</a>`; const A = (s) => `<a href='${s}'>${s}</a>`;
const A_INT = (s, t) => `<a href="javascript:copyShortUrl('${s}');">${t}/${s}</a>`; const A_INT = (s, t) => `<a href="javascript:copyShortUrl('${s}');">${t}/${s}</a>`;
@ -114,7 +129,7 @@ const submitForm = () => {
}) })
.then(res => { .then(res => {
if (!res.ok) { if (!res.ok) {
addAlertBox("Short URL not valid or already in use!", "red"); addAlertBox("Short URL is not valid or it's already in use!", "red");
return "error"; return "error";
} }
else { else {