Skip to content

Instantly share code, notes, and snippets.

@rigalpatel001
Created August 17, 2024 11:12
Show Gist options
  • Save rigalpatel001/0f5031657a9f490bf4d5479658b9bfc2 to your computer and use it in GitHub Desktop.
Save rigalpatel001/0f5031657a9f490bf4d5479658b9bfc2 to your computer and use it in GitHub Desktop.
How to Quickly Find Out What’s in Your Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Clipboard Access Example</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 50px;
text-align: center;
}
button {
margin: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Test Clipboard Access in JavaScript</h1>
<button onclick="getClipboardContent()">Show Clipboard Content</button>
<button onclick="writeToClipboard('Hello, World!')">
Copy "Hello, World!" to Clipboard
</button>
<script>
async function getClipboardContent() {
try {
const readPermission = await navigator.permissions.query({
name: "clipboard-read",
});
if (
readPermission.state === "granted" ||
readPermission.state === "prompt"
) {
const text = await navigator.clipboard.readText();
alert("Clipboard content: " + text);
} else {
alert("Clipboard read access denied");
}
} catch (err) {
console.error("Failed to read clipboard contents:", err);
}
}
async function writeToClipboard(text) {
try {
const writePermission = await navigator.permissions.query({
name: "clipboard-write",
});
if (
writePermission.state === "granted" ||
writePermission.state === "prompt"
) {
await navigator.clipboard.writeText(text);
alert("Text copied to clipboard: " + text);
} else {
alert("Clipboard write access denied");
}
} catch (err) {
console.error("Failed to write to clipboard:", err);
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment