How to Disable Copy Paste (Browser)

后端 未结 10 651
旧时难觅i
旧时难觅i 2020-11-27 19:36

I am trying 2 alternatives:

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

This is my code:

相关标签:
10条回答
  • 2020-11-27 19:53

    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.

    0 讨论(0)
  • 2020-11-27 19:59

    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>
    
    0 讨论(0)
  • 2020-11-27 20:04
    $('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

    0 讨论(0)
  • 2020-11-27 20:05

    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

    0 讨论(0)
提交回复
热议问题