Skip to content

Workaround Safari clipboard.writeText behavior - #139657

Merged
TylerLeonhardt merged 5 commits into
mainfrom
joh/safariCopy
Dec 23, 2021
Merged

TylerLeonhardt merged 5 commits into
mainfrom
joh/safariCopy

Conversation

@TylerLeonhardt

@TylerLeonhardt TylerLeonhardt commented Dec 22, 2021 •

Copy link
Copy Markdown
Member

In Safari, it has the following note:

"The request to write to the clipboard must be triggered during a user gesture. A call to clipboard.write or clipboard.writeText outside the scope of a user gesture(such as "click" or "touch" event handlers) will result in the immediate rejection of the promise returned by the API call."

From: https://webkit.org/blog/10855/async-clipboard-api/

Since extensions run in a web worker, and handle gestures in an asynchronous way, they are not classified by Safari as "in response to a user gesture" and will reject.

This function sets up some handlers to work around that behavior by leveraging the ClipboardItem API that takes in a Promise:
https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem/ClipboardItem#parameters

This PR fixes #106997

@TylerLeonhardt TylerLeonhardt changed the title Workaround Safari clipboard behavior Workaround Safari clipboard.writeText behavior Dec 22, 2021
if (isSafari) {
this.logService.error(error);

return ''; // Safari does not seem to provide anyway to enable cipboard access (https://github.com/microsoft/vscode-internalbacklog/issues/2162#issuecomment-852042867)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safari supports readText now so this does work now.

…xt return the promise DeferredPromise.complete returns
@TylerLeonhardt
TylerLeonhardt merged commit a6487c6 into main Dec 23, 2021
@TylerLeonhardt
TylerLeonhardt deleted the joh/safariCopy branch December 23, 2021 14:45
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clipboard API doesn’t work on Codespaces (using Safari)

4 participants