<div class="parent">
<div class="example">
<div class="pad">
<p>Just some example stuff to copy here for testing.</p>
<img src='https://images.unsplash.com/photo-1517451330947-7809dead78d5?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE3MjE5NTIwNTN8&ixlib=rb-4.0.3&q=80&w=400' alt=''>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ex, minima.</p>
</div>
</div>
<div class="example">
<button id="readCB">Read Clipboard</button>
<div id="log"></div>
<p class="message">If this doesn't work for you <a href="https://codepen.io/pen/debug/ZEdpZKv" target="_blank">try Debug Mode</a>.</p>
</div>
</div>
body {
height: 100vh;
margin: 0;
padding: 1rem;
display: grid;
place-items: center;
background: #455a64;
font-family: monospace;
}
.parent {
display: flex;
gap: 1rem;
}
.example {
background: white;
border-radius: 12px;
overflow: clip;
max-width: 45vw;
}
#readCB {
margin: 2rem;
}
#log {
background: black;
color: white;
padding: 2rem;
}
.pad {
padding: 1rem;
}
img {
max-width: 100%;
}
.message {
text-align: center;
padding-inline: 1rem;
}
* {
box-sizing: border-box;
}
let $log = document.querySelector("#log");
document.querySelector("#readCB").addEventListener("click", async () => {
let contents = await navigator.clipboard.read();
for (let item of contents) {
console.log("Types for this item: ", item.types);
if (item.types.includes("text/html")) {
let blob = await item.getType("text/html");
let html = await blob.text();
console.log(html);
$log.innerHTML += html.replaceAll("<", "<").replaceAll(">", ">");
$log.innerHTML += "<hr>";
}
if (item.types.includes("text/plain")) {
let blob = await item.getType("text/plain");
let text = await blob.text();
console.log(text);
$log.innerHTML += text.replaceAll("<", "<").replaceAll(">", ">");
$log.innerHTML += "<hr>";
}
if (item.types.includes("image/png")) {
// modified from MDN sample
const pngImage = new Image();
pngImage.alt = "PNG image from clipboard";
const blob = await item.getType("image/png");
pngImage.src = URL.createObjectURL(blob);
$log.appendChild(pngImage);
}
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.