Search code examples
javascriptcopy

JavaScript - Copy to clipboard does not work on Windows machines


I have this function, works perfectly in unix based OS (Linux, MacOS), but somehow, does not works in Windows, it just simply does not add anything to the clipboard.

This is running in a NextJS App.

export const copyRichContent = async rich => {

    const html = new Blob([rich], { type: 'text/html' })
    const data = new ClipboardItem({ 'text/html': html })
    await navigator.clipboard
         .write([data])
         .then(e => console.log('All good', e))
         .catch(err => {
           console.error(err)
         })

}

  • I already tried with clipboard-pollyfill
  • I tried in Windows with Firefox and Chrome
  • If I use the MIME type "text/plain", it works (but I need to use html)
  • The app is running securely (with HTTPS)
  • I confirmed I have the write text and write text delayed permissions
  • No errors in the catch block

I expect to be working in Windows machines, no matter the browser.


Solution

  • I was not pasting the copied data into a RCE (What a moron, right?).