mirror of
https://github.com/urbit/shrub.git
synced 2024-12-21 09:51:36 +03:00
Fix copy from non-secure context issue
This commit is contained in:
parent
9de743fae7
commit
b93e2a15e3
@ -2,7 +2,7 @@ import React, { useState } from 'react';
|
||||
import { Row, Text, Button, Col } from '@tlon/indigo-react';
|
||||
import Send from './send.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 { api } from '../../api';
|
||||
|
||||
@ -23,8 +23,8 @@ const Balance = () => {
|
||||
const [copiedString, setCopiedString] = useState(false);
|
||||
const scanning = scanProgress?.main !== null || scanProgress?.change !== null;
|
||||
|
||||
const copyAddress = (arg) => {
|
||||
navigator.clipboard.writeText(address);
|
||||
const copyAddress = async (arg) => {
|
||||
await copyToClipboard(address);
|
||||
api.btcWalletCommand({ 'gen-new-address': null });
|
||||
|
||||
if (arg === 'button') {
|
||||
|
@ -127,3 +127,23 @@ export function mapDenominationToSymbol(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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user