Attempting to copy a chunk of HTML to clipboard from VSCode extension view during copy event (to be able to strip off the background theming when pasted into apps like GMail).
/**
Based off of:
* https://stackoverflow.com/a/64711198/7858768
* https://stackoverflow.com/a/57279336/7858768
*/
function copyToClipboard(html) {
const container = document.createElement('div');
container.innerHTML = html;
container.style.position = 'fixed';
container.style.pointerEvents = 'none';
container.style.opacity = 0;
const blob = new Blob([html], {type: "text/html"});
const item = new ClipboardItem({"text/html": blob});
navigator.clipboard.write([item]).then(function() {
}, function(error) {
console.error("Unable to write to clipboard. Error:");
console.log(error);
});
}
This code works well when working with GMAIL but for some reason does not appear to allow pasting into Editors, like VSCode or Intelij.
When trying to get contents of clipboard from JS also appears not to be working.
const clipboardy = require('clipboardy');
clipboardy.readSync();
// Gives a string with zero length.
Inspecting the contents of the clipboard with MAC OS Clipboard Shows type as unknown.
While normal copy event shows up as text:
Is there some other type that is required to be used when using navigator.clipboard.write
or some other API to be used to be able to copy HTML without theming?