简体   繁体   中英

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.

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.

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

Javascript:

//disable mouse drag select start

document.onselectstart = new Function('return false');

function dMDown(e) { return false; }

function dOClick() { return true; }

document.onmousedown = dMDown;

document.onclick = dOClick;

$("#document").attr("unselectable", "on"); 

//disable mouse drag select end

//disable right click - context menu

document.oncontextmenu = new Function("return false");


//disable CTRL+A/CTRL+C through key board start

//use this function


function disableSelectCopy(e) {

// current pressed key

    var pressedKey = String.fromCharCode(e.keyCode).toLowerCase();

    if (e.ctrlKey && (pressedKey == "c" || pressedKey == "x" || pressedKey == "v" || pressedKey == "a")) {

        return false;

    }

}

document.onkeydown = disableSelectCopy;


//or use this function

$(function () {

    $(document).keydown(function (objEvent) {

        if (objEvent.ctrlKey || objEvent.metaKey) {

            if (objEvent.keyCode == 65 || objEvent.keyCode == 97) {

                return false;

            }

        //}

        }

    });

});

CSS:

//disable selection through CSS for different browsers

#document, #ctl00_MasterPageBodyTag{
    user-select: none;
    -ms-user-select: none;
    -o-user-select:none;
    -moz-user-select: none;
    -khtml-user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

//where #document is the div for which select needs to be disabled and #ctl00_MasterPageBodyTag is the id of the body tag.

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

Why not try to make the text unselectable?

.unselectable {
  -webkit-user-select: none;  /* Chrome all / Safari all */
  -moz-user-select: none;     /* Firefox all */
  -ms-user-select: none;      /* IE 10+ */
  user-select: none;          /* Likely future */       
}


/*Mobile*/

-webkit-touch-callout: default   /* displays the callout */
-webkit-touch-callout: none      /* disables the callout */

I will also very soon edit this answer. I'm looking at the same issue. But I found some info on how to over write. I'm writing a JS function that when the user has copied the clipboard gets overwritten. Anyway will post that when done. Still experimenting with it. You can read the article that I found on css tricks.

https://css-tricks.com/copy-paste-the-web/

You can disable "context menu", "copy", "cut" and "paste" in page with:

<script type="text/javascript">
    document.oncontextmenu = new Function('return false')
    document.body.oncut = new Function('return false');
    document.body.oncopy = new Function('return false');
    document.body.onpaste = new Function('return false');
</script>

You can use CSS to not allow any selection of text, thus no chance of any copying of text will be there.

Add the below CSS and JS into the body:

CSS:

    <style>
    .unselectable
    {
        -moz-user-select:none;
        -webkit-user-select:none;
        cursor: default;
    }
    html
    {
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        -webkit-tap-highlight-color: rgba(0,0,0,0);
    }
</style>

JS:

<script id="wpcp_css_disable_selection" type="text/javascript">
var e = document.getElementsByTagName('body')[0];
if(e)
{
    e.setAttribute('unselectable',on);
}
</script>
$('some.selector').bind('cut copy paste', function (e) {
    e.preventDefault();
});

This works in Chrome, Firefox, Safari, IE11 and Edge. For my testing, I was working with a <div contenteditable> . Source article:

https://www.codexworld.com/disable-mouse-right-click-cut-copy-paste-using-jquery

If you are looking for simple HTML to disable COPY and PASTE on a specific element then use below code. You can even block on the whole page by putting it on the body tag.

<div oncontextmenu="return false" onkeydown="if ((arguments[0] || window.event).ctrlKey) return false"></div>

oncontextmenu="return false" onkeydown="if ((arguments[0] || window.event).ctrlKey) return false"

If you do not want to block highlighting or want to allow user to only copy a limited number of characters :

  function anticopy(event: ClipboardEvent) {    
    // @ts-ignore
    const clipboardData = event.originalEvent.clipboardData || window.clipboardData || event.originalEvent.clipboardData;
    const txt = window.getSelection().toString() || editor.getWin().getSelection().toString();
    if (txt.length > 200) {
      const no = 'You cannot copy more than 200 characters.';
      clipboardData.setData('text', no);
      clipboardData.setData('text/html', `<p>${no}</p>`);
    } else {
      const html = `<p><span data-user="${user.data.id}"></span> ${txt}</p>`;
      clipboardData.setData('text', txt);
      clipboardData.setData('text/html', html);
    }

    event.preventDefault();
  }

You can use the following JS functions and CSS definiton to completely prevent copy-paste on your page:

<script>  
    if (document.layers) {
        document.captureEvents(Event.MOUSEDOWN);
        document.onmousedown = clickNS4;
    }
    else if (document.all && !document.getElementById) {
        document.onmousedown = clickIE4;
    }

    $('body').bind('cut copy paste', function (e) {
        e.preventDefault();
        return false;
    });

    $("body").on("selectstart", function (e) {
        e.preventDefault();
        return false;
    });

    $("body").on("dragstart", function (e) {
        e.preventDefault();
        return false;
    });

    $("body").on("drop", function (e) {
        e.preventDefault();
        return false;
    });

    $(document).keydown(function (event) {
        if (event.ctrlKey == true && event.which == '86') {
            event.preventDefault();
            return false;
        }

        if (event.ctrlKey && (event.keyCode === 83 || event.keyCode === 65)) {
            event.preventDefault();
            return false;
        }
        else if (event.ctrlKey && event.keyCode === 80) {
            event.preventDefault();
            return false;
        }
    });

    $("body").addClass('unselectable');
</script>

unselectable class will look like:

.unselectable {
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

Website True Copy Protection

http://www.securebit.xyz/

Visit the sample page for proof before declaring this as spam or advertising. Try copying the content from the sample page.

http://www.securebit.xyz/

For more details and purchase information kindly write to us on websecurecontent@protonmail.com

Benefits

Support for all languages (English, Hindi, Tamil, Malayalam etc) Support for all CMS including Wordpress, Drupal, Joomla etc The contents cannot be copied from page source. The contents cannot be copied using Developer tools. The contents cannot be copied using any add-ons/extensions on any browser. The contents cannot be copied by disabling javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM