chrome调试小技巧合集

126 阅读1分钟

1、DOM断点调试

image.png

image.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- <button id="create">创建detached dom 节点</button> -->
    <br>
    <br>
    <br>
    <h1 id="h1Dom">请设置DOM断点</h1>
    <button id="removeH1">移除dom断点</button>
    <br>
    <br>
    <br>
    <button id="debuggerBtn">调试按钮</button>

    <script>
        window.onload = () => {
            
            // // detached dom 节点
            // let detachedTree;
            // let create = () => {
            //     let ul = document.createElement('ul');
            //     for(let i = 0;i <= 1000; i ++) {
            //         let li = document.createElement('li');
            //         ul.append(li)
            //     }
            //     detachedTree = ul;
            //     console.log(detachedTree, 'detachedTree');
            // }
            // document.getElementById('create').addEventListener('click', create);

            // dom断点
            let removeH1 = () => {
                document.getElementById('h1Dom').remove();
            }
            document.getElementById('removeH1').addEventListener('click', removeH1);

            // 调试及添加log
            let debuggerHandler = () => {
                let showName = (name) => {
                    const nameIndex = name.match(/\d+/)[0];
                    debugger;
                    const info = `my name is ${name}, name index is ${nameIndex}`;
                    return {
                        nameIndex
                    }
                }
    
                const names = Array(1000).fill(1).map((item, index) => {
                    return `name${index}`;
                });
                names.forEach((name, index) => {
                    debugger;
                    showName(name);
                })
            }
            document.getElementById('debuggerBtn').addEventListener('click', debuggerHandler);


            // setTimeout(() => {
            //     debugger
            // })

        }

    </script>
</body>
</html>

思考:MutationObserver、IntersectionObserver、ResizeObserver 还记得吗 是否和dom断点的触发是一个原理呢?

2、调试条件断点

image.png

3、日志点添加

image.png

**4、node调试 **--inspect-brk

console.log('hello nodejs')
console.log('hello yyzone laotie')


const cp = require('child_process')
console.log('i am main process')
cp.fork('./child.js', {
    execArgv: ['--inspect-brk']
})

webpack插件调试 node --inspect-brk ./node_modules/webpack/bin/webpack --config ./build.js

class LogWebpackPlugin{
    constructor(doneCallback,emitCallback){
       this.emitCallback = emitCallback;
       this.doneCallback = doneCallback;
    }
    apply(compiler){
        console.log(compiler, 'compiler')
        debugger
        compiler.hooks.emit.tap('LogWebpackPlugin',()=>{
            //在 emit 事件中回调 emit Callbackthis.emitCallback()
        })
        compiler.hooks.done.tap('LogWebpackPlugin',()=>{
            //在 done 事件中回调 done Callbackthis.emitCallback()
        })
        compiler.hooks.compilation.tap('LogWebpackPlugin',()=>{
            // compilation('编译器'对'编译ing'这个事件的监听)
        })
        compiler.hooks.compile.tap('LogWebpackPlugin',()=>{
            // compile('编译器'对'开始编译'这个事件的监听)
        })
    }
}
module.exports = LogWebpackPlugin

5、setTimeout巧用

setTimeout(() => {
     debugger
})

6、聊聊我所知道的工作中用到的
--wui-secondary-color: rgb(219 224 229);颜色切换
windows的方法直接定位到代码