CHANGE clipboard function for Windows with fallback option
This commit is contained in:
parent
83f31429ce
commit
ea5e31d07d
1 changed files with 58 additions and 6 deletions
64
index.html
64
index.html
|
@ -319,6 +319,44 @@
|
|||
</footer>
|
||||
|
||||
<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() {
|
||||
// Initialize copy functionality
|
||||
const copyButton = document.getElementById('copyButton');
|
||||
|
@ -380,12 +418,26 @@
|
|||
textToCopy.select();
|
||||
textToCopy.setSelectionRange(0, 99999); // For mobile devices
|
||||
|
||||
// Copy the text to clipboard
|
||||
navigator.clipboard.writeText(textToCopy.value)
|
||||
.catch(err => {
|
||||
console.error('Failed to copy text: ', err);
|
||||
// Even if copy fails, keep the timer going
|
||||
});
|
||||
// Copy the text to clipboard using a more compatible approach
|
||||
try {
|
||||
// First try the Clipboard API
|
||||
navigator.clipboard.writeText(textToCopy.value)
|
||||
.then(() => {
|
||||
console.log('Clipboard API succeeded');
|
||||
copySuccess.textContent = '✓ Text copied to clipboard!';
|
||||
})
|
||||
.catch(err => {
|
||||
console.warn('Clipboard API failed, trying fallback method:', err);
|
||||
// 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)
|
||||
setTimeout(() => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue