I want to modify the clipboard so that it becomes the regex match. And that works - as long as I use Ctrl+C or just "Copy". If I click on "Copy link address" (or "Copy link location"), the event listener doesn't seem to see the copied thing at all. Here's the screen recording: https://i.stack.imgur.com/L4bQD.gif
What am I doing wrong?
<html>
<meta charset="ISO-8859-1">
<div class="source">
<a href="https://www.example.com/example">https://www.example.com/example</a>
</div>
<div class="target" contenteditable="true">Copy the link above here</div>
</html>
<script>
var regex = /\w{2,}.\w{2,}.\w{2,}/;
document.body.addEventListener('copy', (event) => {
const selection = document.getSelection();
console.log(selection.toString());
event.clipboardData.setData('text/plain', selection.toString().match(regex));
event.preventDefault();
});
</script>
I have found the answer - this is intentional, as the documentation explains in the Security Considerations: