最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Clear clipboard to prohibit unauthorised copying, insert message? - Stack Overflow

programmeradmin2浏览0评论

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

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

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

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

Share Improve this question edited Jun 26, 2020 at 10:54 braX 11.8k5 gold badges22 silver badges37 bronze badges asked May 25, 2013 at 10:42 annoyingnewbieannoyingnewbie 1631 gold badge3 silver badges12 bronze badges 2
  • 10 Websites that attempt to subvert standard browser functionality are really irritating. Please don't! – Oliver Charlesworth Commented May 25, 2013 at 10:44
  • A user can still save the page locally as HTML, then copy text from that anyway... – Qantas 94 Heavy Commented May 25, 2013 at 10:49
Add a comment  | 

7 Answers 7

Reset to default 3

You can't do it purely through JavaScript.

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

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

You cannot clear clipboard data since there is no function for that.

Best way to remove it is to assign null values.

ie

navigator.clipboard.writeText("");

You can't clear a user's clipboard history. But,

You can replace their clipboard with something else like

navigator.clipboard.writeText(" ");

Or you can make a script that whenever they try to copy something it stops it.

document.addEventListener('copy', function(e){
    e.preventDefault();
})

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.

Hope it helps! :)

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

Basically; we need to provide the reference of the function in setInterval function; we don't need to call it explicitly. The calling/invoking of the referenced function will be taken care of by the setInterval function.

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

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.

发布评论

评论列表(0)

  1. 暂无评论