新建js文件debug-vconsole.js
/**防止非法调试*/
(function () {
function getLocationHrefParams(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
(function () {
var notdebugger = !getLocationHrefParams("notdebugger"); //有notdebugger标识可以调试
var origin = location.origin.indexOf("8080") != -1 && location.origin.indexOf("test") == -1;// 8080下且非test环境,可以调试
if (notdebugger && origin) {
(() => {
function block() {
if (window.outerHeight - window.innerHeight > 200 || window.outerWidth - window.innerWidth > 200) {
document.body.innerHTML = "非法调试,请关闭调试后重新刷新页面";
}
setInterval(() => {
(function () {
return false;
}
['constructor']('debugger')
['call']());
}, 50);
}
try {
block();
} catch (err) { }
})();
}
})();
// 添加vconsole
var script = document.createElement('script');
// 设置<script>标签的src属性
script.setAttribute('src', 'https://cdn.bootcdn.net/ajax/libs/vConsole/3.9.0/vconsole.min.js');
// 将<script>标签插入到页面的<head>或<body>中
document.head.appendChild(script);
var getURLParameters = function () {
var url = location.href
return (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
(a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),
{}
);
}
if (getURLParameters().vconsole) {
window.addEventListener("load", function () {
new VConsole();
});
}
})();
项目引用:
<!--文件内的代码会自动执行-->
<script src="./debug-vconsole.js"></script>
效果如下请,没有添加notdebugger
添加浏览器地址栏添加notdebugger=1,
可以打开调试页面,并且展示vconsole,去