今天在看http://www.yu1u.org/3419.html这里的网页的时候发现,这里的文字图片不能直接复制,鼠标右键没有反应,经简单查看,可以发现限制这些功能的代码大致如下:
document.body.οncοntextmenu=function(){return false;};
document.body.οndragstart=function(){return false;};
document.body.onselectstart=function(){return false;};
document.body.onbeforecopy=function(){return false;};
document.body.οnselect=function(){document.selection.empty();};
document.body.οncοpy=function(){document.selection.empty();};
其关键性的代码为:
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链接给去掉,但是这样做根本不现实:
同样由于以上的原理2,同样不能奏效;
三、开发Chrome插件+API HOOK实现
插件制作流程:此方法看来是最有效的了;
代码:
manifest.json
{
"name": "UBC",
"manifest_version": 2,
"version": "1.0.0",
"description": "UnBlock Copy",
"browser_action":
{
"default_icon": "icon.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
//UnHook Copy Related Events
var arr_hook_event = ["oncontextmenu","ondragstart","onselectstart","onselect","onbeforecopy","oncopy"];
function Array_indexOf(arr,item)
{
for(var i = 0;i
最后一个问题:
安装的插件由于不是Google WebStore下载安装的,关闭浏览器下次打开之后,插件被Chrome自动禁用,解决办法:
1.每次手动删除再拖进去安装(麻烦!不可取!)
2.申请Google开发者认证,上传自己的插件到Google WebStore(不想用!要付接近40大洋!付费还很麻烦!)
3.为何不每次启动Chrome让它自动加载一次?
C:\Users\XXX>"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --load-extension=E:\Chrome插件开发\UBC\SRC
再进一步,创建一个Chrome的快捷方式到桌面,修改:属性->目标:C:\Users\XXX>"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --load-extension=E:\Chrome插件开发\UBC\SRC