From 3828e475cc8860e74cdfd8a70b4f886de7f9c5c3 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Fri, 27 Sep 2024 10:26:47 +0300 Subject: [PATCH] Fixed: Copy to clipboard in non-secure contexts --- frontend/src/Components/Link/ClipboardButton.tsx | 11 +++++++++-- package.json | 1 + yarn.lock | 12 ++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/frontend/src/Components/Link/ClipboardButton.tsx b/frontend/src/Components/Link/ClipboardButton.tsx index 09095ae74..dfce115ac 100644 --- a/frontend/src/Components/Link/ClipboardButton.tsx +++ b/frontend/src/Components/Link/ClipboardButton.tsx @@ -1,3 +1,4 @@ +import copy from 'copy-to-clipboard'; import React, { useCallback, useEffect, useState } from 'react'; import FormInputButton from 'Components/Form/FormInputButton'; import Icon from 'Components/Icon'; @@ -37,10 +38,16 @@ export default function ClipboardButton({ const handleClick = useCallback(async () => { try { - await navigator.clipboard.writeText(value); + if ('clipboard' in navigator) { + await navigator.clipboard.writeText(value); + } else { + copy(value); + } + setState('success'); - } catch (_) { + } catch (e) { setState('error'); + console.error(`Failed to copy to clipboard`, e); } }, [value]); diff --git a/package.json b/package.json index e5e0bbb8f..d3af32a44 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "@types/react-dom": "18.2.25", "classnames": "2.3.2", "connected-react-router": "6.9.3", + "copy-to-clipboard": "3.3.3", "element-class": "0.2.2", "filesize": "10.0.7", "fuse.js": "6.6.2", diff --git a/yarn.lock b/yarn.lock index f46048a70..4a387bed4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2609,6 +2609,13 @@ copy-anything@^2.0.1: dependencies: is-what "^3.14.1" +copy-to-clipboard@3.3.3: + version "3.3.3" + resolved "https://registry.yarnpkg.com/copy-to-clipboard/-/copy-to-clipboard-3.3.3.tgz#55ac43a1db8ae639a4bd99511c148cdd1b83a1b0" + integrity sha512-2KV8NhB5JqC3ky0r9PMCAZKbUHSwtEo4CwCs0KXgruG43gX5PMqDEBbVU4OUzw2MuAWUfsuFmWvEKG5QRfSnJA== + dependencies: + toggle-selection "^1.0.6" + core-js-compat@^3.36.1: version "3.37.0" resolved "https://registry.yarnpkg.com/core-js-compat/-/core-js-compat-3.37.0.tgz#d9570e544163779bb4dff1031c7972f44918dc73" @@ -6783,6 +6790,11 @@ to-space-case@^1.0.0: dependencies: to-no-case "^1.0.0" +toggle-selection@^1.0.6: + version "1.0.6" + resolved "https://registry.yarnpkg.com/toggle-selection/-/toggle-selection-1.0.6.tgz#6e45b1263f2017fa0acc7d89d78b15b8bf77da32" + integrity sha512-BiZS+C1OS8g/q2RRbJmy59xpyghNBqrr6k5L/uKBGRsTfxmu3ffiRnd8mlGPUVayg8pvfi5urfnu8TU7DVOkLQ== + "tough-cookie@^2.3.3 || ^3.0.1 || ^4.0.0": version "4.1.3" resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-4.1.3.tgz#97b9adb0728b42280aa3d814b6b999b2ff0318bf"