85 lines
2.6 KiB
JavaScript
85 lines
2.6 KiB
JavaScript
// Permalink functionality for Sunday Comics
|
|
// Handles copying comic permalinks to clipboard
|
|
|
|
(function() {
|
|
'use strict';
|
|
|
|
const permalinkButton = document.getElementById('permalink-button');
|
|
|
|
if (!permalinkButton) {
|
|
// Permalink feature not enabled or button not found
|
|
return;
|
|
}
|
|
|
|
// Get the site URL from the page
|
|
const siteUrl = document.body.getAttribute('data-site-url') || window.location.origin;
|
|
|
|
// Copy permalink when button is clicked
|
|
permalinkButton.addEventListener('click', function() {
|
|
const comicNumber = this.getAttribute('data-comic-number');
|
|
if (!comicNumber) return;
|
|
|
|
// Generate permalink URL
|
|
const permalink = `${siteUrl}/comic/${comicNumber}`;
|
|
|
|
// Copy to clipboard
|
|
copyToClipboard(permalink);
|
|
});
|
|
|
|
// Copy text to clipboard
|
|
function copyToClipboard(text) {
|
|
// Modern clipboard API
|
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
navigator.clipboard.writeText(text).then(function() {
|
|
showCopyFeedback();
|
|
}).catch(function() {
|
|
// Fallback for older browsers
|
|
fallbackCopy(text);
|
|
});
|
|
} else {
|
|
// Fallback for older browsers
|
|
fallbackCopy(text);
|
|
}
|
|
}
|
|
|
|
// Fallback copy method for older browsers
|
|
function fallbackCopy(text) {
|
|
// Create temporary textarea
|
|
const textarea = document.createElement('textarea');
|
|
textarea.value = text;
|
|
textarea.style.position = 'fixed';
|
|
textarea.style.opacity = '0';
|
|
document.body.appendChild(textarea);
|
|
textarea.focus();
|
|
textarea.select();
|
|
|
|
try {
|
|
document.execCommand('copy');
|
|
showCopyFeedback();
|
|
} catch (err) {
|
|
alert('Failed to copy. Please copy manually: ' + text);
|
|
}
|
|
|
|
document.body.removeChild(textarea);
|
|
}
|
|
|
|
// Show visual feedback that the permalink was copied
|
|
function showCopyFeedback() {
|
|
const originalText = permalinkButton.textContent;
|
|
permalinkButton.textContent = 'Copied!';
|
|
permalinkButton.classList.add('copied');
|
|
|
|
setTimeout(function() {
|
|
permalinkButton.textContent = originalText;
|
|
permalinkButton.classList.remove('copied');
|
|
}, 2000);
|
|
}
|
|
|
|
// Update permalink button when comic changes via client-side navigation
|
|
window.addEventListener('comicUpdated', function(event) {
|
|
if (event.detail && event.detail.comicNumber && permalinkButton) {
|
|
permalinkButton.setAttribute('data-comic-number', event.detail.comicNumber);
|
|
}
|
|
});
|
|
})();
|