Skip to content

Commit 22799d7

Browse files
committed
ClipboardItem data DOMString support usage document
1 parent 22637b1 commit 22799d7

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# How to create ClipboardItem with string data
2+
**Last updated: October, 2024**
3+
4+
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. This enhancement allows web authors to directly write text data to the clipboard as a string, without needing to create a blob.
5+
6+
The feature is available in Chromium-based browsers in M132 or later behind the flag `ClipboardItemWithDOMStringSupport`
7+
8+
1. Download Microsoft Edge ([Canary Channel](https://www.microsoftedgeinsider.com/en-us/download/canary)).
9+
2. Launch Edge with the command line flag `--enable-blink-features=ClipboardItemWithDOMStringSupport`.
10+
11+
Here is an example of writing a ClipboardItem where text data is passed directly as string.
12+
13+
## Example
14+
15+
```javascript
16+
async function writeToClipboard() {
17+
try {
18+
const text_string = "Hello World";
19+
const html_string = "<h1>Hello World</h1>";
20+
const image_blob = await fetch("/url/to/an/image").blob();
21+
22+
const data = new ClipboardItem({
23+
"text/plain": text_string,
24+
"text/html": html_string,
25+
"image/png": image_blob
26+
});
27+
28+
await navigator.clipboard.write([data]);
29+
console.log('Data copied to clipboard');
30+
} catch (e) {
31+
console.error(e.message);
32+
}
33+
}
34+
```

0 commit comments

Comments
 (0)