简体   繁体   中英

How to copy html and plain text to clipboard in javascript?

I try to copy a HTML and plain text set to clipboard, so that each editor can choose if he wants to paste text/html or text/plain .

I am using navigator.clipboard.write() (the async clipboard api ).

The trick is quite simple, you can pass multiple blobs to one ClipboardItem :

const blob = new Blob([
    '<a href="https://stackoverflow.com/">goto stack</a>'
], { type: "text/html" });
const blobPlain = new Blob(['goto stack'], { type: "text/plain" });

navigator.clipboard.write([
    new ClipboardItem({
        [blob.type]: blob,
        [blobPlain.type]: blobPlain
    },),
])

Be aware this will not work in every browser (right now).

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