简体   繁体   English

如何禁用复制粘贴(浏览器)

[英]How to Disable Copy Paste (Browser)

I am trying 2 alternatives:我正在尝试两种选择:

  • Ignore right-click忽略右键单击
  • Ignore ctrl + C , ctrl + A忽略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:这是我的 HTML:

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

The noMenu() function is working, but disableCopyPaste() doesn't work. noMenu() function 正在运行,但disableCopyPaste()不起作用。

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…).您可以尝试阻止某些向量(例如黑客使右键单击更加困难,拦截ctrl + c ,使其难以选择文本)......但它们只会起作用,并且不可能阻止所有向量(编辑 - > 复制?查看源代码? wget ?等等)。

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.如果您有必须受到保护的敏感内容,您可能需要考虑将其嵌入 Flash blob 或 DRM 格式的 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.htmlhttp://myblog-log.blogspot.com/2007/06/disable-copy-and-paste.html 上找到

Javascript: 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: 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 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.我正在编写一个 JS 函数,当用户复制剪贴板时,它会被覆盖。 Anyway will post that when done.无论如何都会在完成后发布。 Still experimenting with it.还在试验它。 You can read the article that I found on css tricks.您可以阅读我在 css 技巧上找到的文章。

https://css-tricks.com/copy-paste-the-web/ 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.您可以使用 CSS 不允许任何文本选择,因此不会有任何文本复制的机会。

Add the below CSS and JS into the body:将以下 CSS 和 JS 添加到正文中:

CSS: 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: 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.这适用于 Chrome、Firefox、Safari、IE11 和 Edge。 For my testing, I was working with a <div contenteditable> .对于我的测试,我正在使用<div contenteditable> Source article:来源文章:

https://www.codexworld.com/disable-mouse-right-click-cut-copy-paste-using-jquery 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.如果您正在寻找简单的 HTML 来禁用特定元素上的复制和粘贴,请使用以下代码。 You can even block on the whole page by putting it on the body tag.您甚至可以通过将其放在 body 标签上来阻止整个页面。

<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" 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:您可以使用以下JS函数和CSS定义来完全防止在您的页面上复制粘贴:

<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的 class 看起来像:

.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/ 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/ http://www.securebit.xyz/

For more details and purchase information kindly write to us on websecurecontent@protonmail.com有关更多详细信息和购买信息,请发送电子邮件至 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.支持所有语言(英语、印地语、泰米尔语、马拉雅拉姆语等) 支持所有 CMS,包括 Wordpress、Drupal、Joomla 等 内容无法从页面源复制。 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.无法通过禁用 javascript 来复制内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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