Need to gather HTML (to strip some of the styling) during the copy event.
Currently, attempting to do the following:
Add a listener for the 'copy' event
document.addEventListener('copy', (e) => {
console.log("e.target:" + e.target);
});
And then use clipboard api to copy the item Along the lines of:
var blob = new Blob([html], {type: "text/html"});
var item = new ClipboardItem({"text/html": blob});
navigator.clipboard.write([item])
However, when attempting to gather html on copy event I am using e.target
to gather the HTML. It appears that e.target.nextElementSibling
when called recursively should give the value of the next element. However, when items are selected from a list (without the entire list being selected) it does not appear to work as expected.
As example:
<html>
<head>
<title> The simplest HTML example
</title>
</head>
<body>
<h1> This is an HTML Page </h1>
<ul>
<li>
<a href="https://www.google.com/">goog</a>
</li>
<li>
<a href="https://www.bing.com/">bing</a>
</li>
</ul>
Not in list:
<a href="https://www.google.com/">goog</a>
<a href="https://www.bing.com/">bing</a>
</body>
</html>
document.addEventListener('copy', (e) => {
console.log("e.target:" + e.target);
console.log("e.target:" + e.target.nextElementSibling);
});
JSFiddle: https://jsfiddle.net/q8d9vogu/9/
Selecting the Not in list: goog bing
shows both of the elements. While selecting the list goog & bing ONLY shows google one (e.target.nextElementSibling is null).
How should we go about gathering the HTML that is copied within the copy event? (context: this is running within VSCode extension)