mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
fix(filters): add fallback for clipboard api on http (#804)
add a fallback for clipboard api The Clipboard API requires HTTPS, so we need to add a fallback for HTTP. Using the old execCommand('copy') method.
This commit is contained in:
parent
64c2da591e
commit
de6638065a
1 changed files with 34 additions and 7 deletions
|
@ -406,12 +406,39 @@ const FilterItemDropdown = ({ filter, onToggle }: FilterItemDropdownProps) => {
|
|||
|
||||
const finalJson = discordFormat ? "```JSON\n" + json + "\n```" : json;
|
||||
|
||||
const copyTextToClipboard = (text: string) => {
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.style.position = "fixed";
|
||||
textarea.style.opacity = "0";
|
||||
textarea.value = text;
|
||||
document.body.appendChild(textarea);
|
||||
textarea.focus();
|
||||
textarea.select();
|
||||
|
||||
try {
|
||||
const successful = document.execCommand("copy");
|
||||
if (successful) {
|
||||
toast.custom((t) => <Toast type="success" body="Filter copied to clipboard." t={t} />);
|
||||
} else {
|
||||
toast.custom((t) => <Toast type="error" body="Failed to copy JSON to clipboard." t={t} />);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Unable to copy text", err);
|
||||
toast.custom((t) => <Toast type="error" body="Failed to copy JSON to clipboard." t={t} />);
|
||||
}
|
||||
|
||||
document.body.removeChild(textarea);
|
||||
};
|
||||
|
||||
if (navigator.clipboard) {
|
||||
navigator.clipboard.writeText(finalJson).then(() => {
|
||||
toast.custom((t) => <Toast type="success" body="Filter copied to clipboard." t={t} />);
|
||||
}, () => {
|
||||
toast.custom((t) => <Toast type="error" body="Failed to copy JSON to clipboard." t={t} />);
|
||||
});
|
||||
} else {
|
||||
copyTextToClipboard(finalJson);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue