今天在看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();};
其關鍵性的代碼為:
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可能使得網頁根本就無法正常顯示 二、禁用浏覽器的Javascript的執行同樣由於以上的原理2,同樣不能奏效;
三、開發Chrome插件+API HOOK實現
插件制作流程: Chrome地址欄輸入:chrome://extensions/ 打開擴展程序窗口;勾選開發者選項;選擇之前制作好的插件的目錄在代碼調試結束,確定發布之後,可以點擊上圖中的打包擴展程序進行打包;將生成的.crx文件拖進浏覽器安裝即可;此方法看來是最有效的了;
<喎?http://www.Bkjia.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.aspphp.online/bianchen/UploadFiles_4619/201701/2017012114020368.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","onbeforecopy","oncopy"]; function Array_indexOf(arr,item) { for(var i = 0;i
測試效果,兩個網站已經可以選擇內容,並且可以復制: