Skip to content

Instantly share code, notes, and snippets.

@kurrik
Created July 7, 2022 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurrik/c5ed674fa39664de08cb84ecae90bd88 to your computer and use it in GitHub Desktop.
Save kurrik/c5ed674fa39664de08cb84ecae90bd88 to your computer and use it in GitHub Desktop.
Read clipboard data in browser

Read clipboard data in the browser.

You can paste this into devtools. Note that the clipboard API requires the document to be focused, so we wrap in a timeout. You need to switch focus to the active document before the timeout fires.

window.setTimeout(async () => {
   const contents = await navigator.clipboard.read();
   for (var i = 0; i < contents.length; i++) {
       const item = contents[i];
       const types = item.types;
       await types.forEach(async (t) => {
           const value = await item.getType(t);
           const text = await value.text();
           console.log('Type', t, 'Text', text);
       });
   }
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment