javascript 清除剪贴板以禁止未经授权的复制、插入消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16748735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:50:25  来源:igfitidea点击:

Clear clipboard to prohibit unauthorised copying, insert message?

javascripthtmlclipboardclear

提问by annoyingnewbie

Is it possible to write your own message into the clipboard when copying website data using ctrl+c? I've found some Javascript that clears the clipboard - would be interesting to know if there's something that would write to it as well, i.e. replace the text in the clipboard with something like 'Please use the print edition of our website'.

使用 ctrl+c 复制网站数据时,是否可以将自己的消息写入剪贴板?我发现了一些可以清除剪贴板的 Javascript - 想知道是否有一些东西也可以写入它会很有趣,即将剪贴板中的文本替换为“请使用我们网站的印刷版”之类的内容。

  function clearData() {
    window.clipboardData.setData('text', '')
  }

  function cldata() {
    if (clipboardData) {
      clipboardData.clearData();
    }
  }
  setInterval("cldata()", 1000);
<body ondragstart="return false;" onselectstart="return false;" oncontextmenu="return false;" onload="clearData();" onblur="clearData();">

  <h1>Example text</h1>
<input type="text">

回答by Justin

You can't do it purely through JavaScript.

你不能完全通过 JavaScript 来做到这一点。

JavaScript editing of the clipboard is considered a security vulnerability(and there is much more discussion on this).

剪贴板的 JavaScript 编辑被认为是一个安全漏洞(对此有更多讨论)。

You could do it through hacks that uses Flash for clipboard access interacting with JavaScript.

您可以通过使用 Flash 与 JavaScript 交互的剪贴板访问来实现。

回答by The Eskimonian

You could place the following:

您可以放置​​以下内容:

$( document ).ready(function() {
    if (event.ctrlKey && event.keyCode == 67) {
        var inputFieldClear = document.createElement("input");
        inputFieldClear.setAttribute("value", "Insert Default Value Here");
        document.body.appendChild(inputFieldClear);
        inputFieldClear.select();
        document.execCommand('copy');
        inputFieldClear.remove();
        console.log("Attempting to Alter Clipboard")
}});

That would work in something like TamperMonkey - not sure if it could be incorporated into the sites source or not.

这将在诸如 TamperMonkey 之类的东西中起作用 - 不确定它是否可以合并到站点源中。

Hope it helps! :)

希望能帮助到你!:)

回答by James_pic

Yes, you can. The basic trick is that you detect when a user holds down Control, and select a different piece of text on the page.

是的,你可以。基本技巧是检测用户何时按住 Control 键,然后在页面上选择不同的文本。