6

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">
braX
  • 11,506
  • 5
  • 20
  • 33
annoyingnewbie
  • 153
  • 1
  • 3
  • 12

6 Answers6

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.

Community
  • 1
  • 1
Justin
  • 471
  • 2
  • 8
1

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("");
0

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! :)

0

  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">
M T
  • 1
0

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();
})
-1

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.

Community
  • 1
  • 1
James_pic
  • 3,240
  • 19
  • 24