我不是一个开发人员,对Javascript代码有非常基本的理解。我正在尝试打开一个网站上的开发工具,但它检测到开发工具已经打开,并给出一个弹出窗口,上面写着“检测到DevTools”,然后在我按下ok后关闭网站选项卡。这是源代码中名为DevToolDetect.js的文件。我正在尝试理解它是如何检测到devtools已经被打开的,即使菜单是未停靠的。如果问题中有问题,请告诉我,以便我可以提供任何所需的信息。我不需要逐行的解释,只需要一个摘要就可以了。谢谢!
/*!
devtools-detect
Detect if DevTools is open
https://github.com/sindresorhus/devtools-detect
By Sindre Sorhus
MIT License
*/
(function () {
'use strict';
const devtools = {
isOpen: false,
orientation: undefined
};
const threshold = 160;
const emitEvent = (isOpen, orientation) => {
window.dispatchEvent(new CustomEvent('devtoolschange', {
detail: {
isOpen,
orientation
}
}));
};
const main = ({emitEvents = true} = {}) => {
const widthThreshold = window.outerWidth - window.innerWidth > threshold;
const heightThreshold = window.outerHeight - window.innerHeight > threshold;
const orientation = widthThreshold ? 'vertical' : 'horizontal';
if (
!(heightThreshold && widthThreshold) &&
((window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized) || widthThreshold || heightThreshold)
) {
if ((!devtools.isOpen || devtools.orientation !== orientation) && emitEvents) {
emitEvent(true, orientation);
}
devtools.isOpen = true;
devtools.orientation = orientation;
} else {
if (devtools.isOpen && emitEvents) {
emitEvent(false, undefined);
}
devtools.isOpen = false;
devtools.orientation = undefined;
}
};
main({emitEvents: false});
setInterval(main, 500);
if (typeof module !== 'undefined' && module.exports) {
module.exports = devtools;
} else {
window.devtools = devtools;
}
})();发布于 2021-05-22 19:08:33
对于非Chrome浏览器和基于Chrome浏览器的devtools对象的使用,看起来像是历史上成长起来的启发式尝试和错误。
对于非Chrome浏览器,它会通过计算浏览器窗口中是否有一部分不显示网页,是否足够大来托管devtools来进行一些猜测,它还会检查Firebug的痕迹,尽管现在仍然有非常非常低的概率找到它。
在一次简短的Chrome测试中,此脚本无法检测到分离的devtools窗口。
我觉得很受邀请来深入了解这些提示。你可能会学到很多,至少考虑到这是Sindre Sorhus的脚本,人类知道的man with the most npm packages and GitHub repos。
https://stackoverflow.com/questions/67648393
复制相似问题