1. 程式人生 > 實用技巧 >在別的地方看的,還沒研究

在別的地方看的,還沒研究

前端:遮蔽F12審查元素,禁止修改頁面程式碼

2018-05-28 13:58:185816收藏3 文章標籤:HTML前端 眾所周知,審查元素的情況下,大家都可以隨機更改一部分頁面的程式碼,

注入惡意JS等等,這種情況避免也不難,雖然還能看到一部分H5原始碼,但是無法修改

一、遮蔽F12 審查元素

document.onkeydown = function(){

if(window.event && window.event.keyCode == 123) {
alert("F12被禁用");
event.keyCode=0;
event.returnValue=false;
}
if(window.event && window.event.keyCode == 13) {
window.event.keyCode = 505;
}
if(window.event && window.event.keyCode == 8) {
alert(str+"\n請使用Del鍵進行字元的刪除操作!");
window.event.returnValue=false;
}

}

除了遮蔽這個,我們還有其他有趣的設定:


二、遮蔽右鍵選單
document.oncontextmenu = function (event){
if(window.event){
event = window.event;
}try{
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
return false;
}
return true;
}catch (e){
return false;
}
}

三、遮蔽貼上
document.onpaste = function (event){

if(window.event){
event = window.event;
}try{
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
return false;
}
return true;
}catch (e){
return false;
}
}

四、遮蔽複製
document.oncopy = function (event){
if(window.event){
event = window.event;
}try{
var the = event.srcElement;
if(!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
return false;
}
return true;
}catch (e){
return false;
}
}

五、遮蔽剪下
document.oncut = function (event){
if(window.event){
event = window.event;
}try{
var the = event.srcElement;
if(!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
return false;
}
return true;
}catch (e){
return false;
}
}

這種很適合小說網站,畢竟版權珍貴,被別人隨意copy走內容就不好了

六、遮蔽選中
document.onselectstart = function (event){
if(window.event){
event = window.event;
}try{
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
return false;
}
return true;
} catch (e) {
return false;
}
}
======================評論裡的==
  1. 'use strict';
  2. (function() {
  3. function R(a) {
  4. let ona = "on" + a;
  5. if (window.addEventListener){
  6. window.addEventListener(a, function(e) {
  7. for (let n = e.originalTarget; n; n = n.parentNode) n[ona] = null;
  8. }, true);
  9. }
  10. window[ona] = null;
  11. document[ona] = null;
  12. if (document.body) document.body[ona] = null;
  13. }
  14. R("contextmenu");
  15. R("click");
  16. R("mousedown");
  17. R("mouseup");
  18. R("paste");
  19. R("cut");
  20. R("copy");
  21. R("selectstart");
  22. })()