Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to copy with DOMException document not focused in latest firefox / chrome. #38

Open
xpoh434 opened this issue Jul 12, 2021 · 0 comments

Comments

@xpoh434
Copy link

xpoh434 commented Jul 12, 2021

It failed to copy to clipboard with DOMException document not focused in latest firefox / chrome.

It seems to me the root clause is at the

const write = (text: string): void => {
defocused the document and after it failed, it try the clipboard API which throws the DOMException.

It seems it works if I restore the original focus:

const write = (text: string): void => {
  const textArea: HTMLTextAreaElement = createTextArea();
  const curActiveElement = document.activeElement as HTMLElement
  textArea.value = text;
  textArea.select();
  const success: boolean = document.execCommand('copy');
  removeElement(textArea);
  if(curActiveElement) curActiveElement.focus()
  if (!success) {
    throw NOT_ALLOWED_ERROR;
  }
};

I guess similar problem exists for reading the clipboard but I did not use / test it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant