mirror of
https://github.com/diced/zipline.git
synced 2025-04-11 23:31:17 -05:00
url shortening
This commit is contained in:
parent
0e03736923
commit
07aad78993
2 changed files with 63 additions and 5 deletions
|
@ -200,18 +200,53 @@ document.getElementById('saveUser').addEventListener('click', async () => {
|
|||
});
|
||||
});
|
||||
|
||||
document.getElementById('copyToken').addEventListener('click', async () => {
|
||||
async function shortURL(token, url) {
|
||||
if (whitespace(url)) return showAlert('error', 'Please input a URL.')
|
||||
const res = await fetch('/api/shorten', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'authorization': token
|
||||
},
|
||||
body: JSON.stringify({
|
||||
url
|
||||
})
|
||||
});
|
||||
try {
|
||||
let te = await res.text();
|
||||
Swal.fire(
|
||||
'URL Shortened!',
|
||||
`Shorten: <a target="_blank" href="${te}">${te}</a>`,
|
||||
'success'
|
||||
);
|
||||
return;
|
||||
} catch (e) {
|
||||
if (e.message.startsWith('Unexpected token < in JSON at position')) {
|
||||
let te = await res.text();
|
||||
Swal.fire(
|
||||
'URL Shortened!',
|
||||
`Shorten: <a target="_blank" href="${te}">${te}</a>`,
|
||||
'success'
|
||||
);
|
||||
return;
|
||||
} else {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const copyToken = (token) => {
|
||||
Swal.fire({
|
||||
title: 'Are you sure?',
|
||||
text: "You are proceeding to copy your token, make sure NO ONE sees it.",
|
||||
icon: 'warning',
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#3085d6',
|
||||
confirmButtonColor: '#3`085d6',
|
||||
cancelButtonColor: '#d33',
|
||||
confirmButtonText: 'Yes, copy it!'
|
||||
}).then((result) => {
|
||||
if (result.value) {
|
||||
copyText("<%= user.token %>");
|
||||
copyText(token);
|
||||
Swal.fire(
|
||||
'Copied!',
|
||||
'Your API Token has been copied.',
|
||||
|
@ -219,7 +254,7 @@ document.getElementById('copyToken').addEventListener('click', async () => {
|
|||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
document.getElementById('regenToken').addEventListener('click', async () => {
|
||||
Swal.fire({
|
||||
title: 'Are you sure?',
|
||||
|
|
|
@ -31,6 +31,10 @@
|
|||
<a style="border-radius: 50px;" class="nav-link active" id="home-tab" data-toggle="tab" href="#home"
|
||||
role="tab" aria-controls="home" aria-selected="true">Home</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a style="border-radius: 50px;" class="nav-link" id="urls-tab" data-toggle="tab" href="#urls"
|
||||
role="tab" aria-controls="urls" aria-selected="false">URL Shortener</a>
|
||||
</li>
|
||||
<li class="nav-item" id="updateImages">
|
||||
<a style="border-radius: 50px;" class="nav-link" id="profile-tab" data-toggle="tab" href="#images"
|
||||
role="tab" aria-controls="images" aria-selected="false">Your
|
||||
|
@ -51,7 +55,7 @@
|
|||
<p>You have <b><%= images.length %></b> images saved, and you
|
||||
<%= user.administrator ? 'are an administrator' : 'are not an administrator' %>.</p>
|
||||
<h4>API Token</h4>
|
||||
<button type="button" class="btn btn-sm btn-primary" id="copyToken"
|
||||
<button type="button" class="btn btn-sm btn-primary" onclick="copyToken('<%=user.token%>')"
|
||||
style="border-radius: 50px;">Copy</button>
|
||||
<button type="button" class="btn btn-sm btn-danger" id="regenToken"
|
||||
style="border-radius: 50px;">Regenerate</button>
|
||||
|
@ -81,6 +85,25 @@
|
|||
</div>
|
||||
|
||||
</div>
|
||||
<div class="tab-pane fade m-3" id="urls" role="tabpanel" aria-labelledby="urls-tab">
|
||||
<h3>Shorten a URL</h3>
|
||||
<form>
|
||||
<div class="form-group">
|
||||
<label for="urlToShorten">URL</label>
|
||||
<input type="text" class="form-control" id="urlToShorten"
|
||||
placeholder="URL">
|
||||
</div>
|
||||
|
||||
<button type="button"
|
||||
class="btn btn-primary"
|
||||
id="shortenURL"
|
||||
onclick="shortURL('<%=user.token%>', document.getElementById('urlToShorten').value)"
|
||||
style="border-radius: 50px; width:100%;"
|
||||
>Shorten</button>
|
||||
</form>
|
||||
<h3>Your Shortens</h3>
|
||||
<p>Comming soon ™</p>
|
||||
</div>
|
||||
<% if (user.administrator) { %>
|
||||
<div class="tab-pane fade m-3" id="users" role="tabpanel" aria-labelledby="users-tab">
|
||||
<button style="border-radius: 50px; margin-bottom: 10px;" type="button" class="btn btn-primary" data-toggle="modal"
|
||||
|
|
Loading…
Add table
Reference in a new issue