繁体   English   中英

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

[英]How to Disable Copy Paste (Browser)

我正在尝试两种选择:

  • 忽略右键单击
  • 忽略ctrl + C , ctrl + A

这是我的代码:

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
}

这是我的 HTML:

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

noMenu() function 正在运行,但disableCopyPaste()不起作用。

你不能。

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

如果您试图保护您的内容免受技术含量较低的用户的侵害,这些方法可能没问题……但正如这里的评论所暗示的那样,它们会让更多技术用户感到沮丧。

如果您有必须受到保护的敏感内容,您可能需要考虑将其嵌入 Flash blob 或 DRM 格式的 PDF。 这些仍然可以进行逆向工程,但需要更聪明的攻击者。

您可以禁用页面上的文本选择,而不是试图控制用户的键盘命令(某些浏览器可能会将其检测为恶意代码)。 尽管这不会避免如您的评论中所述复制数据。

<!-- 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>

把这个放在你的

    <head> </head> 

标签,用户无法在您的页面上选择文本。

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.

您可以控制将哪些文本放入剪贴板:

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

为什么不尝试使文本不可选择?

.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 */

我也将很快编辑这个答案。 我在看同样的问题。 但是我找到了一些关于如何覆盖的信息。 我正在编写一个 JS 函数,当用户复制剪贴板时,它会被覆盖。 无论如何都会在完成后发布。 还在试验它。 您可以阅读我在 css 技巧上找到的文章。

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

您可以在页面中禁用“上下文菜单”、“复制”、“剪切”和“粘贴”:

<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>

您可以使用 CSS 不允许任何文本选择,因此不会有任何文本复制的机会。

将以下 CSS 和 JS 添加到正文中:

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();
});

这适用于 Chrome、Firefox、Safari、IE11 和 Edge。 对于我的测试,我正在使用<div contenteditable> 来源文章:

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

如果您正在寻找简单的 HTML 来禁用特定元素上的复制和粘贴,请使用以下代码。 您甚至可以通过将其放在 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"

如果您不想阻止突出显示或只想允许用户只复制有限数量的字符:

  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();
  }

您可以使用以下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 看起来像:

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

网站真实复制保护

http://www.securebit.xyz/

在将其声明为垃圾邮件或广告之前,请访问示例页面以获取证据。 尝试从示例页面复制内容。

http://www.securebit.xyz/

有关更多详细信息和购买信息,请发送电子邮件至 websecurecontent@protonmail.com

好处

支持所有语言(英语、印地语、泰米尔语、马拉雅拉姆语等) 支持所有 CMS,包括 Wordpress、Drupal、Joomla 等 内容无法从页面源复制。 无法使用开发者工具复制内容。 无法在任何浏览器上使用任何附加组件/扩展程序复制内容。 无法通过禁用 javascript 来复制内容。

暂无
暂无

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

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