DOMException when trying to write to clipboard after a confirm

138 views Asked by At

I'm having the user confirm wether they want to copy a email address to their clipboard or not after/if they do not have a default email client. It successfully detects if I do not have a default email client but gets stuck on document not being focused. Full exception: Uncaught (in promise) DOMException: Document is not focused.

I have tried using the window.focus() and following if-statement outside of the setTimeout(), in that case I do not get the previous mentioned exception but nothing is copied to the clipboard. I have also tried using async and await on various places in the code without success.

My code below:

verifyEmailHandler() {
      let t;

      window.addEventListener('blur', () => clearTimeout(t));
      let confirmed = false;
      t = setTimeout(() => {
        confirmed = confirm(
          `Email client could not be launched. You will have to manually mail this user. \nPress "OK" to copy "${this.userInfo.email}" to your clipboard.`
        );
        window.focus();

        if (confirmed) {
          navigator.clipboard.writeText(this.userInfo.email);
        }
      }, 500);
    },`
0

There are 0 answers