-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Description
Summary
Deno could implement operations for getting and setting the system clipboard (copy/paste).
I wrote a library for Deno that does that but it needs the all-powerful --allow-run that ruins the permission system.
The browsers implement it with a standard API that could be used.
We would need new permissions like --allow-copy and --allow-paste because this would be a security risk to give access to the clipboard with no restrictions (people often copy/paste personal data, passwords, API keys and other credentials). In the browsers there are separate permissions "clipboard-read" and "clipboard-write" in the standard Permissions API.
document.execCommand
The older API to use the clipboard in the browsers was document.execCommand("copy"),
document.execCommand("cut") and document.execCommand("paste"), see:
navigator.clipboard
The newer API to use the clipboard in the browsers is via the global navigator.clipboard object:
From MDN on Clipboard:
The Clipboard API adds to the Navigator interface the read-only clipboard property, which returns the Clipboard object used to read and write the clipboard's contents. The Clipboard API can be used to implement cut, copy, and paste features within a web application.
Use of the asynchronous clipboard read and write methods requires that the user grant the web site or app permission to access the clipboard. This permission must be obtained from the Permissions API using the "clipboard-read" and/or "clipboard-write" permissions.
The clipboard object has 4 methods:
read() Requests arbitrary data (such as images) from the clipboard, returning a Promise. When the data has been retrieved, the promise is resolved with a DataTransfer object that provides the data.
readText() Requests text from the system clipboard; returns a Promise which is resolved with a DOMString containing the clipboard's text once it's available.
write() Writes arbitrary data to the system clipboard. This asynchronous operation signals that it's finished by resolving the returned Promise.
writeText() Writes text to the system clipboard, returning a Promise which is resolved once the text is fully copied into the clipboard.
deno.land/x/clipboard
I wrote a library for Deno that implements readText() and writeText() on Linux, Mac and Windows:
The navigator.clipboard also implements the EventTarget interface which my library doesn't.
navigator.clipboard in Deno?
This seems like something that would be ideally exposed by Deno itself:
- so the libraries don't spawn external processes just to use the clipboard
- so that the permissions can be controlled
- so that it is maximally familiar to how the browsers work
It would be most optimal to use the standard window.navigator:
Or should it be exposed in some other non-standard place like Deno.clipboard?
References
- https://github.com/rsp/deno-clipboard
- Add clipboard module for copy/paste dotland#132
- https://developer.mozilla.org/en-US/docs/Web/API/Clipboard
- https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API
- https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API
- https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard
- https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard