@mcDec 20.2022 — #It's actually quite easy in the mordern world of today!
If you only want a plain text copied you simply need to make a Blob specifing the type - in this case - text/plain and the text to copy. E.g. Hi, WebDeveloper.com.
Using this Blob of text, you but it into a ClipboardItem and lastly ask the navigator kindly to copy that text for you.
An example would be:
const type = "text/plain"; const text = "Hi, WebDeveloper.com"; const blob = new Blob([text], { type }); const data = [new ClipboardItem({ [type]: blob })];
navigator.clipboard.write(data) // returns a promise
A neat feature of the ClipboardItem and navigator.clipboard.write in general, is that as you can see, the data speficied is an array. Meaning it can contain multiple entries - also in a clipboard manner.
Say you want to copy both a plain-text version and hyperlinked version of the above mentioned text. Sounds tedious, but it's quite easy, once we take the array into account.
const data = [ new ClipboardItem({ [typePlain]: new Blob([textPlain], { typePlain }) }), new ClipboardItem({ [typeHTML]: new Blob([textHTML], { typeHTML }) }) ];
navigator.clipboard.write(data) // still returns a promise
Now try pasting this magicially copied entry into a WYSIWYG-compatible editor, and a link navigating to https://webdeveloper.com/ has been pasted, along with the Hi, -prefix.
@hqauthorThanks Mads! I see you noticed these replies support <code> tags, so I updated your original answer and removed the trial-and-error replies to get there. 😉Dec 20.2022
@mcHaha, thanks! Well, I wasn't prepared for the links to be parsed 🤫 Is there a change, you'd edit it once again - as I'm obsessed with proper formatting for readability 😬 So for the two lines in the `data`-array, might you be able to indent them? Also, I see that I'm missing a quite important part of the answer 🤦♂️ If you're pasting into a non-`WYSIWYG`-compatible, it'll paste the first blob in this case - it simply finds that the one that it can understand, which means the plain text will be pasted 🤩Dec 20.2022