Skip to content

Commit

Permalink
fix: copy error in insecure page (#535)
Browse files Browse the repository at this point in the history
Signed-off-by: Bob Du <[email protected]>
  • Loading branch information
BobDu authored May 14, 2024
1 parent 9ed2e25 commit c01895a
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/utils/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ document.addEventListener('copy', (e) => {
e.preventDefault()
})

export function copyToClip(text: string) {
return navigator.clipboard.writeText(text)
export async function copyToClip(text: string) {
// https://stackoverflow.com/questions/51805395/navigator-clipboard-is-undefined
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text)
}
else {
const input: HTMLTextAreaElement = document.createElement('textarea')
input.setAttribute('readonly', 'readonly')
input.value = text
document.body.appendChild(input)
input.select()
if (document.execCommand('copy'))
document.execCommand('copy')
document.body.removeChild(input)
}
}

0 comments on commit c01895a

Please sign in to comment.