Copy to clipboard not working for interactive examples
tannerdolby opened this issue · comments
Summary
The copy-to-clipboard logic is failing in the interactive example <iframe>
due to lack of clipboard-write permissions for the Clipboard API.
To use the API in iframes, you need to enable it with Permissions Policy, which defines a mechanism that allows for selectively enabling and disabling various browser features and APIs. Concretely, you need to pass either or both of clipboard-read or clipboard-write, depending on the needs of your app.
URL
- https://developer.mozilla.org/en-US/docs/Web/CSS/clip-path
- any page with an interactive example
Reproduction steps
- Go to https://developer.mozilla.org/en-US/docs/Web/CSS/clip-path
- Click into one of the clip-path demo values, then click the copy-to-clipboard icon
- See error in console regarding lack of permissions
![Screen Shot 2024-04-13 at 1 50 05 AM](https://private-user-images.githubusercontent.com/48612525/322190080-f3f85b70-6d84-4046-89d6-c1b1ec8be648.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA2OTkyMDksIm5iZiI6MTcyMDY5ODkwOSwicGF0aCI6Ii80ODYxMjUyNS8zMjIxOTAwODAtZjNmODViNzAtNmQ4NC00MDQ2LTg5ZDYtYzFiMWVjOGJlNjQ4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzExVDExNTUwOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTU0OWIxZGJkZWM3ZmE4OGMzMGIzNGE0ZWJmMDVmOGIxMTBmNmU2ZDE4ZDBjZWVhYzcxNmMyNThkYTI1YzlmN2UmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.DruiyRGZcm5XudUBWzxLxBG_-TKSlI6gUST9U55l7_Q)
Expected behavior
Expected content to be copied to clipboard within interactive examples
Actual behavior
Fails to copy content to clipboard and logs error to the browser console
Device
Desktop
Browser
Chrome
Browser version
Stable
Operating system
Mac OS
Screenshot
copy-to-clipboard-iframe-error.mov
Anything else?
No response
Validations
- I have read the Community Participation Guidelines.
- I have verified that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- I have checked that this is a concrete bug. For Q&A open a GitHub Discussion.
maybe we could make the {{EmbedInteractiveExample}} macros able to passing a parameter to specified allow
attribute on the <iframe>
element, just like the {{EmbedLiveSample}} does
https://github.com/mdn/yari/blob/main/kumascript/macros/EmbedLiveSample.ejs
@skyclouds2001 that could be an option, since the EmbedLiveSample macro supports a parameter for allowed features ($6) it makes sense that the EmbedInteractiveExample macro might do something similar. I'm just not sure if the interactive examples need any other permissions currently other than clipboard API. I added allow="clipboard-write"
directly to the <iframe>
in EmbedInteractiveExample.ejs for my initial PR (#10910) to quickly validate that the copy-to-clipboard worked again in interactive examples without having to update the actual EmbedInteractiveExample() macro calls throughout the various pages.
I searched mdn/content and found 1256 results in 1253 files of EmbedInteractiveExample(
which would mean doing a mass search-and-replace to add a third argument with the "clipboard-write" permission, that is assuming all the interactive examples have code snippets which can be copied to clipboard. If the mdn folks are cool with that extra work of adding another parameter to the EmbedInteractiveExample macro to account for allowed features and updating those 1253 files in mdn/content then I can update my PR here to reflect that and submit one in mdn/content, otherwise I think simply adding the clipboard-write permission to the iframes directly gets the job done.