Skip to content

Commit b24ce03

Browse files
committed
ClipboardItem data Promise<DOMString> support usage document
1 parent 22799d7 commit b24ce03

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

ClipboardAPI/clipboarditem-with-string-data.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The feature is available in Chromium-based browsers in M132 or later behind the
1010

1111
Here is an example of writing a ClipboardItem where text data is passed directly as string.
1212

13-
## Example
13+
## Example 1
1414

1515
```javascript
1616
async function writeToClipboard() {
@@ -31,4 +31,27 @@ async function writeToClipboard() {
3131
console.error(e.message);
3232
}
3333
}
34+
```
35+
36+
Similarly, ClipboardItem data supports promise that resolves to string
37+
38+
## Example 2
39+
40+
```javascript
41+
async function writePromiseToClipboard() {
42+
try {
43+
const promise_text_string = Promise.resolve("Hello World");
44+
const promise_html_string = Promise.resolve("<h1>Hello World</h1>");
45+
46+
const item = new ClipboardItem({
47+
"text/plain": promise_text_string,
48+
"text/html": promise_html_string
49+
});
50+
51+
await navigator.clipboard.write([data]);
52+
console.log('Data copied to clipboard');
53+
} catch (e) {
54+
console.error(e.message);
55+
}
56+
}
3457
```

0 commit comments

Comments
 (0)