[英]How to Disable Copy Paste (Browser)
我正在嘗試兩種選擇:
這是我的代碼:
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 ("return false")
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();
});
為什么不嘗試使文本不可選擇?
.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 技巧上找到的文章。
您可以在頁面中禁用“上下文菜單”、“復制”、“剪切”和“粘貼”:
<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;
}
網站真實復制保護
在將其聲明為垃圾郵件或廣告之前,請訪問示例頁面以獲取證據。 嘗試從示例頁面復制內容。
有關更多詳細信息和購買信息,請發送電子郵件至 websecurecontent@protonmail.com
好處
支持所有語言(英語、印地語、泰米爾語、馬拉雅拉姆語等) 支持所有 CMS,包括 Wordpress、Drupal、Joomla 等 內容無法從頁面源復制。 無法使用開發者工具復制內容。 無法在任何瀏覽器上使用任何附加組件/擴展程序復制內容。 無法通過禁用 javascript 來復制內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.