防止网站图片被盗用:禁用右键另存为、F12,给网站加上了全套前端防复制措施

发布时间:2026/7/9 11:45:38
防止网站图片被盗用:禁用右键另存为、F12,给网站加上了全套前端防复制措施 禁用右键菜单右键无法弹出图片另存为“复制等选项禁止选中文字鼠标拖选文字无效无法复制禁止复制/剪切CtrlC / CtrlX 失效输入框内仍可用禁止拖拽图片图片不能拖到地址栏或桌面另存屏蔽快捷键F12、CtrlShiftI/J/C、CtrlU查看源码、CtrlS保存网页全部拦截开发者工具检测检测到控制台打开时页面内容自动清空并提示请尊重版权”(function(){use strict;// 本地开发白名单这些环境下全部防护不生效 varhostlocation.hostname.toLowerCase();varisLocal(hostlocalhost)||(host127.0.0.1)||(host.indexOf(192.168.)0)||(host.indexOf(10.)0)||(hosttianhong.a.top);// 本地测试域名上线前可删除此行if(isLocal){// 本地环境直接退出不做任何拦截return;}// ---------- 1. 禁用右键菜单防止图片另存为复制等 ----------document.oncontextmenufunction(){returnfalse;};// ---------- 2. 禁止选中文字防止复制 ----------// 通过 onselectstart oncopy oncut 三重保险document.onselectstartfunction(){returnfalse;};document.oncopyfunction(){returnfalse;};document.oncutfunction(){returnfalse;};// ---------- 3. 禁止拖拽图片防止直接把图片拖到地址栏或桌面另存 ----------varimgsdocument.getElementsByTagName(img);for(vari0;iimgs.length;i){imgs[i].oncontextmenufunction(){returnfalse;};imgs[i].ondragstartfunction(){returnfalse;};}// 对后续动态加载的图片也生效MutationObserverif(window.MutationObserver){varmonewMutationObserver(function(mutations){mutations.forEach(function(m){m.addedNodes.forEach(function(node){if(node.nodeType1){if(node.tagNameIMG){node.oncontextmenufunction(){returnfalse;};node.ondragstartfunction(){returnfalse;};}varsubnode.getElementsByTagName?node.getElementsByTagName(img):[];for(vark0;ksub.length;k){sub[k].oncontextmenufunction(){returnfalse;};sub[k].ondragstartfunction(){returnfalse;};}}});});});mo.observe(document.documentElement,{childList:true,subtree:true});}// ---------- 4. 屏蔽常用开发者工具快捷键 ----------// F12 / CtrlShiftI / CtrlShiftJ / CtrlShiftC / CtrlU(查看源码)document.onkeydownfunction(e){ee||window.event;varke.keyCode||e.which;// F12if(k123){e.preventDefault();returnfalse;}// CtrlShiftI / CtrlShiftJ / CtrlShiftCif(e.ctrlKeye.shiftKey(k73||k74||k67)){e.preventDefault();returnfalse;}// CtrlU (查看源代码)if(e.ctrlKeyk85){e.preventDefault();returnfalse;}// CtrlS (保存网页)if(e.ctrlKeyk83){e.preventDefault();returnfalse;}// CtrlC / CtrlX 在非输入框内禁用vartag(e.targete.target.tagName)?e.target.tagName.toUpperCase():;varisInput(tagINPUT||tagTEXTAREA);if(!isInpute.ctrlKey(k67||k88)){e.preventDefault();returnfalse;}};// ---------- 5. 检测到开发者工具打开时清空页面可选较激进 ----------// 通过窗口尺寸差判断// 说明这部分会让页面在控制台打开时变空白如果觉得太激进可删除。vardevtoolsOpenfalse;varthreshold160;// 超过 160px 认为开了 devtoolsfunctioncheckDevTools(){varwidthDiffwindow.outerWidth-window.innerWidth;varheightDiffwindow.outerHeight-window.innerHeight;if(widthDiffthreshold||heightDiffthreshold){if(!devtoolsOpen){devtoolsOpentrue;document.body.innerHTMLdiv styletext-align:center;padding:80px 20px;font-size:18px;color:#888;请尊重本站版权请勿调试或复制本站内容。/div;}}else{devtoolsOpenfalse;}}setInterval(checkDevTools,1000);})();图片防盗链在 .htaccess 或 Nginx 配置里限制 Referer防止图片被外站直接引用图片加水印上传时自动打上公司 logo 水印敏感内容登录可见重要资料要求登录后才能查看如果有技术上问题的不懂得可以私聊交流