document.execCommand('copy') can be used inside the Promise permission function, with the exception of Firefox. All modern browsers, such as Chrome, Opera and even Safari, allow you to copy asynchronous copies up to 1 second.
I want to improve the user experience and copy the data after the calculation to the clipboard.
Is there a solution to copy the result of a promise with Firefox with one click?
Here's a snapshot working with Chrome
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Test</title> </head> <body> <button onclick="copy(genPwd)">copy</button> <script> function genPwd() { return new Promise(function(resolve) { resolve('toto') }) } function copy(p) { p().then(function(result) { console.log('create fake text area'); var fakeTextArea = document.createElement('textarea'); fakeTextArea.setAttribute('readonly', ''); fakeTextArea.value = result; document.body.appendChild(fakeTextArea); fakeTextArea.select(); document.execCommand('copy'); }); } </script> </body> </html>
source share