Skip to content

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

Open
@xpoh434

Description

@xpoh434

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions