From 04d2a4ff87c6298113461358b4dbaca3c00fb3a7 Mon Sep 17 00:00:00 2001 From: theajack <1506282385@qq.com> Date: Sun, 25 Oct 2020 17:14:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=200.0.3,=E4=BF=AE=E5=A4=8Dalert=E7=AD=89?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=92=8C=E5=88=87=E5=90=8E=E5=8F=B0=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84bug=EF=BC=8C=E5=85=BC=E5=AE=B9IE=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .babelrc | 3 +- .eslintignore | 3 +- README.cn.md | 8 +++- README.md | 8 +++- helper/mod-minjs.js | 13 ++++++ helper/testcdn.html | 15 +++++++ helper/util.js | 7 +++ helper/version.md | 21 +++++++++ npm/README.cn.md | 8 +++- npm/README.md | 8 +++- npm/disable-devtool.min.js | 2 +- npm/index.d.ts | 15 ++++--- npm/package.json | 4 +- package.json | 4 +- public/main.js | 7 +-- src/config.js | 5 ++- src/index.d.ts | 15 ++++--- src/interval.js | 12 ++++- src/key-menu.js | 15 +++++-- src/main.js | 23 +++++----- src/util.js | 92 ++++++++++++++++++++++++++++++++++++-- 21 files changed, 233 insertions(+), 55 deletions(-) create mode 100644 helper/mod-minjs.js create mode 100644 helper/testcdn.html create mode 100644 helper/version.md diff --git a/.babelrc b/.babelrc index 10c0a41..89d422c 100644 --- a/.babelrc +++ b/.babelrc @@ -4,7 +4,8 @@ { "useBuiltIns": "entry", "targets": { - "esmodules": true + "esmodules": true, + "ie": 11 } } ]], diff --git a/.eslintignore b/.eslintignore index 827029e..79c2edd 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,5 @@ /public/bundle.js /public/index.html /npm -/cdn \ No newline at end of file +/cdn +**/*.html \ No newline at end of file diff --git a/README.cn.md b/README.cn.md index 804d647..66c9916 100644 --- a/README.cn.md +++ b/README.cn.md @@ -20,7 +20,7 @@ Dependent

-**[English](https://github.com/theajack/disable-devtool/blob/master/helper/README.en.md) | 在线试用/文档 | [Gitee](https://gitee.com/theajack/disable-devtool)** +**[English](https://github.com/theajack/disable-devtool/blob/master/README.md) | [在线试用/文档](https://theajack.gitee.io/disable-devtool) | [更新日志](https://github.com/theajack/disable-devtool/blob/master/helper/version.md) | [Gitee](https://gitee.com/theajack/disable-devtool)** ## 1. 快速使用 @@ -52,10 +52,11 @@ disable-devtool 可以禁用所有一切可以进入开发者工具的方法, 2. 禁用 f12 和 ctrl+shift+i 快捷键 3. 支持识别从浏览器菜单栏打开开发者工具并关闭当前页面 4. 开发者可以绕过禁用 (url参数使用tk配合md5加密) -5. 支持几乎所有浏览器 +5. 支持几乎所有浏览器(包含IE) 6. 高度可配置 7. 使用极简、体积小巧 (仅6kb) 8. 支持npm引用和script标签引用(属性配置) +9. 识别真移动端与浏览器开发者工具设置插件伪造的移动端,为移动端节省性能 ## 3. 使用 @@ -84,9 +85,12 @@ declare interface optionStatic { debugDelay?: number; // debug模式时的延迟 默认200ms interval?: number; // 定时器的时间间隔 默认200ms disableMenu?: boolean; // 是否禁用右键菜单 默认为true + stopIntervalTime?: number; // 在移动端时取消监视的等待时长 } ``` +备注:disableMenu参数在ie下无效,因为ie下右键会阻塞主进程,且无法监听 + ### 3.2 md5 与 tk 绕过禁用 该库中使用 key 与 md5 配合的方式使得开发者可以在线上绕过禁用。 diff --git a/README.md b/README.md index de4c1bd..9ca3767 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Dependent

-**[中文](https://github.com/theajack/disable-devtool/blob/master/README.cn.md) | online trial/document | [Gitee](https://gitee.com/theajack/disable-devtool)** +**[中文](https://github.com/theajack/disable-devtool/blob/master/README.cn.md) | [online trial/document](https://theajack.gitee.io/disable-devtool) | [Version Log](https://github.com/theajack/disable-devtool/blob/master/helper/version.md) | [Gitee](https://gitee.com/theajack/disable-devtool)** ## 1. Quick use @@ -51,10 +51,11 @@ The library has the following features: 2. Disable f12 and ctrl+shift+i shortcuts 3. Support recognition to open the developer tools from the browser menu bar and close the current page 4. Developers can bypass the disablement (use tk and md5 encryption for url parameters) -5. Support almost all browsers +5. Support almost all browsers (Include IE) 6. Highly configurable 7. Minimal use, small size (only 6kb) 8. Support npm reference and script tag reference (attribute configuration) +9. Identify the real mobile terminal and browser developer tool settings plug-in forged mobile terminal, saving performance for the mobile terminal ## 3. Use @@ -83,9 +84,12 @@ declare interface optionStatic { debugDelay?: number; // The delay in debug mode is 200ms by default interval?: number; // Timer interval is 200ms by default disableMenu?: boolean; // Whether to disable the right-click menu The default is true + stopIntervalTime?: number; // Waiting time to cancel monitoring on mobile } ``` +Note: The disableMenu parameter is invalid under ie, because the right button under ie will block the main process and cannot monitor + ### 3.2 md5 and tk bypass disable The combination of key and md5 in the library allows developers to bypass the disabling online. diff --git a/helper/mod-minjs.js b/helper/mod-minjs.js new file mode 100644 index 0000000..1536a06 --- /dev/null +++ b/helper/mod-minjs.js @@ -0,0 +1,13 @@ + +const util = require('./util'); + +function main () { + var file = 'npm/disable-devtool.min.js'; + util.read(file, (code) => { + util.write(file, code.replace(/[a-z]\){/i, (str) => { + let n = str[0]; + return `${str}var _f=${n};${n}=function(){return _f().default};`; + })); + }); +} +main(); \ No newline at end of file diff --git a/helper/testcdn.html b/helper/testcdn.html new file mode 100644 index 0000000..6975330 --- /dev/null +++ b/helper/testcdn.html @@ -0,0 +1,15 @@ + + + + + + + Disable Devtool Demo + + + + + \ No newline at end of file diff --git a/helper/util.js b/helper/util.js index 344890f..7ae2bb6 100644 --- a/helper/util.js +++ b/helper/util.js @@ -1,6 +1,13 @@ let fs = require('fs'); module.exports = { + read: function (file, cb) { + fs.readFile(file, 'utf8', (err, code) => { + if (err) throw err; + cb(code); + }); + }, + write: function (file, txt, cb) { fs.writeFile(file, txt, 'utf8', (err) => { if (err) throw err; diff --git a/helper/version.md b/helper/version.md new file mode 100644 index 0000000..db9ba71 --- /dev/null +++ b/helper/version.md @@ -0,0 +1,21 @@ +# Version Log: + +## 0.0.1 +1. 支持可配置是否禁用右键菜单 +2. 禁用 f12 和 ctrl+shift+i 快捷键 +3. 支持识别从浏览器菜单栏打开开发者工具并关闭当前页面 +4. 开发者可以绕过禁用 (url参数使用tk配合md5加密) +5. 支持几乎所有浏览器 +6. 高度可配置 +7. 使用极简、体积小巧 (仅6kb) +8. 支持npm引用和script标签引用(属性配置) + +## 0.0.2 +1. 解决cdn文件无效的bug + +## 0.0.3 +1. 解决alert等原生方法会影响debug计时导致 +2. 解决页面且后台会影响debug计时导致 +3. 兼容ie,disableMenu参数在ie下无效,因为ie下右键会阻塞主进程,且无法监听 +4. 增加config.stopIntervalTime 表示在移动端时取消监视的等待时长 +5. 优化判断开发者工具打开的逻辑 diff --git a/npm/README.cn.md b/npm/README.cn.md index 0a7a2ed..66c9916 100644 --- a/npm/README.cn.md +++ b/npm/README.cn.md @@ -20,7 +20,7 @@ Dependent

-**[English](https://github.com/theajack/disable-devtool/blob/master/helper/README.en.md) | [在线试用/文档](https://theajack.gitee.io/disable-devtool) | [Gitee](https://gitee.com/theajack/disable-devtool)** +**[English](https://github.com/theajack/disable-devtool/blob/master/README.md) | [在线试用/文档](https://theajack.gitee.io/disable-devtool) | [更新日志](https://github.com/theajack/disable-devtool/blob/master/helper/version.md) | [Gitee](https://gitee.com/theajack/disable-devtool)** ## 1. 快速使用 @@ -52,10 +52,11 @@ disable-devtool 可以禁用所有一切可以进入开发者工具的方法, 2. 禁用 f12 和 ctrl+shift+i 快捷键 3. 支持识别从浏览器菜单栏打开开发者工具并关闭当前页面 4. 开发者可以绕过禁用 (url参数使用tk配合md5加密) -5. 支持几乎所有浏览器 +5. 支持几乎所有浏览器(包含IE) 6. 高度可配置 7. 使用极简、体积小巧 (仅6kb) 8. 支持npm引用和script标签引用(属性配置) +9. 识别真移动端与浏览器开发者工具设置插件伪造的移动端,为移动端节省性能 ## 3. 使用 @@ -84,9 +85,12 @@ declare interface optionStatic { debugDelay?: number; // debug模式时的延迟 默认200ms interval?: number; // 定时器的时间间隔 默认200ms disableMenu?: boolean; // 是否禁用右键菜单 默认为true + stopIntervalTime?: number; // 在移动端时取消监视的等待时长 } ``` +备注:disableMenu参数在ie下无效,因为ie下右键会阻塞主进程,且无法监听 + ### 3.2 md5 与 tk 绕过禁用 该库中使用 key 与 md5 配合的方式使得开发者可以在线上绕过禁用。 diff --git a/npm/README.md b/npm/README.md index b07c7b8..9ca3767 100644 --- a/npm/README.md +++ b/npm/README.md @@ -19,7 +19,7 @@ Dependent

-**[中文](https://github.com/theajack/disable-devtool/blob/master/README.cn.md) | [online trial/document](https://theajack.gitee.io/disable-devtool) | [Gitee](https://gitee.com/theajack/disable-devtool)** +**[中文](https://github.com/theajack/disable-devtool/blob/master/README.cn.md) | [online trial/document](https://theajack.gitee.io/disable-devtool) | [Version Log](https://github.com/theajack/disable-devtool/blob/master/helper/version.md) | [Gitee](https://gitee.com/theajack/disable-devtool)** ## 1. Quick use @@ -51,10 +51,11 @@ The library has the following features: 2. Disable f12 and ctrl+shift+i shortcuts 3. Support recognition to open the developer tools from the browser menu bar and close the current page 4. Developers can bypass the disablement (use tk and md5 encryption for url parameters) -5. Support almost all browsers +5. Support almost all browsers (Include IE) 6. Highly configurable 7. Minimal use, small size (only 6kb) 8. Support npm reference and script tag reference (attribute configuration) +9. Identify the real mobile terminal and browser developer tool settings plug-in forged mobile terminal, saving performance for the mobile terminal ## 3. Use @@ -83,9 +84,12 @@ declare interface optionStatic { debugDelay?: number; // The delay in debug mode is 200ms by default interval?: number; // Timer interval is 200ms by default disableMenu?: boolean; // Whether to disable the right-click menu The default is true + stopIntervalTime?: number; // Waiting time to cancel monitoring on mobile } ``` +Note: The disableMenu parameter is invalid under ie, because the right button under ie will block the main process and cannot monitor + ### 3.2 md5 and tk bypass disable The combination of key and md5 in the library allows developers to bypass the disabling online. diff --git a/npm/disable-devtool.min.js b/npm/disable-devtool.min.js index acaab79..f1d1bd4 100644 --- a/npm/disable-devtool.min.js +++ b/npm/disable-devtool.min.js @@ -1 +1 @@ -!function(e,n){var _ = n;n=function(){return _().default};"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define("DisableDevtool",[],n):"object"==typeof exports?exports.DisableDevtool=n():e.DisableDevtool=n()}(this,(function(){return function(e){var n={};function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:r})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,n){if(1&n&&(e=t(e)),8&n)return e;if(4&n&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(t.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&n&&"string"!=typeof e)for(var o in e)t.d(r,o,function(n){return e[n]}.bind(null,o));return r},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},t.p="",t(t.s=0)}([function(e,n,t){"use strict";function r(){return(new Date).getTime()}t.r(n);var o={md5:"",ondevtoolopen:function(){window.opener=null,window.open("","_self"),window.close(),window.location.href=o.url},url:"http://localhost",tkName:"ddtk",debugDelay:200,interval:200,disableMenu:!0};var u=null,i=null,a=[];function l(){u=window.setInterval(()=>{a.forEach(e=>{e()})},o.interval),i=setTimeout(()=>{/(iphone|ipad|ipod|ios|android)/i.test(navigator.userAgent.toLowerCase())&&window.clearInterval(u)},2e3)}function f(e){a.push(e)}function c(e,n,t,r,o,u){return b((i=b(b(n,e),b(r,u)))<<(a=o)|i>>>32-a,t);var i,a}function d(e,n,t,r,o,u,i){return c(n&t|~n&r,e,n,o,u,i)}function s(e,n,t,r,o,u,i){return c(n&r|t&~r,e,n,o,u,i)}function v(e,n,t,r,o,u,i){return c(n^t^r,e,n,o,u,i)}function p(e,n,t,r,o,u,i){return c(t^(n|~r),e,n,o,u,i)}function b(e,n){var t=(65535&e)+(65535&n);return(e>>16)+(n>>16)+(t>>16)<<16|65535&t}var y=function(e){return function(e){for(var n="0123456789abcdef",t="",r=0;r<4*e.length;r++)t+=n.charAt(e[r>>2]>>r%4*8+4&15)+n.charAt(e[r>>2]>>r%4*8&15);return t}(function(e,n){e[n>>5]|=128<>>9<<4)]=n;for(var t=1732584193,r=-271733879,o=-1732584194,u=271733878,i=0;i>5]|=(255&e.charCodeAt(t/8))<0&&void 0!==arguments[0]?arguments[0]:{};for(var n in o)e[n]&&typeof o[n]==typeof e[n]&&(o[n]=e[n])}(e),function(){if(o.md5){var e=function(e){var n=window.location.search;if(""!==n){var t=new RegExp("(^|&)"+e+"=([^&]*)(&|$)","i"),r=n.substr(1).match(t);if(null!=r)return unescape(r[2])}return""}(o.tkName);if(y(e)===o.md5)return!0}return!1}())||(l(),window.addEventListener("keydown",e=>{if(123===e.keyCode||e.shiftKey&&e.ctrlKey&&73===e.keyCode)return e.returnValue=!1,!1}),o.disableMenu&&window.addEventListener("contextmenu",e=>(e.returnValue=!1,!1)),function(){var e=~navigator.userAgent.indexOf("Firefox"),n="";e?(n=/./).toString=function(){m()}:(n=new Image).__defineGetter__("id",(function(){m()}));f(()=>{console.log(n),console.clear&&console.clear()})}(),n=new Function("debugger"),t=r(),f(()=>{n();var e=r();e-t>o.interval+o.debugDelay?m():t=e}))}function m(){window.clearTimeout(i),o.ondevtoolopen()}g.md5=y,function(){var e=document.getElementById("disable-devtool");if(e){var n={};["md5","url","tk-name","debug-delay","interval","disable-menu"].forEach(t=>{var r=e.getAttribute(t);null!==r&&("debug-delay"===t||"interval"===t?r=parseInt(r):"disable-menu"===t&&(r="false"!==r),n[function(e){if(-1===e.indexOf("-"))return e;var n=!1;return e.split("").map(e=>"-"===e?(n=!0,""):n?(n=!1,e.toUpperCase()):e).join("")}(t)]=r)}),g(n)}}();n.default=g}])})); \ No newline at end of file +!function(n,e){var _f=e;e=function(){return _f().default};"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("DisableDevtool",[],e):"object"==typeof exports?exports.DisableDevtool=e():n.DisableDevtool=e()}(this,(function(){return function(n){var e={};function t(o){if(e[o])return e[o].exports;var i=e[o]={i:o,l:!1,exports:{}};return n[o].call(i.exports,i,i.exports,t),i.l=!0,i.exports}return t.m=n,t.c=e,t.d=function(n,e,o){t.o(n,e)||Object.defineProperty(n,e,{enumerable:!0,get:o})},t.r=function(n){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(n,"__esModule",{value:!0})},t.t=function(n,e){if(1&e&&(n=t(n)),8&e)return n;if(4&e&&"object"==typeof n&&n&&n.__esModule)return n;var o=Object.create(null);if(t.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:n}),2&e&&"string"!=typeof n)for(var i in n)t.d(o,i,function(e){return n[e]}.bind(null,i));return o},t.n=function(n){var e=n&&n.__esModule?function(){return n.default}:function(){return n};return t.d(e,"a",e),e},t.o=function(n,e){return Object.prototype.hasOwnProperty.call(n,e)},t.p="",t(t.s=0)}([function(n,e,t){"use strict";function o(){return(new Date).getTime()}function i(n){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol&&n!==Symbol.prototype?"symbol":typeof n})(n)}t.r(e);var r={md5:"",ondevtoolopen:function(){r.url?window.location.href=r.url:(window.opener=null,window.open("","_self"),window.close(),window.history.back(),setTimeout((function(){window.location.href="http://localhost"}),100))},url:"",tkName:"ddtk",debugDelay:200,interval:200,disableMenu:!0,stopIntervalTime:5e3};function u(){window.addEventListener("keydown",(function(n){if(123===((n=n||window.event).keyCode||n.which)||n.shiftKey&&n.ctrlKey&&73===n.keyCode)return n.returnValue=!1,n.preventDefault(),!1}),!1),(r.disableMenu||function(){var n=navigator.userAgent,e=n.indexOf("compatible")>-1&&n.indexOf("MSIE")>-1,t=n.indexOf("Edge")>-1&&!e,o=n.indexOf("Trident")>-1&&n.indexOf("rv:11.0")>-1;return e||t||o}())&&window.addEventListener("contextmenu",(function(n){return(n=n||window.event).returnValue=!1,n.preventDefault(),!1}),!1)}var a=null,d=null,f=[];function c(){var n,e,t,o,i,u,c=!1,l=function(){c=!0},s=function(){c=!1};n=l,e=s,t=window.alert,o=window.confirm,i=window.prompt,u=function(t){return function(){n&&n(),t.apply(void 0,arguments),e&&e()}},window.alert=u(t),window.confirm=u(o),window.prompt=u(i),function(n,e){var t,o,i;void 0!==document.hidden?(t="hidden",i="visibilitychange",o="visibilityState"):void 0!==document.mozHidden?(t="mozHidden",i="mozvisibilitychange",o="mozVisibilityState"):void 0!==document.msHidden?(t="msHidden",i="msvisibilitychange",o="msVisibilityState"):void 0!==document.webkitHidden&&(t="webkitHidden",i="webkitvisibilitychange",o="webkitVisibilityState");var r=function(){document[o]===t?e():n()};document.removeEventListener(i,r,!1),document.addEventListener(i,r,!1)}(s,l),a=window.setInterval((function(){console.log(1),c||f.forEach((function(n){n()}))}),r.interval),d=setTimeout((function(){/(iphone|ipad|ipod|ios|android)/i.test(navigator.userAgent.toLowerCase())&&window.clearInterval(a)}),r.stopIntervalTime)}function l(n){f.push(n)}function s(n,e,t,o,i,r){return w((u=w(w(e,n),w(o,r)))<<(a=i)|u>>>32-a,t);var u,a}function v(n,e,t,o,i,r,u){return s(e&t|~e&o,n,e,i,r,u)}function m(n,e,t,o,i,r,u){return s(e&o|t&~o,n,e,i,r,u)}function p(n,e,t,o,i,r,u){return s(e^t^o,n,e,i,r,u)}function b(n,e,t,o,i,r,u){return s(t^(e|~o),n,e,i,r,u)}function w(n,e){var t=(65535&n)+(65535&e);return(n>>16)+(e>>16)+(t>>16)<<16|65535&t}var y=function(n){return function(n){for(var e="0123456789abcdef",t="",o=0;o<4*n.length;o++)t+=e.charAt(n[o>>2]>>o%4*8+4&15)+e.charAt(n[o>>2]>>o%4*8&15);return t}(function(n,e){n[e>>5]|=128<>>9<<4)]=e;for(var t=1732584193,o=-271733879,i=-1732584194,r=271733878,u=0;u>5]|=(255&n.charCodeAt(t/8))<0&&void 0!==arguments[0]?arguments[0]:{};for(var e in r)n[e]&&i(r[e])===i(n[e])&&(r[e]=n[e])}(n),function(){if(r.md5){var n=function(n){var e=window.location.search;if(""!==e){var t=new RegExp("(^|&)"+n+"=([^&]*)(&|$)","i"),o=e.substr(1).match(t);if(null!=o)return unescape(o[2])}return""}(r.tkName);if(y(n)===r.md5)return!0}return!1}())||(c(),u(),function(){var n=~navigator.userAgent.indexOf("Firefox"),e="";n?(e=/./).toString=function(){x=!0,h()}:(e=new Image).__defineGetter__("id",(function(){x=!0,h()}));l((function(){console.log(e),console.clear&&console.clear()}))}(),e=new Function("debugger"),l((function(){if(!x){var n=o();e(),o()-n>r.interval+r.debugDelay&&h()}})))}function h(){window.clearTimeout(d),r.ondevtoolopen()}g.md5=y;var x=!1;!function(){var n=document.getElementById("disable-devtool");if(n){var e={};["md5","url","tk-name","debug-delay","interval","disable-menu"].forEach((function(t){var o=n.getAttribute(t);null!==o&&("debug-delay"===t||"interval"===t?o=parseInt(o):"disable-menu"===t&&(o="false"!==o),e[function(n){if(-1===n.indexOf("-"))return n;var e=!1;return n.split("").map((function(n){return"-"===n?(e=!0,""):e?(e=!1,n.toUpperCase()):n})).join("")}(t)]=o)})),g(e)}}();e.default=g}])})); \ No newline at end of file diff --git a/npm/index.d.ts b/npm/index.d.ts index 82dce1c..88f1d10 100644 --- a/npm/index.d.ts +++ b/npm/index.d.ts @@ -1,11 +1,12 @@ declare interface optionStatic { - md5?: string; - url?: string; - tkName?: string; - ondevtoolopen?(): void; // ondevtoolopen 优先级高于 url - debugDelay?: number; - interval?: number; - disableMenu?: boolean; + md5?: string; // 绕过禁用的md5值,详情见3.2,默认不启用绕过禁用 + url?: string; // 关闭页面失败时的跳转页面,默认值为localhost + tkName?: string; // 绕过禁用时的url参数名称,默认为 ddtk + ondevtoolopen?(): void; // 开发者面板打开的回调,启用时url参数无效 + debugDelay?: number; // debug模式时的延迟 默认200ms + interval?: number; // 定时器的时间间隔 默认200ms + disableMenu?: boolean; // 是否禁用右键菜单 默认为true ie下无效 + stopIntervalTime?: number; // 在移动端时取消监视的等待时长 } declare interface DDTStatic { (option?: optionStatic): void; diff --git a/npm/package.json b/npm/package.json index 2d60606..dc711f7 100644 --- a/npm/package.json +++ b/npm/package.json @@ -1,12 +1,12 @@ { "name": "disable-devtool", - "version": "0.0.2", + "version": "0.0.3", "description": "Disable web developer tools from the f12 button, right-click and browser ", "main": "disable-devtool.min.js", "scripts": { "dev": "webpack-dev-server --open --config webpack-config/dev.js", "start": "npm run dev", - "build": "webpack --config webpack-config/build.js", + "build": "webpack --config webpack-config/build.js && node helper/mod-minjs.js", "publish": "npm publish npm", "lint": "eslint src --ext js" }, diff --git a/package.json b/package.json index e56c216..eca0f28 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "disable-devtool", - "version": "0.0.1", + "version": "0.0.3", "description": "Disable web developer tools from the f12 button, right-click and browser ", "main": "disable-devtool.min.js", "unpkg": "disable-devtool.min.js", @@ -13,7 +13,7 @@ "scripts": { "dev": "webpack-dev-server --open --config webpack-config/dev.js", "start": "npm run dev", - "build": "webpack --config webpack-config/build.js", + "build": "webpack --config webpack-config/build.js && node helper/mod-minjs.js", "publish": "npm publish npm", "lint": "eslint src --ext js" }, diff --git a/public/main.js b/public/main.js index 34c8ed7..9c08d68 100644 --- a/public/main.js +++ b/public/main.js @@ -4,8 +4,9 @@ import disableDevtool from '../src'; disableDevtool({ md5: 'd4de605ccb923b7e876b3218a1474653', // url: 'https://www.qq.com', - ondevtoolopen: () => { - // window.location.href = 'https://www.qq.com'; - }, + // ondevtoolopen: () => { + // window.location.href = 'https://www.qq.com'; + // }, + interval: 1000, tkName: 'xx', }); \ No newline at end of file diff --git a/src/config.js b/src/config.js index 569190e..d3f7eff 100644 --- a/src/config.js +++ b/src/config.js @@ -3,11 +3,12 @@ import {closeWindow} from './util'; export let config = { md5: '', ondevtoolopen: closeWindow, // ondevtoolopen 优先级高于 url - url: 'http://localhost', + url: '', tkName: 'ddtk', debugDelay: 200, interval: 200, - disableMenu: true + disableMenu: true, // 该参数ie下无效,ie 右键菜单会阻塞线程影响debug延迟计算 禁用右键菜单 + stopIntervalTime: 5000, // 在移动端时取消监视的等待时长 }; export function mergeConfig (opts = {}) { diff --git a/src/index.d.ts b/src/index.d.ts index 82dce1c..88f1d10 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,11 +1,12 @@ declare interface optionStatic { - md5?: string; - url?: string; - tkName?: string; - ondevtoolopen?(): void; // ondevtoolopen 优先级高于 url - debugDelay?: number; - interval?: number; - disableMenu?: boolean; + md5?: string; // 绕过禁用的md5值,详情见3.2,默认不启用绕过禁用 + url?: string; // 关闭页面失败时的跳转页面,默认值为localhost + tkName?: string; // 绕过禁用时的url参数名称,默认为 ddtk + ondevtoolopen?(): void; // 开发者面板打开的回调,启用时url参数无效 + debugDelay?: number; // debug模式时的延迟 默认200ms + interval?: number; // 定时器的时间间隔 默认200ms + disableMenu?: boolean; // 是否禁用右键菜单 默认为true ie下无效 + stopIntervalTime?: number; // 在移动端时取消监视的等待时长 } declare interface DDTStatic { (option?: optionStatic): void; diff --git a/src/interval.js b/src/interval.js index 339d98f..0e9fb7f 100644 --- a/src/interval.js +++ b/src/interval.js @@ -1,11 +1,19 @@ import {config} from './config'; -import {isPC} from './util'; +import {hackAlert, isPC, onPageShowHide} from './util'; let interval = null, timer = null; let calls = []; export function initInterval () { + let _pause = false; + let pause = () => {_pause = true;}; + let goon = () => {_pause = false;}; + hackAlert(pause, goon); // 防止 alert等方法触发了debug延迟计算 + onPageShowHide(goon, pause); // 防止切后台触发了debug延迟计算 + interval = window.setInterval(() => { + console.log(1); + if (_pause) return; calls.forEach(fn => {fn();}); }, config.interval); // 两秒之后判断 如果不是pc去掉定时器interval,为了优化移动端的性能 @@ -14,7 +22,7 @@ export function initInterval () { if (!isPC()) { clearInterval(); } - }, 2000); + }, config.stopIntervalTime); } export function registInterval (fn) { diff --git a/src/key-menu.js b/src/key-menu.js index bc752b3..7f010e5 100644 --- a/src/key-menu.js +++ b/src/key-menu.js @@ -1,16 +1,23 @@ import {config} from './config'; +import {isIE} from './util'; export function disableKeyAndMenu () { window.addEventListener('keydown', (e) => { - if (e.keyCode === 123 || (e.shiftKey && e.ctrlKey && e.keyCode === 73)) { + e = e || window.event; + let keyCode = e.keyCode || e.which; + // alert(e.keyCode); + if (keyCode === 123 || (e.shiftKey && e.ctrlKey && e.keyCode === 73)) { e.returnValue = false; + e.preventDefault(); return false; } - }); - if (config.disableMenu) { + }, false); + if (config.disableMenu || isIE()) { // ie 右键菜单会阻塞线程影响debug延迟计算 禁用右键菜单 window.addEventListener('contextmenu', (e) => { + e = e || window.event; e.returnValue = false; + e.preventDefault(); return false; - }); + }, false); } } \ No newline at end of file diff --git a/src/main.js b/src/main.js index dc078f3..5033d68 100644 --- a/src/main.js +++ b/src/main.js @@ -6,9 +6,7 @@ import md5 from './md5'; export function disableDevtool (opts) { mergeConfig(opts); - if (checkTk()) { - return; - } + if (checkTk()) {return;} initInterval(); disableKeyAndMenu(); initDevTool(); @@ -32,17 +30,19 @@ function checkTk () { return false; } +let disableDebug = false; // 当 initDevTool 方式生效时,去除debug断点 + function initDebugger () { - let fn = new Function('debugger'); - let last = getNowTime(); + let debug = new Function('debugger'); registInterval(() => { - fn(); - let now = getNowTime(); + if (disableDebug) { + return; + } + var last = getNowTime(); + debug(); // interval 时间是 config.interval,设置config.debugDelay是为了给一个执行的时间 - if (now - last > config.interval + config.debugDelay) { + if (getNowTime() - last > config.interval + config.debugDelay) { onDevToolOpen(); - } else { - last = now; } }); } @@ -53,11 +53,13 @@ function initDevTool () { if (isFF) { toTest = /./; toTest.toString = function () { + disableDebug = true; onDevToolOpen(); }; } else { toTest = new Image(); toTest.__defineGetter__('id', function () { + disableDebug = true; onDevToolOpen(); }); } @@ -87,5 +89,4 @@ function checkScriptUse () { disableDevtool(json); } - checkScriptUse(); \ No newline at end of file diff --git a/src/util.js b/src/util.js index 58c09dc..1e8f1bb 100644 --- a/src/util.js +++ b/src/util.js @@ -6,11 +6,18 @@ export function isPC () { export function closeWindow () { // 需要是有js跳转到这个页面才可以关闭这个页面 - window.opener = null; - window.open('', '_self'); - window.close(); + if (config.url) { + window.location.href = config.url; + } else { + window.opener = null; + window.open('', '_self'); + window.close(); + window.history.back(); + setTimeout(() => { + window.location.href = 'http://localhost'; + }, 100); + } // 否则执行跳转到 url - window.location.href = config.url; } export function getNowTime () { @@ -45,4 +52,81 @@ export function formatName (name) { } return c; }).join(''); +} + + +export function onPageShowHide (onshow, onhide) { + var hidden, state, visibilityChange; + if (typeof document.hidden !== 'undefined') { + hidden = 'hidden'; + visibilityChange = 'visibilitychange'; + state = 'visibilityState'; + } else if (typeof document.mozHidden !== 'undefined') { + hidden = 'mozHidden'; + visibilityChange = 'mozvisibilitychange'; + state = 'mozVisibilityState'; + } else if (typeof document.msHidden !== 'undefined') { + hidden = 'msHidden'; + visibilityChange = 'msvisibilitychange'; + state = 'msVisibilityState'; + } else if (typeof document.webkitHidden !== 'undefined') { + hidden = 'webkitHidden'; + visibilityChange = 'webkitvisibilitychange'; + state = 'webkitVisibilityState'; + } + var cb = function () { + if (document[state] === hidden) { + onhide(); + } else { + onshow(); + } + }; + document.removeEventListener(visibilityChange, cb, false); + document.addEventListener(visibilityChange, cb, false); +} + +export function hackAlert (before, after) { + let _alert = window.alert; + let _confirm = window.confirm; + let _prompt = window.prompt; + let mod = (fn) => { + return (...args) => { + if (before) {before();} + fn(...args); + if (after) {after();} + }; + }; + window.alert = mod(_alert); + window.confirm = mod(_confirm); + window.prompt = mod(_prompt); +} + +export function isIE () { + var userAgent = navigator.userAgent; // 取得浏览器的userAgent字符串 + var isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1; // 判断是否IE<11浏览器 + var isEdge = userAgent.indexOf('Edge') > -1 && !isIE; // 判断是否IE的Edge浏览器 + var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1; + return isIE || isEdge || isIE11; + // if (isIE) { + // var reIE = new RegExp('MSIE (\\d+\\.\\d+);'); + // reIE.test(userAgent); + // var fIEVersion = parseFloat(RegExp['$1']); + // if (fIEVersion == 7) { + // return 7; + // } else if (fIEVersion == 8) { + // return 8; + // } else if (fIEVersion == 9) { + // return 9; + // } else if (fIEVersion == 10) { + // return 10; + // } else { + // return 6;// IE版本<=7 + // } + // } else if (isEdge) { + // return 'edge';// edge + // } else if (isIE11) { + // return 11; // IE11 + // } else { + // return -1;// 不是ie浏览器 + // } } \ No newline at end of file