Clipboard
writeText() method for writing text to clipboard
Ready to run
await navigator.clipboard.writeText('Hello, clipboard!');
console.log('Text copied to clipboard');
Parameter text - text string to copy to clipboard
Method returns a Promise that resolves after text is successfully written
readText() method for reading text from clipboard
Ready to run
const text = await navigator.clipboard.readText();
console.log('Clipboard content:', text);
Method returns a Promise that resolves to plain text content of the clipboard (text/plain only)
Important: Reading from clipboard requires user permission
Note: To read HTML or other data types use read() method
write() method for writing arbitrary data to clipboard
Ready to run
const html = '<p>Hello <strong>world</strong>!</p>';
const blob = new Blob([html], { type: 'text/html' });
const item = new ClipboardItem({ 'text/html': blob });
await navigator.clipboard.write([item]);
console.log('HTML copied!');
write() method allows copying various data types:
text/plain- plain texttext/html- HTML markupimage/png- images
read() method for reading arbitrary data from clipboard
Ready to run
const items = await navigator.clipboard.read();
for (const item of items) {
for (const type of item.types) {
const blob = await item.getType(type);
const text = await blob.text();
console.log(`${type}:`, text);
}
}
read() method allows reading various data types from clipboard, including HTML, images and other formats
Returns a Promise that resolves to an array of ClipboardItem objects
Important: This method requires user permission and works only in secure context (HTTPS)