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

Clipboard api throws 'NotAllowedError: Failed to execute 'write' on 'Clipboard': Document is not focused' #2196

Open
ZimnitskiYury opened this issue Mar 4, 2024 · 5 comments

Comments

@ZimnitskiYury
Copy link

Clipboard write() method not working in desktop Teams

I'm developing a personal tab for Microsoft Teams. Part of the functionality of my app is copying some data to the clipboard. For this, I use the write() method from the clipboard namespace. It works fine in the web version of Teams but throws an error in the desktop Teams.
Failed to copy to clipboard. NotAllowedError: Failed to execute 'write' on 'Clipboard': Document is not focused.

Here is the code snippets that I use to copy data to the clipboard:
1.

const content = ‘Hello, world!’; 
const blob = new Blob([content], { type: ‘text/plain’ });
clipboard.write(blob).catch((error) => { throwError(error); });
  1. I also tried to use window.focus() and this approach, but neither of them worked:
const content = ‘Hello, world!’;
const blob = new Blob([content], {type: ‘text/plain’});

const button = document.createElement(‘button’); 
button.innerHTML = ‘Copy’; 
button.style.display = ‘none’; 
button.addEventListener(‘click’, async () => { 
    clipboard.write(blob)
                   .catch((error) => { 
                       throwError(error); 
                   })
                  .finally(() => { 
                     button.remove(); 
                  });
}); 
document.body.append(button); 
pages.returnFocus(); 
button.focus(); 
button.click();
@maglims
Copy link
Contributor

maglims commented Mar 4, 2024

Thanks for reporting. Our engineering team is taking a look and will provide a update when we have some findings. Thanks.

@Prasad-MSFT
Copy link

The PR #2201 is a WIP to fix this issue, so please follow that PR for tracking when the fix is available!

@ZimnitskiYury
Copy link
Author

@Prasad-MSFT @shrshindeMSFT unfortunately, #2201 didn't solve the issue. isSupported() returns trueonn desktop, but on try clipdoard.write I get the error Failed to copy to clipboard. NotAllowedError: Failed to execute 'write' on 'Clipboard': Document is not focused.
The web version works fine after a request for permission.

@ZimnitskiYury
Copy link
Author

actual for #2220

@shrshindeMSFT
Copy link
Contributor

Thank you for reporting. I'll look into it and see if I can repro this issue.

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

No branches or pull requests

5 participants