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

Rys*_*Rys 11 html javascript jquery keyboard-shortcuts

我正在尝试两种选择:

  • 忽略右键单击
  • 忽略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
}
Run Code Online (Sandbox Code Playgroud)

这是我的HTML:

<body class="node88" oncontextmenu="return noMenu();" onkeydown="return disableCopyPaste();">
Run Code Online (Sandbox Code Playgroud)

noMenu()功能正在运行,但disableCopyPaste()不起作用.

Dav*_*ver 17

你不能.

你可以尝试阻止一些向量(比如黑客使得点击更加困难,拦截ctrl+ c,使得难以选择文本)......但它们只会有点工作,并且不可能阻止所有向量(编辑 - >复制?查看来源?wget等等......).

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

如果您的敏感内容必须受到保护,您可能需要考虑将其嵌入Flash blob或DRM'dPDF中.这些仍然可以进行逆向工程,但需要一个稍微聪明的攻击者.

  • +1用于澄清和uber-pro` <kbd>`标签^.^ (2认同)

Ric*_*ich 10

您可以禁用页面上的文本选择,而不是尝试控制用户键命令(某些浏览器可能将其检测为恶意代码).虽然这不会避免按照您的评论中的说明复制数据.

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

把它放在你的

    <head> </head> 
Run Code Online (Sandbox Code Playgroud)

标签和用户无法选择页面上的文字.

http://myblog-log.blogspot.com/2007/06/disable-copy-and-paste.html上找到


小智 6

使用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;

            }

        //}

        }

    });

});
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)


dra*_*mus 5

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

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();
});
Run Code Online (Sandbox Code Playgroud)

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