简体   繁体   中英

Copy html content to clipboard

I want to copy HTML content to the clipboard.

For example

1

In modern browsers (assuming you are working in the browser), you now have access to navigator.clipboard api to access the clipboard.

To copy html you just need to specify a MIME type of text/html

However, you can also specify multiple MIME types simultaneously (which is likely what you want so as to allow the clipboard to fallback to plain text when html isn't supported).

Something like this:

navigator.clipboard.write([new ClipboardItem({
  'text/plain': new Blob([element.innerText], {type: 'text/plain'}),
  'text/html': new Blob([element.innerHTML], {type: 'text/html'})
}))])

You can use this.

import sanitizeHtml from "sanitize-html";      
const copyToClipBoard =(str)=> {
                let data= sanitizeHtml(str,{allowedTags: [ ],allowedAttributes: {},allowedIframeHostnames: []})
                navigator.clipboard.writeText(data);
        }

sanitize-html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM