fix(filters): export not working with Safari (#1505)

* fix(filters): export not working with Safari

* speculative fix for safari

---------

Co-authored-by: s0up4200 <s0up4200@pm.me>
This commit is contained in:
ze0s 2024-04-25 17:01:30 +02:00 committed by GitHub
parent 9d08f149b4
commit d558db231c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 53 deletions

View file

@ -152,3 +152,39 @@ export const RandomLinuxIsos = (count: number) => {
return Array.from({ length: count }, () => linuxIsos[Math.floor(Math.random() * linuxIsos.length)]);
};
export async function CopyTextToClipboard(text: string) {
if ("clipboard" in navigator) {
// Safari requires clipboard operations to be directly triggered by a user interaction.
// Using setTimeout with a delay of 0 ensures the clipboard operation is deferred until
// after the current call stack has cleared, effectively placing it outside of the
// immediate execution context of the user interaction event. This workaround allows
// the clipboard operation to bypass Safari's security restrictions.
setTimeout(async () => {
try {
await navigator.clipboard.writeText(text);
console.log("Text copied to clipboard successfully.");
} catch (err) {
console.error("Copy to clipboard unsuccessful: ", err);
}
}, 0);
} else {
// fallback for browsers that do not support the Clipboard API
copyTextToClipboardFallback(text);
}
}
function copyTextToClipboardFallback(text: string) {
const textarea = document.createElement("textarea");
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand('copy');
console.log("Text copied to clipboard successfully.");
} catch (err) {
console.error('Failed to copy text using fallback method: ', err);
}
document.body.removeChild(textarea);
}