annoyingnewbie
annoyingnewbie

Reputation: 163

Clear clipboard to prohibit unauthorised copying, insert message?

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

Upvotes: 7

Views: 25982

Answers (7)

Sanket Zad
Sanket Zad

Reputation: 1

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

Upvotes: 0

coderchad123lol
coderchad123lol

Reputation: 21

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

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

Upvotes: 3

M T
M T

Reputation: 1

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

Upvotes: 0

The Eskimonian
The Eskimonian

Reputation: 21

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

Upvotes: 0

James_pic
James_pic

Reputation: 3307

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.

Upvotes: -1

Justin
Justin

Reputation: 458

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.

Upvotes: 3

Related Questions