Chrome调试

Chrome调试相关

Chrome调试工具是日常项目中常用到的,但面对Command + Shift + i(window:F12)开启调试面板,总有些不被知晓的功能被遗漏。本着工欲善其事,必先利其器的想法,扒点历史存货,也从官网系统学习一下Chrome较为完善的功能.

Elements Panel

所见即所得,主要用于显示编辑htmlstyle样式

DOM Inspect:

  1. 鼠标选中,右键inspect
  2. 菜单底部可快速定位父级元素
  3. 选中元素Enter,then press Tab可快速选择属性,进行编辑

DOM样式修改

  1. html双击修改,style单击修改,tabor enter确认修改
  2. 查看历史修改,可通过以下操作:
    • In the Styles pane, click on the file that you modified. DevTools takes you to the Sources panel.
    • Right-click on the file.
    • Select Local modifications.

演示地址:

历史修改查看

Set DOM breakpoints

按照DOM Inspect提示选中元素后,可右键设置DOM breakpoints

  1. Subtree Modifications: 加在父级上的断点,用来监听子元素的内容增删
  2. Attribute Modifications:加在元素上的断点,用来监听元素的属性变化
  3. Node Removal:加在元素上的断点,用来监听元素是否被删除

断点事件触发,会有如下提示

breakpoint-reason.png

以上断点虽然都在DOM元素上添加,实则仍为监听JS

Console Panel

Shortcut: Press Ctrl+Shift+J (Windows / Linux) or Cmd+Opt+J (Mac).

Console:输出控制台,前端调试后花园,内容输出显示,或者直接执行javascript代码

Write Console

  1. javascript文件中执行console.log('some function or string')

  2. 控制台直接输入命令执行

Console API

  1. console.group:内容输出成组

    // 输出在控制台的内容成组,可实现多层级,以及折叠效果(需要配合console.groupCollapsed)
    console.group('group start'); //group开始
    console.log('group content 1'); // group 内容1
    ... // group 其他内容
    console.groupEnd(); //group结束
    

    效果如下:

group.png
  1. console.error():输出错误

    function connectToServer() {
        console.error("Error: %s (%i)", "Server is  not responding",500);
    }
    connectToServer();
    
  2. console.warn():输出警告

    if(a.childNodes.length < 3 ) {
        console.warn('Warning! Too few nodes (%d)', a.childNodes.length);
    }
    
  3. console.assert:输出断言,不影响后续的函数执行,断言可以viewexception stack trace.

track-exceptions-exception-stack-trace.jpg

Writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.

MDN - Console.assert()

console.assert(list.childNodes.length < 500, "Node count is > 500");
  1. console.table:表格

    [官方示例](https://developers.google.com/web/tools/chrome-devtools/debug/console/structured-data?hl=en)

  2. console.time(arg),console.timeEnd(arg):查看函数执行时间。

    console.time("Array initialize");
    var array= new Array(1000000);
    for (var i = array.length - 1; i >= 0; i--) {
        array[i] = new Object();
    };
    console.timeEnd("Array initialize");
    

    结果:

track-executions-time-duration.png
  1. console.trace可追踪执行路径

    function foo() {
      function bar() {
        console.trace();
      }
      bar();
    }
    
    foo();
    

    The output in the console looks something like this:


    api-trace2.png

完整Console文档在这里:Console API Reference

Console Format

  1. 基本Format.如上,内容输出可进行格式化输出.Console支持的输出列表:

    Specifier Output
    %s Formats the value as a string
    %i or %d Formats the value as an integer
    %f Formats the value as a floating point value
    %o Formats the value as an expandable DOM element. As seen in the Elements panel
    %O Formats the value as an expandable JavaScript object
    %c Applies CSS style rules to the output string as specified by the second parameter
  2. 格式化输出CSS样式

    // 控制台可直接输出带有样式的内容
    console.log("%cThis will be formatted with large, blue text", "color: blue; font-size: x-large");
    

  3. 格式化DOM元素为对象:

    By default, DOM elements are logged into the console as representation of their HTML, but sometimes you want to access the DOM element as JavaScript object and inspect its properties. You can use the %o string specifier to do that (see above), or use console.dir to achieve the same:

    console.dir(document.body.firstElementChild)
    

Print stack traces

以上console.trace可实现路径追踪,在报错中,我们可以通过Error输出打印

  1. Error.stack:error 的stack属性可以输出执行路径
track-exceptions-error-stack.jpg

Tips:

  1. 控制台输入clear()可快速实现清屏
  2. 同上清屏功能,可以在自己的JS代码中执行console.clear(),解除控制台各种问题

Source Panel

Source:顾名思义,加载的资源这里都能找得到。此部分主要介绍断点。

Set Breakpoint

  1. 方法一:Source内容区设置

    • 主体内容区域需要设置断点的行号前点击,对应的行即设置好了。以此类推。
    • 刷新浏览器,根据页面逻辑以此会在断点处暂停执行,用于`debug
  2. 方法二:js文件中设置

    • js文件中需要执行断点操作的code line前加上debugger

      function sum(a, b){
        debugger;
        return a + b;
      }
      

Breakpoint conditional断点执行条件

右键设置的断点,选择Edit breakpoint输入执行断点的条件

adding-condition.png

Breakpoint Step

0.gif
  • Pause/Resume script execution:暂停/恢复脚本执行(程序执行到下一断点停止)。
  • Step over next function call:执行到下一步的函数调用(跳到下一行)。
  • Step into next function call:进入当前函数。
  • Step out of current function:跳出当前执行函数。
  • Deactive/Active all breakpoints:关闭/开启所有断点(不会取消)。
  • Pause on exceptions:异常情况自动断点设置。

Call Stack

可以通过勾选Async实现异步查找事件依赖关系,方便debug。官方建议避免使用匿名函数。

Near the top of the sidebar is the Call Stack section. When the code is paused at a breakpoint, the call stack shows the execution path, in reverse chronological order, that brought the code to that breakpoint. This is helpful in understanding not just where the execution is now, but how it got there, an important factor in debugging.

The call stack

image_15.png

An initial onclick event at line 50 in the index.html file called thesetone() function at line 18 in the dgjs.js JavaScript file, which then called the setall() function at line 4 in the same file, where execution is paused at the current breakpoint.

可以通过右键在对应的脚本(如第三方)文件中设置blackbox关进小黑屋,防止迷惑debug.官方是通过toolssetting进行全局设置。

Data manipulation更改执行数值

暂停break,控制台输出需要变更的值,强制更改值即可继续按照所需执行.

image_17.png

针对Workspaces 和 Sourcemaps开启的权限,见下。

Set Up Persistence with DevTools Workspaces

Set Up CSS & JS Preprocessors

参考链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容