diff --git a/frontend/src/components/Dialogs/AuditPGP.tsx b/frontend/src/components/Dialogs/AuditPGP.tsx index 3c5cf31c..49ea6f82 100644 --- a/frontend/src/components/Dialogs/AuditPGP.tsx +++ b/frontend/src/components/Dialogs/AuditPGP.tsx @@ -15,6 +15,7 @@ import { } from "@mui/material" import { saveAsJson } from "../../utils/saveFile"; +import { copyToClipboard } from "../../utils/clipboard"; // Icons import KeyIcon from '@mui/icons-material/Key'; @@ -36,7 +37,7 @@ function CredentialTextfield(props){ InputProps={{ endAdornment: - navigator.clipboard.writeText(props.value)}> + copyToClipboard(props.value)}> , diff --git a/frontend/src/components/Dialogs/Profile.tsx b/frontend/src/components/Dialogs/Profile.tsx index 27815e91..d9d57a64 100644 --- a/frontend/src/components/Dialogs/Profile.tsx +++ b/frontend/src/components/Dialogs/Profile.tsx @@ -33,6 +33,7 @@ import PersonAddAltIcon from "@mui/icons-material/PersonAddAlt"; import EmojiEventsIcon from "@mui/icons-material/EmojiEvents"; import { getCookie } from "../../utils/cookies"; +import { copyToClipboard } from "../../utils/clipboard"; type Props = { isOpen: boolean; @@ -75,13 +76,13 @@ const ProfileDialog = ({ const robotToken = getCookie("robot_token"); if (robotToken) { - navigator.clipboard.writeText(robotToken); + copyToClipboard(robotToken); setAppState({copiedToken:true}); } }; const copyReferralCodeHandler = () => { - navigator.clipboard.writeText(`http://${host}/ref/${referralCode}`); + copyToClipboard(`http://${host}/ref/${referralCode}`); }; return ( diff --git a/frontend/src/components/EncryptedChat.js b/frontend/src/components/EncryptedChat.js index 04788ceb..a48ee07f 100644 --- a/frontend/src/components/EncryptedChat.js +++ b/frontend/src/components/EncryptedChat.js @@ -5,6 +5,7 @@ import ReconnectingWebSocket from 'reconnecting-websocket'; import { encryptMessage , decryptMessage} from "../utils/pgp"; import { getCookie } from "../utils/cookies"; import { saveAsJson } from "../utils/saveFile"; +import { copyToClipboard } from "../utils/clipboard"; import { AuditPGPDialog } from "./Dialogs" // Icons @@ -234,7 +235,7 @@ class Chat extends Component {
navigator.clipboard.writeText(this.state.showPGP[props.index] ? props.message.encryptedMessage : props.message.plainTextMessage)}> + onClick={()=> copyToClipboard(this.state.showPGP[props.index] ? props.message.encryptedMessage : props.message.plainTextMessage)}> diff --git a/frontend/src/components/MakerPage.js b/frontend/src/components/MakerPage.js index 22efef53..a2bda5d5 100644 --- a/frontend/src/components/MakerPage.js +++ b/frontend/src/components/MakerPage.js @@ -19,7 +19,7 @@ import { BuySatsCheckedIcon, BuySatsIcon, SellSatsCheckedIcon, SellSatsIcon} fro import { getCookie } from "../utils/cookies"; import { pn } from "../utils/prettyNumbers"; - +import { copyToClipboard } from "../utils/clipboard"; class MakerPage extends Component { defaultCurrency = 1; @@ -732,7 +732,7 @@ class MakerPage extends Component { this.setState({openStoreToken:false})} - onClickCopy={()=> (navigator.clipboard.writeText(getCookie("robot_token")) & this.props.setAppState({copiedToken:true}))} + onClickCopy={()=> (copyToClipboard(getCookie("robot_token")) & this.props.setAppState({copiedToken:true}))} copyIconColor={this.props.copiedToken ? "inherit" : "primary"} onClickBack={() => this.setState({openStoreToken:false})} onClickDone={this.handleCreateOfferButtonPressed} diff --git a/frontend/src/components/OrderPage.js b/frontend/src/components/OrderPage.js index 883dc3d6..6edb01b1 100644 --- a/frontend/src/components/OrderPage.js +++ b/frontend/src/components/OrderPage.js @@ -23,6 +23,7 @@ import { SendReceiveIcon } from "./Icons"; import { getCookie } from "../utils/cookies"; import { pn } from "../utils/prettyNumbers"; +import { copyToClipboard } from "../utils/clipboard"; class OrderPage extends Component { constructor(props) { @@ -359,7 +360,7 @@ class OrderPage extends Component { this.setState({openStoreToken:false})} - onClickCopy={()=> (navigator.clipboard.writeText(getCookie("robot_token")) & this.props.setAppState({copiedToken:true}))} + onClickCopy={()=> (copyToClipboard(getCookie("robot_token")) & this.props.setAppState({copiedToken:true}))} copyIconColor={this.props.copiedToken ? "inherit" : "primary"} onClickBack={() => this.setState({openStoreToken:false})} onClickDone={() => this.setState({openStoreToken:false}) & diff --git a/frontend/src/components/TradeBox.js b/frontend/src/components/TradeBox.js index 1902c34f..e303c9d6 100644 --- a/frontend/src/components/TradeBox.js +++ b/frontend/src/components/TradeBox.js @@ -6,6 +6,7 @@ import Countdown, { zeroPad} from 'react-countdown'; import Chat from "./EncryptedChat" import MediaQuery from 'react-responsive' import QrReader from 'react-qr-reader' +import { copyToClipboard } from "../utils/clipboard"; // Icons import PercentIcon from '@mui/icons-material/Percent'; @@ -216,7 +217,7 @@ class TradeBox extends Component { - + @@ -299,7 +300,7 @@ class TradeBox extends Component { - + @@ -1252,7 +1253,7 @@ handleRatingRobosatsChange=(e)=>{ {t("Your TXID")} - {navigator.clipboard.writeText(this.props.data.txid)}}> + {copyToClipboard(this.props.data.txid)}}> diff --git a/frontend/src/components/UserGenPage.js b/frontend/src/components/UserGenPage.js index 4400ffe0..7154a221 100644 --- a/frontend/src/components/UserGenPage.js +++ b/frontend/src/components/UserGenPage.js @@ -17,7 +17,7 @@ import { genBase62Token, tokenStrength } from "../utils/token"; import { genKey } from "../utils/pgp"; import { getCookie, writeCookie, deleteCookie } from "../utils/cookies"; import { saveAsJson } from "../utils/saveFile"; - +import { copyToClipboard } from "../utils/clipboard"; class UserGenPage extends Component { constructor(props) { @@ -259,7 +259,7 @@ class UserGenPage extends Component { (navigator.clipboard.writeText(getCookie('robot_token')) & this.props.setAppState({copiedToken:true}))} + onClick={()=> (copyToClipboard(getCookie('robot_token')) & this.props.setAppState({copiedToken:true}))} > diff --git a/frontend/src/utils/clipboard.js b/frontend/src/utils/clipboard.js new file mode 100644 index 00000000..a186a4d9 --- /dev/null +++ b/frontend/src/utils/clipboard.js @@ -0,0 +1,24 @@ +export function copyToClipboard(textToCopy) { +// navigator clipboard api needs a secure context (https) +// this function attempts to copy also on http contexts +// useful on the http i2p site and on torified browsers +if (navigator.clipboard && window.isSecureContext) { + // navigator clipboard api method' + return navigator.clipboard.writeText(textToCopy); +} else { + // text area method + let textArea = document.createElement("textarea"); + textArea.value = textToCopy; + // make the textarea out of viewport + textArea.style.position = "fixed"; + textArea.style.left = "-999999px"; + textArea.style.top = "-999999px"; + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + return new Promise((res, rej) => { + // here the magic happens + document.execCommand('copy') ? res() : rej(); + textArea.remove(); + }); +}}