Skip to content

Commit

Permalink
Enhance copy functionality to use clipboard API and fallback for lega…
Browse files Browse the repository at this point in the history
…cy support
  • Loading branch information
kevinlee-06 committed Feb 6, 2025
1 parent bd55bfd commit 11ab1f2
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions public/scripts/copy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
function copyText() {
var copyText = document.getElementById("shortenedUrl");
copyText.select();
copyText.setSelectionRange(0, 99999);
const textToCopy = document.getElementById("shortenedUrl");
navigator.clipboard
.writeText(textToCopy)
.then(() => {
displayCopyMessage();
})
.catch((err) => {
console.error("Failed to copy text: ", err);
legacyCopyTextApi();
});
}

function legacyCopyTextApi() {
const textToCopy = document.getElementById("shortenedUrl");
textToCopy.select();
textToCopy.setSelectionRange(0, 99999);
document.execCommand("copy");
// alert("已複製訊息: " + copyText.value);
displayCopyMessage();
}

function displayCopyMessage() {
const message = document.getElementById("copyMessage");
message.style.display = "block";

// Hide the message after 1.5 seconds
setTimeout(() => {
message.style.display = "none";
}, 1500);
Expand Down

0 comments on commit 11ab1f2

Please sign in to comment.