CHANGE clipboard function for Windows with fallback option

This commit is contained in:
CaffeineFueled 2025-05-08 18:12:25 +02:00
parent 83f31429ce
commit ea5e31d07d

View file

@ -319,6 +319,44 @@
</footer> </footer>
<script> <script>
// Fallback function for clipboard operations (better Windows compatibility)
function copyTextToClipboardFallback(text) {
// Create a temporary textarea element
const textArea = document.createElement("textarea");
// Set its value to the text we want to copy
textArea.value = text;
// Make it invisible but part of the document
textArea.style.position = "fixed";
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.width = "2em";
textArea.style.height = "2em";
textArea.style.padding = "0";
textArea.style.border = "none";
textArea.style.outline = "none";
textArea.style.boxShadow = "none";
textArea.style.background = "transparent";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
// Execute the copy command
const successful = document.execCommand("copy");
if (!successful) {
console.warn("Fallback clipboard copy failed");
}
} catch (err) {
console.error("Fallback clipboard error:", err);
}
// Clean up
document.body.removeChild(textArea);
}
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
// Initialize copy functionality // Initialize copy functionality
const copyButton = document.getElementById('copyButton'); const copyButton = document.getElementById('copyButton');
@ -380,12 +418,26 @@
textToCopy.select(); textToCopy.select();
textToCopy.setSelectionRange(0, 99999); // For mobile devices textToCopy.setSelectionRange(0, 99999); // For mobile devices
// Copy the text to clipboard // Copy the text to clipboard using a more compatible approach
try {
// First try the Clipboard API
navigator.clipboard.writeText(textToCopy.value) navigator.clipboard.writeText(textToCopy.value)
.then(() => {
console.log('Clipboard API succeeded');
copySuccess.textContent = '✓ Text copied to clipboard!';
})
.catch(err => { .catch(err => {
console.error('Failed to copy text: ', err); console.warn('Clipboard API failed, trying fallback method:', err);
// Even if copy fails, keep the timer going // Fallback for older browsers and better Windows compatibility
copyTextToClipboardFallback(textToCopy.value);
copySuccess.textContent = '✓ Text copied to clipboard! (fallback method)';
}); });
} catch (err) {
console.warn('Primary clipboard method failed, using fallback:', err);
// If the Clipboard API isn't available at all, use fallback
copyTextToClipboardFallback(textToCopy.value);
copySuccess.textContent = '✓ Text copied to clipboard! (fallback method)';
}
// After 15 seconds, show the button again (fallback) // After 15 seconds, show the button again (fallback)
setTimeout(() => { setTimeout(() => {