Detect the Content Type in the Clipboard
A user’s clipboard is a “catch all” between the operating system and the apps employed on it. When you use a web browser, you can highlight text or right-click an image and select “Copy Image”. That made me think about how developers can detect what is in the clipboard.
You can retrieve the contents of the user’s clipboard using the navigator.clipboard
API. This API requires user permission as the clipboard could contain sensitive data. You can employ the following JavaScript to get permission to use the clipboard API:
const result = await navigator.permissions.query({name: "clipboard-write"}); if (result.state === "granted" || result.state === "prompt") { // Clipboard permissions available }
With clipboard permissions granted, you query the clipboard to get a ClipboardItem
instance with details of what’s been copied:
const [item] = await navigator.clipboard.read(); // When text is copied to clipboard.... item.types // ["text/plain"] // When an image is copied from a website... item.types // ["text/html", "image/png"]
Once you know the contents and the MIME type, you can get the text in clipboard with readText()
:
const content = await navigator.clipboard.readText();
In the case of an image, if you have the MIME type and content available, you can use <img>
with a data URI for display. Knowing the contents of a user’s clipboard can be helpful when presenting exactly what they’ve copied!
How to Create a RetroPie on Raspberry Pi – Graphical Guide
Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices. While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo…
CSS vs. JS Animation: Which is Faster?
How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point…
Translate Content with the Google Translate API and JavaScript
Note: For this tutorial, I’m using version1 of the Google Translate API. A newer REST-based version is available. In an ideal world, all websites would have a feature that allowed the user to translate a website into their native language (or even more ideally, translation would be…
[ad_2]
Source link