From b24ce0366ef6066c8c614a12f81e3662100d8a52 Mon Sep 17 00:00:00 2001 From: Samba Murthy Bandaru Date: Wed, 16 Oct 2024 20:14:49 +0530 Subject: [PATCH] ClipboardItem data Promise support usage document --- .../clipboarditem-with-string-data.md | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ClipboardAPI/clipboarditem-with-string-data.md b/ClipboardAPI/clipboarditem-with-string-data.md index a612167a..57ec9365 100644 --- a/ClipboardAPI/clipboarditem-with-string-data.md +++ b/ClipboardAPI/clipboarditem-with-string-data.md @@ -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() { @@ -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("

Hello World

"); + + 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); + } +} ``` \ No newline at end of file