打开devtools是否触发了事件?

时间:2019-09-08 11:25:30

标签: javascript browser devtools

从问题标题可以明显看出,我需要一个与打开devtools相关的事件的回调,例如

    /* I expect for something like that with var "devtools" as window's object or the instanceof an unknowed class  */

    devtools.onDevtoolsOpened = () =>
    {
        // do my stuff
    }

如果可能,请寻求帮助。但是,如果没有,是否有相关的行动或其他方式可以实现我的愿望?

1 个答案:

答案 0 :(得分:1)

这实际上取决于每个单独的浏览器。但是,我发现了一个可以对大多数浏览器起作用的模块: devtools-detect
从他们的文档中:

<script src="node_modules/devtools-detect/index.js"></script>
<script type="module">
    // Check if it's open
    console.log('Is DevTools open:', window.devtools.isOpen);

    // Check it's orientation, `undefined` if not open
    console.log('DevTools orientation:', window.devtools.orientation);

    // Get notified when it's opened/closed or orientation changes
    window.addEventListener('devtoolschange', event => {
        console.log('Is DevTools open:', event.detail.isOpen);
        console.log('DevTools orientation:', event.detail.orientation);
    });
</script>