get clipboard value then display alert with value synchronously in javascript

To get the value from the clipboard and display it in an alert synchronously using JavaScript, you can use the navigator.clipboard.readText() method to read the contents of the clipboard and then display the value using the alert() function. Here's how you can achieve this:

index.tsx
async function readClipboard() {
  try {
    const clipboardValue = await navigator.clipboard.readText();
    alert(`Clipboard Value: ${clipboardValue}`);
  } catch (error) {
    console.error('Failed to read clipboard contents: ', error);
  }
}

readClipboard();
264 chars
11 lines

In the above code, we define an async function readClipboard() that reads the contents of the clipboard using the navigator.clipboard.readText() method. This method returns a Promise, so we use the await keyword to wait until the Promise is resolved.

Once the value is retrieved, we display it in an alert using the alert() function. Finally, we call the readClipboard() function to read the clipboard value synchronously and display it in an alert.

gistlibby LogSnag