今天在看http://www.yu1u.org/3419.html这里的网页的时候发现,这里的文字图片不能直接复制,鼠标右键没有反应,经简单查看,可以发现限制这些功能的代码大致如下:
document.body.oncontextmenu=function(){return false;};
document.body.ondragstart=function(){return false;};
document.body.onselectstart=function(){return false;};
document.body.onbeforecopy=function(){return false;};
document.body.onselect=function(){document.selection.empty();};
document.body.oncopy=function(){document.selection.empty();};
事实上,很多网站防止拷贝的方法之一就是类似的方法,306doc图书馆的文章也是如此:

其关键性的代码为:
document.body.oncopy = function() {
var CurUserNameCookiescgcg = getCookie("LoginName");
if (CurUserNameCookiescgcg == "" || CurUserNameCookiescgcg == null) {
var docarttitle = document.getElementById("docarttitle");
var docencodetitle = "\";
if (docarttitle == null) {
docencodetitle = ""
} else {
docencodetitle = "&titleencode=" + docarttitle.value
}
$.ajax({
url: "http://www.360doc.com/ajax/GetLoginForm20130912.ashx?ArtID=" + ArticleID + "&type=2&arttype=" + CurArtType + docencodetitle,
cache: false,
success: function(result) {
$("#LayerLogin").html(result);
showBg("dialog", "dialog_content", "1")
},
error: onFailed
});
return false
} else {
var selhtml = "";
var selection;
if (window.getSelection) {
selection = window.getSelection();
if (selection != null) {
selhtml = selection.toString()
}
} else if (document.selection) {
selection = document.selection.createRange();
if (selection != null) {
selhtml = selection.text
}
}
if (selhtml.length > 200) {
document.getElementById('fuzhitishidiv').style.display = 'none';
if (getCookie("360doc1") != null && UserID != 0) {
$.ajax({
url: "http://www.360doc.com/ajax/GetLoginForm20130912.ashx?ArtID=" + ArticleID + "&type=5&arttype=" + CurArtType + "",
cache: false,
success: function(result) {
if (result == "-1") {
return true
} else {
if (document.getElementById("fuzhitishidiv") != null) {
document.getElementById("fuzhitishidiv").innerHTML = "
";
window.scroll(0, 0);
document.getElementById("fuzhitishidiv").style.display = ''
} else {
alert("提示:点击标题下方的“转藏到我的图书馆”,将文章保存到您的个人图书馆中,然后可以拷贝文章的内容!")
}
return false
}
},
error: onFailed
})
} else {
if (document.getElementById("fuzhitishidiv") != null) {
document.getElementById("fuzhitishidiv").innerHTML = "
";
window.scroll(0, 0);
document.getElementById("fuzhitishidiv").style.display = ''
} else {
alert("提示:点击标题下方的“转藏到我的图书馆”,将文章保存到您的个人图书馆中,然后可以拷贝文章的内容!")
}
return false
}
} else {
return true
}
}
}
为了解决以上的这些问题,首先想到的思路是:
一、把这些代码或者外部js链接给去掉,但是这样做根本不现实:
对于第一种情况,使用
浏览器的脚本屏蔽插件如ScriptBlock也到不到效果;屏蔽所有JS可能使得网页根本就无法正常显示 二、禁用浏览器的java script的执行
同样由于以上的原理2,同样不能奏效;
三、开发Chrome插件+API HOOK实现
此方法看来是最有效的了;
插件制作流程:
Chrome地址栏输入:chrome://extensions/ 打开扩展程序窗口;勾选开发者选项;
选择之前制作好的插件的目录在代码调试结束,确定发布之后,可以点击上图中的打包扩展程序进行打包;将生成的.crx文件拖进浏览器安装即可;
<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPC9ibG9ja3F1b3RlPgo8cD60+sLro7o8L3A+CjxwPm1hbmlmZXN0Lmpzb248YnI+CjwvcD4KPHA+PHByZSBjbGFzcz0="brush:java;">{ "name": "UBC", "manifest_version": 2, "version": "1.0.0", "description": "UnBlock Copy", "browser_action": { "default_icon": "https://www.cppentry.com/upload_files/article/49/1_nnext__.png", "default_title": "UnBlock Copy", "default_popup": "popup.html" }, "content_scripts": [{ "matches": ["http://*/*","https://*/*"], "js": ["js/hookapi.js"], "run_at": "document_end", "all_frames": true }] }
注意:
"manifest_version": 2,
这一行固定,新的Chrome插件开发规范,网上的大多数教程没有此行。
popup.html
- Unblock Copy???Event
- Unblock Paste??Event
- Unblock Select Event
hookapi.js
//UnHook Copy Related Events
var arr_hook_event = ["oncontextmenu","ondragstart","onselectstart","onselect","onbeforeco