Skip to content

Commit

Permalink
ClipboardItem data Promise<DOMString> support usage document
Browse files Browse the repository at this point in the history
  • Loading branch information
sambandaru committed Oct 16, 2024
1 parent 22799d7 commit b24ce03
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion ClipboardAPI/clipboarditem-with-string-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The feature is available in Chromium-based browsers in M132 or later behind the

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

## Example
## Example 1

```javascript
async function writeToClipboard() {
Expand All @@ -31,4 +31,27 @@ async function writeToClipboard() {
console.error(e.message);
}
}
```

Similarly, ClipboardItem data supports promise that resolves to string

## Example 2

```javascript
async function writePromiseToClipboard() {
try {
const promise_text_string = Promise.resolve("Hello World");
const promise_html_string = Promise.resolve("<h1>Hello World</h1>");

const item = new ClipboardItem({
"text/plain": promise_text_string,
"text/html": promise_html_string
});

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

0 comments on commit b24ce03

Please sign in to comment.