Skip to content

Commit

Permalink
Add functionality to copy shortened URL and update internationalizati…
Browse files Browse the repository at this point in the history
…on support
  • Loading branch information
kevinlee-06 committed Jan 21, 2025
1 parent 0a91cfd commit c24d4a0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
10 changes: 9 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ <h2 id="response-h2" title="A json will be returned if success." data-i18n="resp
</label>
</h2>
<pre id="responseMessage"></pre>
<div>
<label for="shortenedUrl" data-i18n="shortened-url">Shortened URL:</label>
<input type="text" id="shortenedUrl" readonly onclick="copyText()"><br><br>

<button onclick="copyText()">Copy</button>

<p id="copyMessage" style="display: none; color: rgb(255, 56, 56);">Text copied!</p>
</div>
</div>
<div class="container">

Expand All @@ -76,7 +84,7 @@ <h2 id="response-h2" title="A json will be returned if success." data-i18n="resp
</select>
</div>
</div>

<script src="/public/scripts/copy.js"></script>
<script src="/public/scripts/delete.js"></script>
<script src="/public/scripts/post.js"></script>
<script src="/public/scripts/styles.js"></script>
Expand Down
14 changes: 14 additions & 0 deletions public/scripts/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function copyText() {
var copyText = document.getElementById("shortenedUrl");
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
// alert("已複製訊息: " + copyText.value);
const message = document.getElementById("copyMessage");
message.style.display = "block";

// Hide the message after 1.5 seconds
setTimeout(() => {
message.style.display = "none";
}, 1500);
}
4 changes: 4 additions & 0 deletions public/scripts/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ document.getElementById('deleteForm').addEventListener('submit', function(event)
password: deletePassword
};

document.getElementById('shortenedUrl').value = "";

fetch(`/${deleteId}`, {
method: 'DELETE',
headers: {
Expand All @@ -23,8 +25,10 @@ document.getElementById('deleteForm').addEventListener('submit', function(event)
})
.then(message => {
document.getElementById('responseMessage').textContent = message;
document.getElementById('response-h2').scrollIntoView({ behavior: 'smooth' });
})
.catch(error => {
document.getElementById('responseMessage').textContent = 'Error: ' + error.message;
document.getElementById('response-h2').scrollIntoView({ behavior: 'smooth' });
});
});
2 changes: 2 additions & 0 deletions public/scripts/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const translations = {
"theme-black": "Black",
"theme-tailwind": "Tailwind",
"language-label": "語言:",
"shortened-url": "Shortened URL:",
},
"zh-TW": {
"title": "Linklie - 短網址",
Expand All @@ -46,6 +47,7 @@ const translations = {
"theme-black": "黑色",
"theme-tailwind": "Tailwind",
"language-label": "Language:",
"shortened-url": "短網址:",
}
};

Expand Down
6 changes: 6 additions & 0 deletions public/scripts/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ document.getElementById('postForm').addEventListener('submit', function(event) {
})
.then(response => {
if (!response.ok) {
document.getElementById('shortenedUrl').value = "";
return response.text().then(text => {
throw new Error(text);
});
Expand All @@ -28,8 +29,13 @@ document.getElementById('postForm').addEventListener('submit', function(event) {
})
.then(data => {
document.getElementById('responseMessage').textContent = JSON.stringify(data, null, 2);
const host = window.location.host;
const protocol = window.location.protocol;
document.getElementById('shortenedUrl').value = `${protocol}//${host}/${data.id}`;
document.getElementById('response-h2').scrollIntoView({ behavior: 'smooth' });
})
.catch(error => {
document.getElementById('responseMessage').textContent = 'Error: ' + error.message;
document.getElementById('response-h2').scrollIntoView({ behavior: 'smooth' });
});
});
4 changes: 4 additions & 0 deletions public/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ h2, #responseMessage, input {

.language-selector, .theme-selector {
margin-bottom: 20px;
}

input[readonly] {
cursor: copy;
}

0 comments on commit c24d4a0

Please sign in to comment.