Replace deprecated document.execCommand with modern Clipboard API

- Replace document.execCommand('copy') with navigator.clipboard.writeText()
- Add fallback function for older browsers or non-secure contexts
- Fixes CSP eval blocking issues in modern browsers
- Improves clipboard functionality with better error handling
parent d8abcab4
......@@ -390,12 +390,34 @@ function extendToken(tokenId, tokenName) {
function copyToken() {
const tokenInput = document.getElementById('newTokenValue');
const tokenValue = tokenInput.value;
// Use modern Clipboard API if available
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(tokenValue).then(() => {
showAlert('Token copied to clipboard!', 'success');
}).catch(err => {
console.error('Failed to copy token:', err);
fallbackCopyToken(tokenInput);
});
} else {
// Fallback for older browsers or non-secure contexts
fallbackCopyToken(tokenInput);
}
}
function fallbackCopyToken(tokenInput) {
try {
tokenInput.select();
tokenInput.setSelectionRange(0, 99999); // For mobile devices
try {
document.execCommand('copy');
// Use the deprecated execCommand as fallback
const successful = document.execCommand('copy');
if (successful) {
showAlert('Token copied to clipboard!', 'success');
} else {
showAlert('Failed to copy token. Please copy manually.', 'warning');
}
} catch (err) {
console.error('Failed to copy token:', err);
showAlert('Failed to copy token. Please copy manually.', 'warning');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment