事件侦听器 'Copy' 不适用于 "Copy Link Address"

Event listener 'Copy' doesn't work with "Copy Link Address"

我想修改剪贴板,使其成为正则表达式匹配项。这行得通——只要我使用 Ctrl+C 或只是 "Copy"。如果我点击 "Copy link address"(或 "Copy link location"),事件侦听器似乎根本看不到复制的东西。这是屏幕录制:https://i.stack.imgur.com/L4bQD.gif

我做错了什么?

<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>

我找到了答案 - 这是故意的,正如 the documentation 在安全注意事项中解释的那样:

Enabling authors to change what is copied by a user, or to make an automated copy of something that was never selected and allowing unrestricted calls to paste information can raise various security concerns.

Some example scenarios include:

  • A user selects a link and copies it, but a different link is copied to the clipboard. The effect of this can range from an unexpected result on pasting to an attempted "phishing" attack.