Html 防止复制网页中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8957443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:41:33  来源:igfitidea点击:

Prevent copying text in web-page

htmlcopy-pastecopy-protection

提问by Anton

I've got quiz application. Where robot ask different questions in chat, this questions belong to different areas of knowledges. User that answered question first, receive points. The problem is, that some users googling answers. I want somehow prevent users from coping question from web page and googling answers.

我有测验申请。机器人在聊天中提出不同的问题,这些问题属于不同的知识领域。先回答问题的用户,获得积分。问题是,一些用户在谷歌上搜索答案。我想以某种方式阻止用户处理来自网页的问题和谷歌搜索答案。

I'm not even sure, that this is possible, anyway probably someone got any ideas

我什至不确定,这是可能的,无论如何可能有人有任何想法

回答by mowwwalker

Here: How to disable text selection highlighting using CSS?

此处: 如何使用 CSS 禁用文本选择突出显示?

-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;

Disallow them from being able to answer when the window's onBlur event is fired. They can still use other devices, but they won't be able to cheat on the same computer.

当窗口的 onBlur 事件被触发时,禁止他们回答。他们仍然可以使用其他设备,但他们将无法在同一台计算机上作弊。

回答by Anuj Kaithwas

In your div tag where you paste your question, add the following line of code:

在粘贴问题的 div 标签中,添加以下代码行:

<div id="test" onmousedown='return false;' onselectstart='return false;'>

This will prevent copying of anything that is within the tags...

这将防止复制标签内的任何内容......

回答by Pekka

There is no good way to do this. A cheater will be able to work around pretty much everything.

没有好的方法可以做到这一点。作弊者几乎可以解决所有问题。

The only thing that comes to mind is to output the questions as dynamically generated images. That would protect against copy-pasting. You'll have to decide how much of a protection that really is, though - most short questions can be typed into Google in no time, can't they?

唯一想到的是将问题输出为动态生成的图像。这将防止复制粘贴。不过,您必须决定真正的保护程度 - 大多数简短的问题都可以立即输入 Google,不是吗?

回答by shiggity

Note that this question might be found via Google by people who want to overridea no-copy rule via a Greasemonkey script or the like on the browser side.

请注意,想要通过浏览器端的 Greasemonkey 脚本等覆盖无复制规则的人可能会通过 Google 找到这个问题。

In addition to select disabling, I've seen the following tactic in at least one website:

除了选择禁用之外,我至少在一个网站上看到了以下策略:

<body oncopy="return false" onpaste="return false" oncut="return false">...</body>

回答by Guest

You could also make the page an image instead of html/text.

您还可以使页面成为图像而不是 html/text。

It is not easy to copy the text from an image. It would have to be saved and OCR'd.

从图像中复制文本并不容易。它必须被保存和OCR。

回答by Cornel Ghiban

Could you place a transparent PNG on top of the element that contains the quiz/question?

你能在包含测验/问题的元素顶部放置一个透明的 PNG 吗?

回答by Sheetal Kumar Maurya

If you are using JQuery then use:

如果您使用的是 JQuery,请使用:

function disableSelection(target){
    $(function() {
         $(this).bind("contextmenu", function(e) {
             e.preventDefault();
         });
     }); 
     if (typeof target.onselectstart!="undefined") //For IE 
          target.onselectstart=function(){return false}
     else if (typeof target.style.MozUserSelect!="undefined") //For Firefox
          target.style.MozUserSelect="none"
     else //All other route (For Opera)
          target.onmousedown=function(){return false}
     target.style.cursor = "default";
}

Call this function where you want to disable.

在要禁用的地方调用此函数。

$(document).ready(function(){
     disableSelection(document.body);
});

回答by WilliamK

You can use the ArtistScope Site Protection System (ASPS) to prevent all copy including screen capture and recording or just the ArtisBrowser on its own to prevent copy-n-paste and drag-n-drop. It is the most secure solution and proven. In use by online tutors and universities worldwide.

您可以使用 ArtistScope 站点保护系统 (ASPS) 来防止所有复制,包括屏幕捕获和录制,或者仅使用 ArtisBrowser 来防止复制粘贴和拖放。它是最安全的解决方案,并且经过验证。被世界各地的在线导师和大学使用。

回答by Ilan Schemoul

For future googlers who may not want to block highlighting or want to allow user to copy a limited number of characters :

对于可能不想阻止突出显示或希望允许用户复制有限数量的字符的未来 googlers :

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

回答by Alok Goyal Dadri U.P

<head>
<script type='text/javascript'>
var isCtrl = false;
document.onkeyup=function(e)
{
if(e.which == 17)
isCtrl=false;
}
document.onkeydown=function(e)
{
if(e.which == 123)
isCtrl=true;
if (((e.which == 85) || (e.which == 65) || (e.which == 88) || (e.which == 67) || (e.which == 86) || (e.which == 2) || (e.which == 3) || (e.which == 123) || (e.which == 83)) && isCtrl == true)
{
alert('This is Function Disabled');
return false;
}
}
// right click code
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
function mischandler(){
    alert('This is Function Disabled');
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
//select content code disable  alok goyal
function killCopy(e){
return false
}
function reEnable(){
return true
}
document.onselectstart=new Function ("return false")
if (window.sidebar){
document.onmousedown=killCopy
document.onclick=reEnable
}
</script>
</head>

<body>
  <h2>Disable code right click and ctrl a, ctrl u, ctrl c, ctrl v key and f12 and select content code</h2>
  <div>
    Some text...
  </div>
</body>