Code refactoring after #878

This commit is contained in:
Alex X
2024-04-29 11:54:00 +03:00
parent 215d55771c
commit a1983c725d

View File

@@ -167,55 +167,29 @@ Telegram: rtmps://xxx-x.rtmp.t.me/s/xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxx</pre>
} }
function copyTextToClipboard(text) { function copyTextToClipboard(text) {
// Modern approach with the Clipboard API // https://web.dev/patterns/clipboard/copy-text
if (navigator.clipboard && window.isSecureContext) { if (navigator.clipboard && window.isSecureContext) {
// Navigator clipboard is available navigator.clipboard.writeText(text).catch(err => {
navigator.clipboard.writeText(text).then(function() { console.error(err.name, err.message);
console.log('Async: Copying to clipboard was successful!');
}, function(err) {
console.error('Async: Could not copy text: ', err);
}); });
} else { } else {
// Fallback using document.execCommand() const textarea = document.createElement('textarea');
try {
// Create a temporary element to hold the text to copy
var textarea = document.createElement("textarea");
// Make it almost invisible and uneditable
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';
// Set the text content and append the textarea to the body
textarea.value = text; textarea.value = text;
textarea.style.opacity = '0';
document.body.appendChild(textarea); document.body.appendChild(textarea);
// Select all the text in the textarea
textarea.focus(); textarea.focus();
textarea.select(); textarea.select();
// Attempt to copy the text selected in the textarea try {
var successful = document.execCommand('copy'); document.execCommand('copy');
if (successful) {
console.log('Fallback: Copying text command was successful!');
} else {
console.error('Fallback: Could not copy text');
}
// Cleanup - remove the temporary textarea
document.body.removeChild(textarea);
} catch (err) { } catch (err) {
console.error('Fallback: Exception while trying to copy', err); console.error(err.name, err.message);
}
}
} }
document.body.removeChild(textarea);
}
}
document.getElementById('shareadd').addEventListener('click', ev => { document.getElementById('shareadd').addEventListener('click', ev => {
ev.preventDefault(); ev.preventDefault();