Clear clipboard to prohibit unauthorised copying, insert message?
Asked Answered
F

7

7

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">
Frohne answered 25/5, 2013 at 10:42 Comment(2)
Websites that attempt to subvert standard browser functionality are really irritating. Please don't!Pretension
A user can still save the page locally as HTML, then copy text from that anyway...Exploiter
I
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.

Isthmus answered 25/5, 2013 at 16:32 Comment(1)
But it is possible to write to the clipboard using the async clipboard API.Houppelande
C
3

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("");
Chadwickchae answered 11/8, 2021 at 5:8 Comment(0)
J
1

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();
})
Johnajohnath answered 5/1, 2022 at 23:41 Comment(0)
C
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! :)

Carpel answered 26/9, 2018 at 19:37 Comment(0)
I
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">
Imogene answered 6/1, 2021 at 12:44 Comment(2)
Welcome to SO. Please explain your answer for future readers.Domingodominguez
Ouch, I haven't seen anyone in the last 6-8 years passing a string of code to setInterval. I hope you know that it's not recommended.Opportina
I
0

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">
Icarian answered 25/7 at 6:8 Comment(0)
M
-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.

Mazard answered 15/4, 2014 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.