My first attempt at getting text from the clipboard resulted in the ‘document not focused’ error (not using DevTools) – then managed to get to it returning ‘[Object Promise]’ and now it returns ‘2’ (which is not the text on the clipboard). I have tried every which way but drunk to get this to work and it’s probably a combination of some of the failed attempts. Any pointer will be appreciated.
I have noticed that using setTimeout stops the ‘document not focused’ problem, but there is not a three second delay.
async function readClipboard() {
try {
const myText = setTimeout(async()=>(await navigator.clipboard.readText()), 3000);
alert(myText);
} catch(err) {
alert(err);
}
}
readClipboard();
This returns ‘undefined’ and if I give the variable a text value (xxx) it just returns that not the clipboard text!
async function readClipboard() {
var myText // = 'xxx';
try {
setTimeout(async()=>(myText = await navigator.clipboard.readText()), 3000);
alert(myText);
} catch(err) {
alert(err);
}
}
readClipboard();