diff --git a/src/detector/date-to-string.js b/src/detector/date-to-string.js index c7be80c..def0dc9 100644 --- a/src/detector/date-to-string.js +++ b/src/detector/date-to-string.js @@ -2,7 +2,7 @@ * @Author: tackchen * @Date: 2021-11-15 22:26:57 * @LastEditors: tackchen - * @LastEditTime: 2021-11-15 23:48:53 + * @LastEditTime: 2022-01-05 09:02:19 * @FilePath: /disable-devtool/src/detector/date-to-string.js * @Description: Coding something */ @@ -11,7 +11,8 @@ import {registInterval} from '../utils/interval'; import {DETECTOR_TYPE, triggerOnDevOpen} from './detector'; import {clearLog, log} from '../utils/log'; -export default function detector () { +export default function detector (isTrueIOSChrome) { + if (isTrueIOSChrome) return; let count = 0; const date = new Date(); date.toString = () => { diff --git a/src/detector/debugger.js b/src/detector/debugger.js index 3116586..ff39fd5 100644 --- a/src/detector/debugger.js +++ b/src/detector/debugger.js @@ -2,7 +2,7 @@ * @Author: tackchen * @Date: 2021-11-15 22:26:57 * @LastEditors: tackchen - * @LastEditTime: 2021-11-15 23:49:00 + * @LastEditTime: 2022-01-05 09:06:30 * @FilePath: /disable-devtool/src/detector/debugger.js * @Description: Coding something */ @@ -10,12 +10,15 @@ import {registInterval} from '../utils/interval'; import {DETECTOR_TYPE, triggerOnDevOpen} from './detector'; -export default function detector () { - registInterval(() => { - const date = Date.now(); - (() => {debugger;})(); - if (Date.now() - date > 100) { - triggerOnDevOpen(DETECTOR_TYPE.DEBUGGER); - } - }); +export default function detector (isTrueIOSChrome) { + if (isTrueIOSChrome) { + // 仅在 ios chrome 下生效 + registInterval(() => { + const date = Date.now(); + (() => {debugger;})(); + if (Date.now() - date > 100) { + triggerOnDevOpen(DETECTOR_TYPE.DEBUGGER); + } + }); + } } \ No newline at end of file diff --git a/src/detector/detector.js b/src/detector/detector.js index e64c265..325387f 100644 --- a/src/detector/detector.js +++ b/src/detector/detector.js @@ -2,7 +2,7 @@ * @Author: theajack * @Date: 2021-07-24 23:16:34 * @LastEditor: theajack - * @LastEditTime: 2021-12-24 15:17:10 + * @LastEditTime: 2022-01-05 12:38:04 * @Description: Coding something */ @@ -12,12 +12,11 @@ import DefineIdDetector from './define-id'; import SizeDetector from './size'; import DateToStringDetector from './date-to-string'; import FuncToStringDetector from './func-to-string'; -// import DebuggerDetector from './debugger'; // 会debuger显示devtool -// import LogTimeDetector from './log-time'; // 不准确 容易误伤 +import DebuggerDetector from './debugger'; +// import LogTimeDetector from './log-time'; // 不准确 容易误伤 故弃用 import {clearDDInterval, clearDDTimeout} from '../utils/interval'; import {closeWindow} from '../utils/close-window'; - -const detectorList = []; +import {isIOSChrome, isLogRegExpCount3} from '../utils/util'; export const DETECTOR_TYPE = { UNKONW: -1, @@ -37,19 +36,23 @@ const Detectors = { [DETECTOR_TYPE.DATE_TO_STRING]: DateToStringDetector, [DETECTOR_TYPE.FUNC_TO_STRING]: FuncToStringDetector, // [DETECTOR_TYPE.DEBUGGER]: DebuggerDetector, + // [DETECTOR_TYPE.LOG_TIME]: LogTimeDetector, }; -export function registDetector (detector) { - detectorList.push(detector); -} - -export function initDetectors () { +export async function initDetectors () { + // ! 判断是否是 ios chrome 真机, true时 禁用 date 和 func detector,因为会误伤。启用debugger detector兜底 + const isTrueIOSChrome = isIOSChrome && (await isLogRegExpCount3()); + const typeArray = config.detectors === 'all' ? Object.keys(Detectors) : config.detectors; + if (isTrueIOSChrome) { + typeArray.push(DebuggerDetector); // 会debuger显示devtool, 仅在ios chrome 真机生效 + } + typeArray.forEach(type => { if (Detectors[type]) { - Detectors[type](); + Detectors[type](isTrueIOSChrome); } }); } diff --git a/src/detector/func-to-string.js b/src/detector/func-to-string.js index 465b3af..fa352fd 100644 --- a/src/detector/func-to-string.js +++ b/src/detector/func-to-string.js @@ -2,16 +2,19 @@ * @Author: tackchen * @Date: 2021-11-15 22:26:57 * @LastEditors: tackchen - * @LastEditTime: 2021-11-15 23:49:07 + * @LastEditTime: 2022-01-05 08:08:02 * @FilePath: /disable-devtool/src/detector/func-to-string.js * @Description: Coding something */ +// // ! 会误伤ios mobile chrome 可能会误伤谷歌搜索seo 故放弃使用 + import {registInterval} from '../utils/interval'; import {DETECTOR_TYPE, triggerOnDevOpen} from './detector'; import {log, clearLog} from '../utils/log'; -export default function detector () { +export default function detector (isTrueIOSChrome) { + if (isTrueIOSChrome) return; let count = 0; const func = () => {}; func.toString = () => { diff --git a/src/utils/util.js b/src/utils/util.js index 7ec346d..2ddd6a3 100644 --- a/src/utils/util.js +++ b/src/utils/util.js @@ -1,3 +1,5 @@ +import {log} from './log'; + export function isPC () { return !/(iphone|ipad|ipod|ios|android)/i.test(navigator.userAgent.toLowerCase()); } @@ -103,4 +105,27 @@ export const isMacOs = hasUaName('macintosh'); export const isOldEdge = hasUaName('edge') && !hasUaName('chrome'); -export const isIE = isOldEdge || hasUaName('trident') || hasUaName('msie'); \ No newline at end of file +export const isIE = isOldEdge || hasUaName('trident') || hasUaName('msie'); + +export const isIOSChrome = hasUaName('crios'); + +// ios chrome log regExp count=3 , 以此区别真机和开发者工具模拟的 +export async function isLogRegExpCount3 () { + let count = 0; + const target = new RegExp(); + target.toString = () => { + count ++; + return ''; + }; + log(target); + await delay(100); + return count === 3; +} + +export function delay (time = 1000) { + return new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, time); + }); +} \ No newline at end of file