Fix copy from non-secure context issue

This commit is contained in:
finned-palmer 2021-07-10 09:20:57 -05:00 committed by ixv
parent 9de743fae7
commit b93e2a15e3
2 changed files with 23 additions and 3 deletions

View File

@ -2,7 +2,7 @@ import React, { useState } from 'react';
import { Row, Text, Button, Col } from '@tlon/indigo-react'; import { Row, Text, Button, Col } from '@tlon/indigo-react';
import Send from './send.js'; import Send from './send.js';
import CurrencyPicker from './currencyPicker.js'; import CurrencyPicker from './currencyPicker.js';
import { satsToCurrency } from '../../lib/util.js'; import { copyToClipboard, satsToCurrency } from '../../lib/util.js';
import { useSettings } from '../../hooks/useSettings.js'; import { useSettings } from '../../hooks/useSettings.js';
import { api } from '../../api'; import { api } from '../../api';
@ -23,8 +23,8 @@ const Balance = () => {
const [copiedString, setCopiedString] = useState(false); const [copiedString, setCopiedString] = useState(false);
const scanning = scanProgress?.main !== null || scanProgress?.change !== null; const scanning = scanProgress?.main !== null || scanProgress?.change !== null;
const copyAddress = (arg) => { const copyAddress = async (arg) => {
navigator.clipboard.writeText(address); await copyToClipboard(address);
api.btcWalletCommand({ 'gen-new-address': null }); api.btcWalletCommand({ 'gen-new-address': null });
if (arg === 'button') { if (arg === 'button') {

View File

@ -127,3 +127,23 @@ export function mapDenominationToSymbol(denomination) {
return denomination; return denomination;
} }
} }
export function copyToClipboard(textToCopy) {
// navigator clipboard api needs a secure context (https or localhost)
if (navigator.clipboard && window.isSecureContext) {
return navigator.clipboard.writeText(textToCopy);
} else {
let textArea = document.createElement('textarea');
textArea.value = textToCopy;
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) => {
document.execCommand('copy') ? res() : rej();
textArea.remove();
});
}
}