Namespace: SpawnDev.SpawnJS.JSObjects
Inherits: SpawnJSObject
Source: JSObjects/ClipboardItem.cs
MDN Reference: ClipboardItem on MDN
The ClipboardItem interface of the Clipboard API represents a single item format, used when reading or writing clipboard data using clipboard.read() and clipboard.write() respectively. https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem
| Signature | Description |
|---|---|
ClipboardItem(Dictionary<string, Union<string, Blob, Task<string>, Task<Blob>>> data, ClipboardItemOptions? options) |
The ClipboardItem() constructor creates a new ClipboardItem object, which represents data to be stored or retrieved via the Clipboard API clipboard.write() and clipboard.read() methods, respectively. |
ClipboardItem(Dictionary<string, string> data, ClipboardItemOptions? options) |
The ClipboardItem() constructor creates a new ClipboardItem object, which represents data to be stored or retrieved via the Clipboard API clipboard.write() and clipboard.read() methods, respectively. |
ClipboardItem(Dictionary<string, Blob> data, ClipboardItemOptions? options) |
The ClipboardItem() constructor creates a new ClipboardItem object, which represents data to be stored or retrieved via the Clipboard API clipboard.write() and clipboard.read() methods, respectively. |
ClipboardItem(Dictionary<string, Task<string>> data, ClipboardItemOptions? options) |
The ClipboardItem() constructor creates a new ClipboardItem object, which represents data to be stored or retrieved via the Clipboard API clipboard.write() and clipboard.read() methods, respectively. |
ClipboardItem(Dictionary<string, Task<Blob>> data, ClipboardItemOptions? options) |
The ClipboardItem() constructor creates a new ClipboardItem object, which represents data to be stored or retrieved via the Clipboard API clipboard.write() and clipboard.read() methods, respectively. |
| Property | Type | Access | Description |
|---|---|---|---|
Types |
string[] |
get | Returns an Array of MIME types available within the ClipboardItem. |
PresentationStyle |
string? |
get | Returns one of the following: "unspecified", "inline" or "attachment". Support varies by browser |
| Method | Return Type | Description |
|---|---|---|
Supports(string type) |
bool |
Checks whether a given MIME type is supported by the clipboard. This enables a website to detect whether a MIME type is supported before attempting to write data. |
GetType(string type) |
Task<Blob> |
Returns a Promise that resolves with a Blob of the requested MIME type, or an error if the MIME type is not found. |
SpawnDev.SpawnJS Mapping Note: The JavaScript examples below show the standard Web API usage. In SpawnDev.SpawnJS, the same API is available with C# conventions:
- Properties and methods use PascalCase (e.g.,
readyStatebecomesReadyState)- Events use ActionEvent with
+=/-=(e.g.,addEventListener("click", fn)becomesOnClick += handler)- Async methods return
Task<T>instead ofPromise<T>- Objects should be disposed with
usingstatements- Access via
JS.Get<ClipboardItem>(...)or constructornew ClipboardItem(...)
const textSource = document.querySelector("p");
const copyBtn = document.querySelector("button");
async function copyToClipboard() {
const type = "text/plain";
const clipboardItemData = {
[type]: textSource.textContent,
};
const clipboardItem = new ClipboardItem(clipboardItemData);
await navigator.clipboard.write([clipboardItem]);
}
copyBtn.addEventListener("click", copyToClipboard);async function writeClipImg() {
try {
if (ClipboardItem.supports("image/svg+xml")) {
const imgURL = "/my-image.svg";
const data = await fetch(imgURL);
const blob = await data.blob();
await navigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob,
}),
]);
console.log("Fetched image copied.");
} else {
console.log("SVG images are not supported by the clipboard.");
}
} catch (err) {
console.error(err.name, err.message);
}
}async function getClipboardContents() {
try {
const clipboardItems = await navigator.clipboard.read();
for (const clipboardItem of clipboardItems) {
for (const type of clipboardItem.types) {
const blob = await clipboardItem.getType(type);
// we can now use blob here
}
}
} catch (err) {
console.error(err.name, err.message);
}
}