Based on Clipboard Write API specification I could copy to clipboard like below:
const type = "text/plain";
const text = "test plain";
const blob = new Blob([text], { type });
const data = [new ClipboardItem({ [type]: blob })];
navigator.clipboard.write(data).then(
() => {alert('success plain copy')},
() => {}
);
I tried that and that worked. But I tried to change the type to HTML or rich text like below:
const type = "text/html"
const text = "<b>text html</b>";
const blob = new Blob([text], { type });
const data = [new ClipboardItem({ [type]: blob })];
navigator.clipboard.write(data).then(
() => {alert('success html copy')},
() => {}
);
And it doesn't work. The success alert is shown up, but the HTML text is not copied.
I have conducted online research and based on my findings, my code appears to be correct. However, it is puzzling why it is not functioning as intended.
By the way, actually I want to create a SO snippet, but the permission is blocked, so if others want to try my codes, you check on jsfiddle
text/plain
type. not astext/html
– Initiative