Read file from clipboard outside the paste event in a browser

Viewed 421

I'd like to know if there is a file on the user Clipboard data.

This is possible when I capture the paste event like here:

document.addEventListener('paste', (event) => {
  const data = event.clipboardData;
  const file = data && data.files && data.files[0];
  if (file) {
      // do something with the file.
  }
  
});

However this requires the user to paste it to the page. I'd like to achieve it without them doing any action, similar to capturing a text from the clipboard:

setInterval(()=>{
    const text = await navigator.clipboard.readText();
    if (text){
       // do something....
    }
},1000)

I can run this code anytime i want and "find out" if the user has any text on their clipboard.

The issue is I cannot access files like that. I get an empty string if there is a file on the clipboard.

I understand it might be a security issue, but at least I'd like to know if there is any file on their clipboard (rather than text)

1 Answers
Related