feat: 规避ios移动端chrome误伤

This commit is contained in:
tackchen 2022-01-05 12:41:31 +08:00
parent db665cc1dc
commit 00c689e0b7
5 changed files with 60 additions and 25 deletions

View File

@ -2,7 +2,7 @@
* @Author: tackchen * @Author: tackchen
* @Date: 2021-11-15 22:26:57 * @Date: 2021-11-15 22:26:57
* @LastEditors: tackchen * @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 * @FilePath: /disable-devtool/src/detector/date-to-string.js
* @Description: Coding something * @Description: Coding something
*/ */
@ -11,7 +11,8 @@ import {registInterval} from '../utils/interval';
import {DETECTOR_TYPE, triggerOnDevOpen} from './detector'; import {DETECTOR_TYPE, triggerOnDevOpen} from './detector';
import {clearLog, log} from '../utils/log'; import {clearLog, log} from '../utils/log';
export default function detector () { export default function detector (isTrueIOSChrome) {
if (isTrueIOSChrome) return;
let count = 0; let count = 0;
const date = new Date(); const date = new Date();
date.toString = () => { date.toString = () => {

View File

@ -2,7 +2,7 @@
* @Author: tackchen * @Author: tackchen
* @Date: 2021-11-15 22:26:57 * @Date: 2021-11-15 22:26:57
* @LastEditors: tackchen * @LastEditors: tackchen
* @LastEditTime: 2021-11-15 23:49:00 * @LastEditTime: 2022-01-05 09:06:30
* @FilePath: /disable-devtool/src/detector/debugger.js * @FilePath: /disable-devtool/src/detector/debugger.js
* @Description: Coding something * @Description: Coding something
*/ */
@ -10,7 +10,9 @@
import {registInterval} from '../utils/interval'; import {registInterval} from '../utils/interval';
import {DETECTOR_TYPE, triggerOnDevOpen} from './detector'; import {DETECTOR_TYPE, triggerOnDevOpen} from './detector';
export default function detector () { export default function detector (isTrueIOSChrome) {
if (isTrueIOSChrome) {
// 仅在 ios chrome 下生效
registInterval(() => { registInterval(() => {
const date = Date.now(); const date = Date.now();
(() => {debugger;})(); (() => {debugger;})();
@ -19,3 +21,4 @@ export default function detector () {
} }
}); });
} }
}

View File

@ -2,7 +2,7 @@
* @Author: theajack * @Author: theajack
* @Date: 2021-07-24 23:16:34 * @Date: 2021-07-24 23:16:34
* @LastEditor: theajack * @LastEditor: theajack
* @LastEditTime: 2021-12-24 15:17:10 * @LastEditTime: 2022-01-05 12:38:04
* @Description: Coding something * @Description: Coding something
*/ */
@ -12,12 +12,11 @@ import DefineIdDetector from './define-id';
import SizeDetector from './size'; import SizeDetector from './size';
import DateToStringDetector from './date-to-string'; import DateToStringDetector from './date-to-string';
import FuncToStringDetector from './func-to-string'; import FuncToStringDetector from './func-to-string';
// import DebuggerDetector from './debugger'; // 会debuger显示devtool import DebuggerDetector from './debugger';
// import LogTimeDetector from './log-time'; // 不准确 容易误伤 // import LogTimeDetector from './log-time'; // 不准确 容易误伤 故弃用
import {clearDDInterval, clearDDTimeout} from '../utils/interval'; import {clearDDInterval, clearDDTimeout} from '../utils/interval';
import {closeWindow} from '../utils/close-window'; import {closeWindow} from '../utils/close-window';
import {isIOSChrome, isLogRegExpCount3} from '../utils/util';
const detectorList = [];
export const DETECTOR_TYPE = { export const DETECTOR_TYPE = {
UNKONW: -1, UNKONW: -1,
@ -37,19 +36,23 @@ const Detectors = {
[DETECTOR_TYPE.DATE_TO_STRING]: DateToStringDetector, [DETECTOR_TYPE.DATE_TO_STRING]: DateToStringDetector,
[DETECTOR_TYPE.FUNC_TO_STRING]: FuncToStringDetector, [DETECTOR_TYPE.FUNC_TO_STRING]: FuncToStringDetector,
// [DETECTOR_TYPE.DEBUGGER]: DebuggerDetector, // [DETECTOR_TYPE.DEBUGGER]: DebuggerDetector,
// [DETECTOR_TYPE.LOG_TIME]: LogTimeDetector,
}; };
export function registDetector (detector) { export async function initDetectors () {
detectorList.push(detector); // ! 判断是否是 ios chrome 真机, true时 禁用 date 和 func detector因为会误伤。启用debugger detector兜底
} const isTrueIOSChrome = isIOSChrome && (await isLogRegExpCount3());
export function initDetectors () {
const typeArray = config.detectors === 'all' ? const typeArray = config.detectors === 'all' ?
Object.keys(Detectors) : config.detectors; Object.keys(Detectors) : config.detectors;
if (isTrueIOSChrome) {
typeArray.push(DebuggerDetector); // 会debuger显示devtool, 仅在ios chrome 真机生效
}
typeArray.forEach(type => { typeArray.forEach(type => {
if (Detectors[type]) { if (Detectors[type]) {
Detectors[type](); Detectors[type](isTrueIOSChrome);
} }
}); });
} }

View File

@ -2,16 +2,19 @@
* @Author: tackchen * @Author: tackchen
* @Date: 2021-11-15 22:26:57 * @Date: 2021-11-15 22:26:57
* @LastEditors: tackchen * @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 * @FilePath: /disable-devtool/src/detector/func-to-string.js
* @Description: Coding something * @Description: Coding something
*/ */
// // ! 会误伤ios mobile chrome 可能会误伤谷歌搜索seo 故放弃使用
import {registInterval} from '../utils/interval'; import {registInterval} from '../utils/interval';
import {DETECTOR_TYPE, triggerOnDevOpen} from './detector'; import {DETECTOR_TYPE, triggerOnDevOpen} from './detector';
import {log, clearLog} from '../utils/log'; import {log, clearLog} from '../utils/log';
export default function detector () { export default function detector (isTrueIOSChrome) {
if (isTrueIOSChrome) return;
let count = 0; let count = 0;
const func = () => {}; const func = () => {};
func.toString = () => { func.toString = () => {

View File

@ -1,3 +1,5 @@
import {log} from './log';
export function isPC () { export function isPC () {
return !/(iphone|ipad|ipod|ios|android)/i.test(navigator.userAgent.toLowerCase()); return !/(iphone|ipad|ipod|ios|android)/i.test(navigator.userAgent.toLowerCase());
} }
@ -104,3 +106,26 @@ export const isMacOs = hasUaName('macintosh');
export const isOldEdge = hasUaName('edge') && !hasUaName('chrome'); export const isOldEdge = hasUaName('edge') && !hasUaName('chrome');
export const isIE = isOldEdge || hasUaName('trident') || hasUaName('msie'); 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);
});
}