Saturday, October 14, 2023
HomeSoftware DevelopmentDetect the Content material Sort within the Clipboard

Detect the Content material Sort within the Clipboard


A consumer’s clipboard is a “catch all” between the working system and the apps employed on it. If you use an online browser, you possibly can spotlight textual content or right-click a picture and choose “Copy Picture”. That made me take into consideration how builders can detect what’s within the clipboard.

You’ll be able to retrieve the contents of the consumer’s clipboard utilizing the navigator.clipboard API. This API requires consumer permission because the clipboard may include delicate information. You’ll be able to make use of the next JavaScript to get permission to make use of the clipboard API:

const outcome = await navigator.permissions.question({title: "clipboard-write"});
if (outcome.state === "granted" || outcome.state === "immediate") {
  // Clipboard permissions out there
}

With clipboard permissions granted, you question the clipboard to get a ClipboardItem occasion with particulars of what is been copied:

const [item] = await navigator.clipboard.learn();

// When textual content is copied to clipboard....
merchandise.sorts // ["text/plain"]

// When a picture is copied from an internet site...
merchandise.sorts // ["text/html", "image/png"]

As soon as you realize the contents and the MIME kind, you may get the textual content in clipboard with readText():

const content material = await navigator.clipboard.readText();

Within the case of a picture, in case you have the MIME kind and content material out there, you should use <img> with a knowledge URI for show. Figuring out the contents of a consumer’s clipboard could be useful when presenting precisely what they’ve copied!

  • Create Spinning, Fading Icons with CSS3 and jQuery
  • Using CSS attr and content for Tooltips


Supply hyperlink
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments