Clipboard APIs do not work inside iframes in webviews
#182.642 geöffnet am 16. Mai 2023
Repository-Metriken
- Stars
- (74.848 Stars)
- PR-Merge-Metriken
- (Durchschn. Merge 11h 43m) (1.000 gemergte PRs in 30 T)
Beschreibung
Create a webview that has an iframe with allow="clipboard-write":
<iframe src="http://127.0.0.1:8000/" allow="clipboard-write" width="100%" height="600" />
In the hosted page, try to use the clipboard APIs:
<button onclick="window.navigator.clipboard.writeText('inner');">Copy "inner" to clipboard</button>
When clicking the button, the operation will fail and the developer tools will report:
Uncaught (in promise) DOMException: Write permission denied.
I've tried numerous combinations of things in the allow attribute (such as clipboard and clipboard-write http://127.0.0.1:8000/), but nothing seems to fix this. I presume the iframe being another origin is complicating things.
However, outside of VS Code this works fine. I made iframes nested three-deep that cross origins, and as long as I have allow="clipboard-write" then all of the nested frames are able to call this API successfully.
This might be related to https://github.com/microsoft/vscode/issues/129178 (which is that the system-level Cmd+C shortcut does not work), but I'm not sure these two things should be the same (one is the system copying something, and the other is JavaScript access to the clipboard API) so am raising a separate issue.