gomoku/public/scripts/copy-game-link.js

32 lines
1.2 KiB
JavaScript

function copyGameLink() {
const gameId = document.querySelector('meta[name="gameId"]').content;
const gameLink = `${window.location.origin}${window.location.pathname}?gameId=${gameId}`;
navigator.clipboard
.writeText(gameLink)
.then(() => {
const copyLinkButton = document.getElementById('copy-link-button');
const copyLinkText = document.getElementById('copy-link-text');
const originalIconSrc = '/icons/clipboard-copy.svg';
const originalTextContent = 'Click to copy game link!';
if (window.copyButtonTimeoutId) {
clearTimeout(window.copyButtonTimeoutId);
}
copyLinkButton.querySelector('use').href = '/icons/copy-success.svg';
copyLinkText.textContent = 'Game link copied!';
copyLinkButton.classList.add('copied-state');
window.copyButtonTimeoutId = setTimeout(() => {
copyLinkButton.querySelector('use').href = originalIconSrc;
copyLinkText.textContent = originalTextContent;
copyLinkButton.classList.remove('copied-state');
window.copyButtonTimeoutId = null;
}, 3000);
})
.catch((err) => {
console.error('Failed to copy link: ', err);
});
}