浏览器防F12审查
法1 插件
f12.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>测试F12</title>
</head>
<body>
<script src="./console-ban.min.js"></script>
<script>
ConsoleBan.init({
redirect: "about:blank",
});
</script>
<div>123</div>
</body>
</html>
console-ban.min.js
/*!
* console-ban v4.1.0
* (c) 2020-2022 fz6m
* Released under the MIT License.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ConsoleBan={})}(this,(function(e){"use strict";var t=function(){return t=Object.assign||function(e){for(var t,i=1,n=arguments.length;i<n;i++)for(var o in t=arguments[i])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},t.apply(this,arguments)},i={clear:!0,debug:!0,debugTime:3e3},n=2,o=function(e){return~navigator.userAgent.toLowerCase().indexOf(e)},r=function(e,t){t!==n?location.href=e:location.replace(e)},c=0,a=0,f=function(e){var t=0,i=1<<c++;return function(){(!a||a&i)&&2===++t&&(a|=i,e(),t=1)}},l=function(e){var t=/./;t.toString=f(e);var i=function(){return t};i.toString=f(e);var n=new Date;n.toString=f(e),console.log("%c",i,i(),n);var o,r,c=f(e);o=c,r=new Error,Object.defineProperty(r,"message",{get:function(){o()}}),console.log(r)},u=function(){function e(e){var n=t(t({},i),e),o=n.clear,r=n.debug,c=n.debugTime,a=n.callback,f=n.redirect,l=n.write;this._debug=r,this._debugTime=c,this._clear=o,this._callback=a,this._redirect=f,this._write=l}return e.prototype.clear=function(){this._clear&&(console.clear=function(){})},e.prototype.debug=function(){if(this._debug){var e=new Function("debugger");setInterval(e,this._debugTime)}},e.prototype.redirect=function(e){var t=this._redirect;if(t)if(0!==t.indexOf("http")){var i,n=location.pathname+location.search;if(((i=t)?"/"!==i[0]?"/".concat(i):i:"/")!==n)r(t,e)}else location.href!==t&&r(t,e)},e.prototype.callback=function(){if((this._callback||this._redirect||this._write)&&window){var e,t=this.fire.bind(this),i=window.chrome||o("chrome"),r=o("firefox");if(!i)return r?((e=/./).toString=t,void console.log(e)):void function(e){var t=new Image;Object.defineProperty(t,"id",{get:function(){e(n)}}),console.log(t)}(t);l(t)}},e.prototype.write=function(){var e=this._write;e&&(document.body.innerHTML="string"==typeof e?e:e.innerHTML)},e.prototype.fire=function(e){this._callback?this._callback.call(null):(this.redirect(e),this._redirect||this.write())},e.prototype.ban=function(){this.callback(),this.clear(),this.debug()},e}();e.init=function(e){new u(e).ban()},Object.defineProperty(e,"__esModule",{value:!0})}));
法2 封装的函数
// 防审查函数
/**
* 1、setDebugger 设置debugger 默认启用
* 2、setForbidF12 禁用F12 默认启用
* 3、setForbidRightClick 禁用右键
* 4、setReloadTimes 点击右键,1000s发现视图发生变化 默认启用
*/
Vue.prototype.forbidCheckHandle = ({
setDebugger = true,
setForbidF12 = true,
setForbidRightClick = false,
setRightClickAndWindowOnresize = true
}) => {
// 非正式环境不触发
if (win.environment === 'prod') return
var event = event || window.event //兼容ie低版本的
// 设置debugger
if (setDebugger) {
setTimeout(function b() {
debugger
setTimeout(b, 500)
}, 500)
}
// 禁用F12
if (setForbidF12) {
document.onkeydown = function () {
if (window.event && window.event.keyCode == 123) {
return false
}
}
}
// 禁用右键
if (setForbidRightClick) {
document.oncontextmenu = function () {
return false
}
}
// 点击右键,且发现视图发生变化
if (setRightClickAndWindowOnresize) {
document.oncontextmenu = function (event) {
//鼠标右键
if (event.button == 2) {
let h = window.innerHeight,
w = window.innerWidth
window.onresize = function () {
// 视图变化
if (h != window.innerHeight || w != window.innerWidth) {
window.close()
window.location = 'about:blank'
}
}
}
}
}
}