Javascript 禁用网页中的复制/粘贴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3020715/
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-23 02:56:56  来源:igfitidea点击:

Disabling Copy/Paste in a Web Page

javascripthtml

提问by 1s2a3n4j5e6e7v

How Do I disable the copy paste feature in my webpage. To be precise, I don't want my users to copy any information from my website and use them for personal purposes. The previous question on the same topic doesn't give enough explanation. The onselect and ondrag aren't working. Please help.

如何在我的网页中禁用复制粘贴功能。准确地说,我不希望我的用户从我的网站复制任何信息并将其用于个人目的。关于同一主题的上一个问题没有给出足够的解释。onselect 和 ondrag 不起作用。请帮忙。

回答by Sev

I don't want my users to copy any information from my website and use them for personal purposes

我不希望我的用户从我的网站复制任何信息并将其用于个人目的

There is no way to do this. If someone really wants your information, they can get it.

没有办法做到这一点。如果有人真的想要你的信息,他们可以得到。

You might be able to give them a litte bit of trouble with disabling certain functions using javascript or whatever...but you'll only give the people who don't know much about technology that trouble. And usually those people aren't even trying to copy your data. The one's who are, will figure out a way.

您可能会给他们带来一些麻烦,即使用 javascript 或其他方式禁用某些功能……但您只会给对技术不太了解的人带来麻烦。通常这些人甚至不会尝试复制您的数据。谁是,会想办法。

回答by Fenton

If you publish information online, you should clearly indicate your copyright claim on the page (or indicate the type of license you issue the content under). Please find and read the copyright law of your territory to understand what this does and doesn't allow - for example, in the UK there are provisions for making personal copies of copyrighted material and for using parts of copyrighted work for critical review or parody.

如果您在线发布信息,您应该在页面上清楚地表明您的版权主张(或表明您发布内容所依据的许可类型)。请查找并阅读您所在地区的版权法,以了解这允许和不允许的内容 - 例如,在英国,有关于制作受版权保护材料的个人副本以及使用部分受版权保护作品进行批判性或模仿的规定。

You can't stop people from copying the content on your page. You can make it more difficult for them to do - but this will have a negative impact on your page. Techniques such as preventing the left-click of the mouse, intercepting keyboard events or converting your entire article into images just make your website less usable.

您无法阻止人们复制您页面上的内容。你可以让他们更难做 - 但这会对你的页面产生负面影响。诸如防止鼠标左键单击、拦截键盘事件或将整篇文章转换为图像等技术只会降低网站的可用性。

If you have textual information on your website, I can re-type it even if you've stopped every other method of me copying the image. If you have an image and you've managed to lock out everything else, I can still do a screen-grab (not to mention the fact that my browser caches all the images in a temporary folder on my machine).

如果您的网站上有文本信息,即使您已停止我复制图像的所有其他方法,我也可以重新输入它。如果你有一个图像并且你已经设法锁定了其他所有东西,我仍然可以进行屏幕抓取(更不用说我的浏览器将所有图像缓存在我机器上的一个临时文件夹中的事实)。

Your content-paranoia affects many people who set up a website - but the idea behind the Internet is that it is used for sharing information.

您的内容偏执症影响了许多建立网站的人 - 但互联网背后的想法是它用于共享信息。

回答by Hieu Nguyen Ngoc

Just add the following code to the HEAD tag of your web page:

只需将以下代码添加到网页的 HEAD 标记中:

<script type="text/JavaScript">
//courtesy of BoogieHyman.com
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>

回答by Adam McArthur

You need to rethink your strategy if you're resorting to these measures on the front end. What you are trying to do is inherently wrong.

如果您在前端采用这些措施,则需要重新考虑您的策略。你试图做的事情本质上是错误的。

As a visitor to your web page, pulling something like this is just going to annoy me - I will eventually figure out what you've done and get around it. That said, I've recently found this particular methodcan be quite effective if you're aiming to restrict impatient or non-technical users. Proceed with caution...

作为您网页的访问者,拉出这样的东西只会让我烦恼 - 我最终会弄清楚您做了什么并绕过它。也就是说,如果您的目标是限制不耐烦或非技术用户,我最近发现这种特殊方法非常有效。谨慎行事...

