Open Chrome DevTools
Ctrl + Shift + I => 唤醒控制台
DevTools for Beginners
Elements => Styles => + New Style Rule (添加新的临时注入样式表)
Elements => Styles => :hov (激活元素伪类状态) => :active, :hover, :focus, :visited, :focus-within
CSS
CSS Reference
Ctrl + Shift + C => select an element in the page to inspect it (选择元素检查)
Elements => Computed => Show all => (显示所有 css 继承属性)
Command Menu (命令菜单) => Ctrl + Shift + P (在唤醒控制台的前提下)
JavaScript
Sources => Event Listener Breakpoints => (特定事件发生时调试)
Sources => Ctrl + Shift + O (查找函数定义) + Ctrl + O (查找文件)
Search => Ctrl + Shift + F (查找面板)
Sources => Watch => (监听变量/表达式的值)
Pause Your Code With Breakpoints
Breakpoint Type | Use This When You Want to Pause... |
---|---|
Line-of-code | On an exact region of code. |
Conditional line-of-code | On an exact region of code, but only when some other condition is true. |
DOM | On the code that changes or removes a specific DOM node, or its children. |
XHR | When an XHR URL contains a string pattern. |
Event listener | On the code that runs after an event, such as click, is fired. |
Exception | On the line of code that is throwing a caught or uncaught exception. |
Function | Whenever a specific function is called. |
DOM change breakpoints => Elements => Right-click the element (选中元素鼠标右键点击) => Break on => Subtree modifications (内容变化) , Attribute modifications (属性变化) , Node removal (节点移除)
Exception breakpoints => Sources => Pause on exceptions (八角形暂停按钮)
Function breakpoints => debug(fn) => is equivalent to setting a line-of-code breakpoint on the first line of the function.
JavaScript Debugging Reference
Right-click the line of code that you're interested in, and select Continue to here. DevTools runs all of the code up to that point, and then pauses on that line. (调试时右键选择执行代码到当前行)
While paused on a line of code, right-click anywhere in the Call Stack pane and select Restart Frame to pause on the first line of the top function in your call stack. The top function is the last function that was called. (重新执行调用栈里面的最顶层函数)
Blackbox a script when you want to ignore that script while debugging. When blackboxed, a script is obscured in the Call Stack pane, and you never step into the script's functions when you step through your code.
Blackbox a script from Settings => Settings => Blackboxing => Add pattern
Run snippets of debug code from any page => Sources => Snippets => + New snippet
UI References and Overviews
Keyboard Shortcuts
快捷键 | |
---|---|
Ctrl + F | 搜索当前文件 |
Ctrl + Shift + F | 搜索所有文件 |
Ctrl + O / Ctrl + P | 按文件名搜索 |
Ctrl + D | 选择光标处变量 |
Ctrl + M | 转到匹配的括号处 |
Ctrl + ` | 聚焦到控制台 |
Ctrl + L | 清空控制台输出 |
Ctrl + Shift + C | 检查元素 |
Shift + 左键点击拖动 | 模拟双指手势放大缩小 |
Accessibility
Audits => Accessibility
Elements => (选择元素) => Accessibility
You may prefer to use the aXe extension (Accessibility testing in Chrome Developer Tools) rather than the Audits panel. They generally provide the same information, since aXe is the underlying engine that powers the Audits panel.
ARIA attributes ensure that screen readers have all of the information that they need in order to properly represent a page's contents.
Track Which Element has Focus => Console => Create Live Expression (眼睛) => document.activeElement => Right-click => Reveal in Elements panel / Store as global variable
More Tools => Sensors (传感器) => Geolocation / Orientation
override the user agent string => Ctrl + Shift + P => Command Menu => Show Network conditions => User agent
Remote Debug Android Devices
Android 设备 => Settings => Developer Options => Enable USB Debugging
PC 电脑 => Main Menu => More tools => Remote devices => Discover USB devices
chrome://inspect => 访问已启用调试的 webview 列表
Console
console.clear() => 清除控制台历史记录
Console => console settings => Preserve log => 保留控制台输出历史记录
Network => Preserve log => 保留网络请求历史记录
Settings => Preferences => Console
$() => document.querySelector() $$() => document.querySelectorAll() keys() values() debug(fn) => sets a breakpoint on the first line of that function
// 将控制台消息组织在一起 =>
// 自动折叠组, 不默认展开全部
console.groupCollapsed()
// 允许嵌套
console.group()
console.groupEnd()
console.log()
console.warn()
...
console.groupEnd()
复制代码
// assert 在第一个参数判读为 false 时显示第二个错误参数
console.assert(list.childNodes.length < 500, "Node count is > 500");
复制代码
占位符 | |
---|---|
%s | string |
%i / %d | int |
%f | float |
%o | DOM |
%O | Object |
%c | css style |
console.count(label) => 标签计数 console.dir() => 以 JavaScript 形式表示的指定对象 console.dirxml(object) => 输出 XML 表示形式
// 性能分析
function processPixels() {
console.profile("processPixels()");
// later, after processing pixels
console.profileEnd();
}
复制代码
// 计时器
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");
复制代码
console.timeStamp(label) => 在录制会话期间向 Timeline 添加一个事件
console.trace() => 从调用位置输出一个堆叠追踪
console.table() => 直观查看数组/对象
// inspect() 函数选取一个 DOM 元素或 JavaScript 引用作为一个参数
// 如果提供 DOM 元素, 进入 Elements 面板并显示该元素.
// 如果提供 JavaScript 引用, 则进入 Profile 面板
$('[data-target="inspecting-dom-elements-example"]');
inspect($_);
复制代码
Monitor Events
- 使用 monitorEvents() 侦听特定类型的事件
- 使用 unmonitorEvents() 停止侦听
- 使用 getEventListeners() 获取 DOM 元素的侦听器
- 使用 Event Listeners Inspector 面板获取有关事件侦听器的信息
monitorEvents(document.body, "click");
unmonitorEvents(document.body);
getEventListeners(document);
复制代码
- $_ 返回最近评估的表达式的值
- $0 - $4 Elements 选择的最后五个 DOM 元素或 Profiles 选择的最后五个 JavaScript 堆对象的历史记录
- $(selector) => document.querySelector()
- $$(selector) => document.querySelectorAll()
- $x(path) => 与 XPath 表达式匹配的 DOM 元素数组
- clear()
- copy(object)
- debug(fn) => undebug(fn)
- dir(object) => console.dir()
- dirxml(object) => console.dirxml()
- inspect(object/fn) => ( DOM => Elements / JavaScript Object => Profiles )
- keys(object)
- values(object)
- monitor(fn) => 指明函数名称及调用时所需参数 => unmonitor(fn)
- monitorEvents(object, events) => 监听指定对象指定事件 => unmonitorEvents(object[, events])
- profile(name) => 启动 JavaScript CPU 分析会话 => profileEnd()
- table(data[, columns])
Performance
RAIL
=> Response (响应) , Animation (动画) , Idle (空闲) , Load (加载)
- 以用户为中心
- 立即响应用户 => <= 100ms
- 设置动画或滚动时, 在 10ms 内生成帧 => 人们特别擅长跟踪运动, 如果动画不流畅, 他们就会对运动心生反感. 用户可以感知每秒渲染 60 帧的平滑动画转场. 也就是每帧 16 毫秒(包括浏览器将新帧绘制到屏幕上所需的时间), 留给应用大约 10 毫秒的时间来生成一帧. => { JavaScript => Style => Layout => Paint => Composite }
- 最大程度增加主线程的空闲时间 => 利用空闲时间完成推迟的工作, 推迟的工作应分成每个耗时约 50ms 的多个块. 如果用户开始交互, 优先级最高的事项是响应用户. 要实现小于 100 毫秒的响应, 应用必须在每 50 毫秒内将控制返回给主线程, 这样应用就可以执行其像素管道, 对用户输入作出反应等等. 以 50 毫秒块工作既可以完成任务, 又能确保即时的响应.
- 持续吸引用户 => <= 1000ms 呈现交互内容
The main metric for measuring the performance of any animation is frames per second (FPS). Users are happy when animations run at 60 FPS.
在 FPS 图下面是CPU图. CPU 图表中的颜色与性能面板底部的Summary选项卡中的颜色相对应. CPU 图表充满颜色这一事实意味着在记录期间 CPU 已经达到了最大值. 每当您看到 CPU 长时间耗尽时, 就提示您要想办法减少工作量.
In the Frames section, hover your mouse over one of the green squares. DevTools shows you the FPS for that particular frame. Each frame is probably well below the target of 60 FPS.
Ctrl + Shift + P => Command Menu => Show Rendering => FPS Meter
Expand the Main section. DevTools shows you a flame chart of activity on the main thread, over time. The x-axis represents the recording, over time. Each bar represents an event. A wider bar means that event took longer. The y-axis represents the call stack. When you see events stacked on top of each other, it means the upper events caused the lower events.
Optimize Website Speed
Audits
Each Size cell shows two values. The top value is the size of the downloaded resource. The bottom value is the size of the uncompressed resource.
Ctrl + Shift + P => Command Menu => Show Coverage => Coverage => js 文件代码使用情况
When you're working with your own code, the Coverage tab can help you analyze your code, line-by-line, and only ship the code that's needed for page load.
Ctrl + Shift + P => Command Menu => Show Request Blocking => Request Blocking => 请求拦截
Performance => Main => Bottom-Up
Network
TTFB => Time To First Byte => waiting to receive the first byte from the server
If the connection is slow, consider hosting your content on a CDN
or changing hosting providers.
If the server is slow, consider optimizing database queries, implementing a cache, or modifying your server configuration
.
Timing breakdown phases explained
Queueing => 排队时间 Stalled => 被停止 DNS Lookup => 解析IP Proxy negotiation => 浏览器正在与代理服务器协商请求 Request sent => 请求发送 Request to ServiceWorker => 请求发送到sw Waiting(TTFB) => 浏览器正在等待响应的第一个字节. 这个时间包括1次延迟往返和服务器准备响应所花费的时间. Content Download => 浏览器正在接收响应 Receiving Push => 接收HTTP2服务器推送 Reading Push => 读取以前接收到的本地数据
To view the initiators and dependencies of a request, hold Shift and hover over the request in the Requests table. DevTools colors initiators green, and dependencies red. 若要查看请求的发起者和依赖项, 请按住Shift并将鼠标悬停在请求表中的请求上. DevTools将启动器设置为绿色, 而依赖项设置为红色.
View the stack trace that caused a request When a JavaScript statement causes a resource to be requested, hover over the Initiator column to view the stack trace leading up to the request. 当JavaScript语句导致请求资源时, 将鼠标悬停在Initiator列上, 以查看导致请求的堆栈跟踪.
Memory
垃圾回收期间, 所有脚本执行将暂停.
More tools => Task manager => Shift + Esc => 任务管理器
Memory => 原生内存 => DOM节点存储在此 => 值大说明正在创建DOM节点
JavaScript Memory => 实时数字表示页面上可到达对象正在使用的内存量 => 正在创建对象或已有对象正在增长
只有页面的 DOM 树或 JavaScript 代码不再引用 DOM 节点时, DOM 节点才会被作为垃圾进行回收. 如果某个节点已从 DOM 树移除, 但某些 JavaScript 仍然引用它, 我们称此节点为"已分离". 已分离的 DOM 节点是内存泄漏的常见原因.
Memory => Profiles