Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1fb74ed
Create clipboard-change-event-explainer
roraja Aug 5, 2024
1963375
Rename clipboard-change-event-explainer to clipboard-change-event-exp…
roraja Aug 5, 2024
0398b4e
Update clipboard-change-event-explainer.md
roraja Aug 5, 2024
fddb588
Added scenario diagram
roraja Aug 5, 2024
c9a97cf
Update clipboard-change-event-explainer.md
roraja Aug 6, 2024
2e8123a
Delete ClipboardAPI/image-3.png
roraja Aug 6, 2024
34d0ba1
Update clipboard-change-event-explainer.md
roraja Aug 6, 2024
3c3b0dd
Update clipboard-change-event-explainer.md
roraja Aug 12, 2024
f5183aa
Add files via upload
roraja Aug 12, 2024
cd22cef
Add files via upload
roraja Aug 12, 2024
0d0ebfe
Update clipboard-change-event-explainer.md
roraja Aug 12, 2024
ba2dbba
Update clipboard-change-event-explainer.md
roraja Aug 14, 2024
7084fd3
Update clipboard-change-event-explainer.md
roraja Aug 14, 2024
ee5c7be
Add files via upload
roraja Aug 16, 2024
15c7b80
Update clipboard-change-event-explainer.md
roraja Aug 16, 2024
f2ca673
Update clipboard-change-event-explainer.md
roraja Aug 16, 2024
3673874
Clean up images
roraja Aug 16, 2024
8c88fdb
Fix review comments
roraja Aug 20, 2024
174290f
two cmts
roraja Aug 21, 2024
c984169
fix comments round 2
roraja Aug 22, 2024
effe4c7
fix comments round 2 - 2
roraja Aug 23, 2024
6c21a47
removed tech discussion
roraja Aug 23, 2024
43137c0
fixed con on user permission
roraja Aug 23, 2024
e0623e7
fix comments
roraja Aug 27, 2024
0f5d9ec
Added note that clipboardData is null
roraja Aug 27, 2024
91501f3
Added sample web app
roraja Aug 29, 2024
81d329c
fix review comments
roraja Sep 4, 2024
32e753c
update app
roraja Sep 4, 2024
f458a11
fix comments
roraja Sep 13, 2024
0b8b463
reword
roraja Sep 15, 2024
ff2ff9b
Fix Sanket's comments
roraja Sep 18, 2024
d3618d1
Fix
roraja Sep 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions ClipboardAPI/clipboard-change-event-example-app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clipboard Format Checker</title>
<script>
const isClipboardChangeEventAvailable = true;

async function checkClipboard() {
try {
console.log("Clipboard changed!");
const clipboardItems = await navigator.clipboard.read();
let formats = {};

for (const item of clipboardItems) {
if (item.types.includes('text/plain')) {
const textBlob = await item.getType('text/plain');
const text = await textBlob.text();
document.getElementById('clipboardText').innerText = text;
formats.text = true;
}
else {
document.getElementById('clipboardText').innerText = "N/A";
}
if (item.types.includes('image/png') || item.types.includes('image/jpeg')) {
const imgBlob = await item.getType(item.types.find(type => type.startsWith('image/')));
const imgUrl = URL.createObjectURL(imgBlob);
document.getElementById('clipboardImg').innerHTML = `<img src="${imgUrl}" alt="Clipboard Image" width="150">`;
formats.img = true;
}
else {
document.getElementById('clipboardImg').innerText = "N/A";
}
if (item.types.includes('text/html')) {
const htmlBlob = await item.getType('text/html');
const html = await htmlBlob.text();
document.getElementById('clipboardHtml').innerText = html;
formats.html = true;
}
else {
document.getElementById('clipboardHtml').innerText = "N/A";
}
}

document.getElementById('pasteText').disabled = !formats.text;
document.getElementById('pasteImg').disabled = !formats.img;
document.getElementById('pasteHtml').disabled = !formats.html;
} catch (err) {
console.error('Failed to read clipboard contents: ', err);
}
}


if (isClipboardChangeEventAvailable) {

// Try to read the clipboard to trigger a permissions prompt if required.
navigator.clipboard.readText().then(() => {

console.log("Clipboard read permission granted");

// Invoke the on clipboardchange handler on page load to initialize current UI state
checkClipboard();

// Start listening to the clipboardchange event
navigator.clipboard.addEventListener("clipboardchange", checkClipboard);
});

}
else {
// Invoke the on clipboardchange handler on page load to initialize current UI state
checkClipboard();
// Since clipboardchange event is not available, fallback to polling
setInterval(checkClipboard, 2000);
}
</script>
</head>

<body>
<h1>Clipboardchange event demo app - Paste formats viewer</h1>
<p>
This HTML application demonstrates the use of the clipboardchange event to monitor
and display clipboard data in various formats. The app listens for changes to the
clipboard using the clipboardchange event. It then displays the clipboard data in a table
with columns for Text, Image, and HTML formats.
<br />
When the clipboard content changes, the app updates the table to show the current clipboard data.
The app includes buttons for pasting clipboard data as Text, Image, and HTML.
These buttons are initially disabled and are enabled based on the available clipboard formats.

</p>
<hr />
<button id="pasteText" disabled>Paste as Text</button>
<button id="pasteImg" disabled>Paste as Image</button>
<button id="pasteHtml" disabled>Paste as HTML</button>
<br />
<hr />
<table border="1">
<thead>
<tr>
<th>Text</th>
<th>Image</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td id="clipboardText">N/A</td>
<td id="clipboardImg">N/A</td>
<td id="clipboardHtml">N/A</td>
</tr>
</tbody>
</table>
</body>

</html>
Loading