<div class="text">
  <p>Hello, world! Sadly, <a href="#">I won't work</a>.</p>
  <img alt="I can't be dragged or saved either :(" src="tree.png"> 
  <div class="preventSelect"></div>
</div>

...and the CSS:

...和CSS:

.text {
  position: relative;
  width: auto; /* can be fixed as well (ie 400px) */
  width: auto; /* can be fixed as well (ie 400px) */
  z-index: 0;
}

.preventSelect {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1;
}

The obvious drawback for this method is that the user cannot interact with anything inside the div we're preventSelecting. That includes links, buttons, images etc.

这种方法的明显缺点是用户无法与我们正在使用的 div 中的任何内容进行交互preventSelect。这包括链接、按钮、图像等。

Please don't use this unless you absolutely have to. Frankly, it's a pain in the ass for everyone.

除非万不得已,请不要使用它。坦率地说,这对每个人来说都是一种痛苦。

回答by FWishbringer

By default, Chrome and Firefox block disabling the right click menu. You have to manually edit an entry in about:config in Firefox to prevent it being blocked, which is not something you can force your visitors to do.

默认情况下,Chrome 和 Firefox 会阻止禁用右键单击菜单。您必须在 Firefox 中手动编辑 about:config 中的条目以防止它被阻止,这不是您可以强迫访问者执行的操作。

Regarding IE, you can modify your BODY tag like so:

关于 IE,您可以像这样修改您的 BODY 标签:

<body onContextMenu="return false">

Which will prevent the right click context menu.

这将阻止右键单击上下文菜单。

Other than that, the next best step is to create an image of your text, place it in a .swf (flash) document, and point the page to load the .swf as the page. This will cause all browsers to display the flash context menu on right click, and will prevent simple copy/paste efforts.

除此之外,下一个最佳步骤是创建文本图像,将其放入 .swf(flash)文档中,然后指向页面以加载 .swf 作为页面。这将导致所有浏览器在右键单击时显示 flash 上下文菜单,并将阻止简单的复制/粘贴工作。

I do agree with previous replies, regardless of method used, any user can simply use their Print Screen key, paste the image in Paint (or other program), save it, and use OCR to grab your text.

我同意以前的答复,无论使用何种方法,任何用户都可以简单地使用他们的 Print Screen 键,将图像粘贴到 Paint(或其他程序)中,保存它,然后使用 OCR 来抓取您的文本。

回答by FWishbringer

To be honest, if you don't want people to use any information on your site, then you can't put it up there. If you stop them from being able to copy and paste the information, they'll still be able to take a screenshot of it, type it out and save the data that way. I know it's not the answer you're looking for, but that's just something to think about.

老实说,如果您不希望人们使用您网站上的任何信息,那么您就不能把它放在那里。如果您阻止他们复制和粘贴信息,他们仍然可以对其进行截图、输入并以这种方式保存数据。我知道这不是您要寻找的答案,但这只是需要考虑的问题。

