Skip to content

Instantly share code, notes, and snippets.

@psenger
Created September 22, 2024 22:01
Show Gist options
  • Save psenger/0000ce35bf018cfbc92c203957aefec7 to your computer and use it in GitHub Desktop.
Save psenger/0000ce35bf018cfbc92c203957aefec7 to your computer and use it in GitHub Desktop.
[How to programmatically get data in JavaScript directly into your buffer] #Browser #JavaScript #clipboard #copy-cut-paste

How to programmatically get data in JavaScript directly into your buffer

Important Notes:

  • This API requires a secure context (HTTPS).
  • Users must interact with the page (e.g., click a button) before clipboard access is allowed for security reasons.
const toClipboard = async (text) => {

	try {
		await navigator.clipboard.writeText(text);
	}
	catch (error) {
		console.log(error)
	}
}
const fromClipboard = async () => {

	try {
		return navigator.clipboard.readText();
	}
	catch (error) {
		console.log(error)
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment