Skip to content
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

ClipboardItem data DOMString support usage document #895

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions ClipboardAPI/clipboarditem-with-string-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# How to create ClipboardItem with DOMString
**Last updated: October, 2024**

ClipboardItem constructor extends support to string data. ClipboardItem data can now be a blob, a string, or a promise that resolves to either a blob or a string.

The feature is available in Chromium-based browsers in M132 or later behind the flag `ClipboardItemWithDOMStringSupport`

1. Download Microsoft Edge ([Canary Channel](https://www.microsoftedgeinsider.com/en-us/download/canary)).
2. Launch Edge with the command line flag `--enable-blink-features=ClipboardItemWithDOMStringSupport`.

This enhancement allows web authors to directly write text data to the clipboard as a string, without needing to create a blob.

```javascript
const data = new ClipboardItem({
"text/plain": "Hello World", // DOMString
"text/html": Promise.resolve("<h1>Hello World</h1>") // Promise<DOMString>
});
```

If the flag is not enabled, a blob is required for the same.

```javascript
const data = new ClipboardItem({
"text/plain": new Blob(["Hello World"], {type: 'text/plain'}),
"text/html": new Blob(["<h1>Hello World</h1>"], {type: 'text/html'})
});
```

Here is an example of writing a ClipboardItem with different supported data types.

## Example

```javascript
async function writeToClipboard() {
try {
const plain_string = "Hello World";
const html_string_promise = Promise.resolve("<h1>Hello World</h1>");
const png_blob_promise = await fetch("/url/to/a/png/image").blob();

const data = new ClipboardItem({
"text/plain": plain_string,
"text/html": html_string_promise,
"image/png": png_blob_promise
});

await navigator.clipboard.write([data]);
console.log('Data copied to clipboard');
} catch (e) {
console.error(e.message);
}
}
```