(I did this because i can't comment yet).

(我这样做是因为我还不能发表评论)。

回答by Pekka

Forget it. It is not possible to block these functions in a browser. The "best" you can do is to present your data in an image or Flash movie - inconceivable, slow, impractical, horrible to implement and also circumventable using OCR software.

算了吧。无法在浏览器中阻止这些功能。您可以做的“最好”是在图像或 Flash 电影中呈现您的数据 - 不可思议、缓慢、不切实际、实施起来很糟糕,并且还可以使用 OCR 软件来规避。

If all else fails, users will simply make screen shots or key in the data manually.

如果所有其他方法都失败了,用户只需手动制作屏幕截图或键入数据即可。

If you present data to your users, you will have to live with the possibility that they can copy it. End of story.

如果您向用户展示数据,您将不得不忍受他们可以复制数据的可能性。故事结局。

Use legal threats to prevent your contents, not technical means.

使用法律威胁来阻止您的内容,而不是技术手段。

回答by Thariama

It is impossible to secure a website against copying. There are some technices to make it more difficult, but as soon as the user has the information on his screen its already too late. He could for example take a picture with a camera if the screenshot function could be disabled somehow.

防止网站被复制是不可能的。有一些技术可以让它变得更加困难,但是一旦用户在他的屏幕上看到信息就已经太晚了。例如,如果可以以某种方式禁用屏幕截图功能,他可以用相机拍照。

Disabling of javascript functionality (f.e. shortcuts) is not working in all browsers and the user may disable javascript.

禁用 javascript 功能(fe 快捷方式)并非在所有浏览器中都有效,用户可能会禁用 javascript。

Using programs like curl all the information on the webpage can be grabbed.

使用 curl 之类的程序可以抓取网页上的所有信息。

Best thing you could do is to put all the information you present into an image.

您能做的最好的事情是将您呈现的所有信息放入图像中。

回答by cHao

What the developers of lyrics.com have done is attach events to document.body.oncontextmenu, document.onselectstart, and document.body.onkeydownto disable the actions browsers would take.

什么lyrics.com开发商所做的是将事件附加到document.body.oncontextmenudocument.onselectstartdocument.body.onkeydown禁用浏览器的行动将采取。

It can be done as simply as

它可以像这样简单地完成

<body oncontextmenu="return false" onselectstart="return false"
      onkeydown="if ((arguments[0] || window.event).ctrlKey) return false">

You'd need all three; oncontextmenubasically governs right clicks, onselectstartcovers drag-selecting with the mouse, and onkeydownCtrl-key events (like someone who'd hit Ctrl+A, Ctrl+Cto copy the whole page).

你需要所有三个;oncontextmenu基本上控制右键单击,onselectstart包括用鼠标拖动选择和onkeydownCtrl 键事件(比如有人点击Ctrl+ A, Ctrl+C复制整个页面)。

But I highly recommend that you NOT DO THIS.It kills usability and frustrates even legitimate users (for example people that have certain key mappings set up, or the ones who use "back" and "reload" from the context menu), and the ones you'd have to worry about would not be hindered even the slightest bit. And frankly, your content is not as special as you think it is, or you wouldn't be serving it up to any loser with a web browser. Information that valuable is not put online.

我强烈建议你不要这样做。它扼杀了可用性,甚至使合法用户感到沮丧(例如,设置了某些键映射的人,或者从上下文菜单中使用“返回”和“重新加载”的人),而那些你不得不担心的人不会哪怕是最轻微的阻碍。坦率地说,您的内容并不像您想象的那么特别,否则您将不会使用 Web 浏览器将其提供给任何失败者。有价值的信息不会放在网上。

As has been noted before, all that return falsestuff is not enforceable. And because i found the page particularly infuriating, thatprompted me to pop up a console and dissect what they did, and detach event handlers so i could copy whatever i like and they don't even get their precious click-tracking data. Really, though, all anyone has to do is disable JavaScript.

正如之前所指出的,所有这些return false东西都是不可执行的。因为我发现这个页面特别令人生气,促使我弹出一个控制台并剖析他们所做的事情,并分离事件处理程序,这样我就可以复制我喜欢的任何东西,他们甚至没有得到他们宝贵的点击跟踪数据。不过,实际上,任何人所要做的就是禁用 JavaScript。

The only way to keep people from copying text from the internet, is to keep it off the internet. Any other way is doomed to fail, as you yourself are handing them a copy as part of the very act of serving it to them.

阻止人们从互联网上复制文本的唯一方法是将其远离互联网。任何其他方式都注定要失败,因为您自己正在将副本交给他们,作为将其提供给他们的行为的一部分

回答by Dan Heberden

You can't ever disable it.. users can view the source of your page so the text is alwaysavailable. If you put click handlers to disable right-click, they can turn javascript off..

您永远无法禁用它.. 用户可以查看您页面的源代码,因此文本始终可用。如果您将单击处理程序设置为禁用右键单击,则它们可以关闭 javascript。

The best you can try to do is make it inconvenient for people to deter them, but never can you prevent them.

您可以尝试做的最好的事情是让人们不方便阻止他们,但您永远无法阻止他们。