Interacting with a user’s host clipboard is something web developers have wanted for both good and evil purposes. On the good side, it’s nice to allow users to easily copy text like wallet addresses or branch names; for evil, copying malicious text that the user may mistakenly paste into a form and have their funds stolen — and there are probably more evil reasons.
We used to use the document.execCommand('copy')
to accomplish this task, but it was unreliable. The navigator.clipboard
API provides async readText
and writeText
methods for managing clipboard data. Let’s have a look how it works!
// Write to clipboard document.body.addEventListener( "click", (e) => { await navigator.clipboard.writeText("Yo") } ) // Read from clipboard document.body.addEventListener( "click", (e) => { const text = await navigator.clipboard.readText() } )
The readText
and writeText
methods are easy enough to use, but you can’t execute this code whenever you’d like, due to browser security protocols. Oftentimes you need to use this code inside of an event listener, as a result of an action taken by the users.
I’m glad we now have an API that’s async and more reliable than the gross execCommand
hack of the old days. Still, I sometimes wonder how this could be exploited, because after all, you can still put any text there. Let’s all do each other a solid though — let’s use this API for good, not evil!
Create a CSS Cube
CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals. Add animation and you’ve got something really neat. Unfortunately each CSS cube tutorial I’ve read is a bit…
Designing for Simplicity
Before we get started, it’s worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech…
Build a Slick and Simple MooTools Accordion
Last week I covered a smooth, subtle MooTools effect called Kwicks. Another great MooTools creation is the Accordion, which acts like…wait for it…an accordion! Now I’ve never been a huge Weird Al fan so this is as close to playing an accordion as…
MooTools 1.2 OpenLinks Plugin
I often incorporate tools into my customers’ websites that allow them to have some control over the content on their website. When doing so, I offer some tips to my clients to help them keep their website in good shape. One of the tips…