Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Disable Copy Paste (Browser)

I am trying 2 alternatives:

  • Ignore right-click
  • Ignore ctrl + C, ctrl + A

This is my code:

function noMenu() {
  return false;
}
function disableCopyPaste(elm) {
  // Disable cut/copy/paste key events
  elm.onkeydown = interceptKeys
  // Disable right click events
  elm.oncontextmenu = function() {
    return false
  }
}
function interceptKeys(evt) {
  evt = evt||window.event // IE support
  var c = evt.keyCode
  var ctrlDown = evt.ctrlKey||evt.metaKey // Mac support
  // Check for Alt+Gr (http://en.wikipedia.org/wiki/AltGr_key)
  if (ctrlDown && evt.altKey) return true
  // Check for ctrl+c, v and x
  else if (ctrlDown && c==67) return false // c
  else if (ctrlDown && c==86) return false // v
  else if (ctrlDown && c==88) return false // x
  // Otherwise allow
  return true
}

And this is my HTML:

<body class="node88" oncontextmenu="return noMenu();" onkeydown="return disableCopyPaste();">

The noMenu() function is working, but disableCopyPaste() doesn't work.

like image 379
Rys Avatar asked Mar 31 '12 18:03

Rys


People also ask

How do I enable copy and paste on a disabled website?

Enable copy paste on websites that have disabled copy paste. How to use: - Click on the extension icon - After a popup is opened, use the “Enable copy paste for all websites” checkbox. - Manually refresh the page and see if the extension has successfully enabled copy paste functionality on the website.

How do I restrict copy and paste in HTML?

You can allow text selection, but prevent copy and cut functions using the oncopy, oncut and onpaste event attributes. By adding these attributes into a textbox's <input> tag, you can disable cut, copy and paste features. The user is left with the option to enter the field manually with these attributes set.


3 Answers

You can control what text is put into the clipboard:

document.addEventListener('copy', function(e) {
    e.clipboardData.setData('text/plain', 'Please do not copy text');
    e.clipboardData.setData('text/html', '<b>Please do not copy text</b>');
    e.preventDefault();
});

https://developer.mozilla.org/en-US/docs/Web/Events/copy

like image 147
hello_luke Avatar answered Sep 27 '22 17:09

hello_luke


You can't.

You can sort of try to block some vectors (like hacks to make right clicking more difficult, intercepting ctrl+c, making it difficult to select text)… But they will only sort of work, and it's impossible to block all vectors (edit -> copy? view source? wget? etc…).

If you are trying to protect your content from less technical users, these methods might be okay… But as the comments here suggest, they will frustrate more technical users.

If you have sensitive content that must be protected, you might want to consider embedding it in a Flash blob or a DRM'd PDF. These are still possible to reverse engineer, but it will take a slightly more intelligent attacker.

like image 18
David Wolever Avatar answered Oct 23 '22 01:10

David Wolever


Instead of trying to control the users key commands(it is possible some browsers may detect this as malicious code) you can disable selection of text on your page. Although this will not avoid data being copied as stated in your comments.

<!-- Disable Copy and Paste-->
<script language='JavaScript1.2'>
function disableselect(e) {
    return false
}

function reEnable() {
    return true
}

document.onselectstart = new Function (&quot;return false&quot;)

if (window.sidebar) {
    document.onmousedown = disableselect
    document.onClick = reEnable
}
</script>

Place this in your

    <head> </head> 

tags and the user cannot select text on your page.

Found on http://myblog-log.blogspot.com/2007/06/disable-copy-and-paste.html

like image 10
Rich Avatar answered Oct 23 '22 01:10

Rich