-
Notifications
You must be signed in to change notification settings - Fork 22
add navigator clipboard, ClipboardItem #18
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
Conversation
This is great, thank you @keithamus for putting this together 🎉
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is super cool, just left my one drive-by question
164abf9
to
faa236c
Compare
faa236c
to
f054df8
Compare
This comment was marked as spam.
This comment was marked as spam.
Tested today on Firefox and confirmed that this fixes the Copy Remote functionality in the blob page for Firefox, see details on https://github.com./github/repos/issues/1073#issuecomment-1226971804. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 🚀
This implements
navigator.clipboard.write
andnavigator.clipboard.read
, which both work withClipboardItem
s. It also adds a re-implementation ofClipboardItem
which supports Promises, adhering to the spec.write
, per the spec takes an array ofClipboardItem
s and writes to the clipboard. We're limited in what we can do here, so we extract thetext/plain
content of the first one and callwriteText
. This is a pretty reasonable default behaviour.read
, per the spec should return an array ofClipboardItem
s representing the user's current clipboard. We're limited what we can do here, so we callreadText
and convert it into aClipboardItem
with justtext/plain
.ClipboardItem
has apresentationStyle
attribute (defaulting to'unspecified'
unless it's passed as an option), andgetType()
always returnsBlob
s. In Chrome's buggy implementation,getType('text/plain')
will return a String. We've accounted for both possibilities innavigator.clipboard.write
just in case the polyfill isn't applied (e.g. Chrome adds promises but keeps their buggygetType()
behaviour).This means
await navigator.clipboard.write(await navigator.clipboard.read())
should be a no-op (as long as the user only has plain-text data in their clipboard) which feels like a sensible thing to do.Customer Support Data
Here are some tables representing the % of browsers we see that support (or don't support) these APIs.
Clipboard Write
(Note: "Other" browsers make up 0.408%)