Interviewer, I implemented a Chrome Devtools

Interviewer, I implemented a Chrome Devtools

[[426371]]

Web pages will load resources, run JS, render interfaces, store data, etc. How can we see the execution status when developing? Use the debugging tool chrome devtools. It supports dom debugging, JS debugger, local storage display, runtime profile, etc.

The same is true for Node.js, but it only supports JS debugger and profile. We can debug through chrome devtools or vscode debugger.

These tools are all remotely attached to the running program for debugging. How do they exchange data? Through webSocket. In addition, the chrome devtools protocol is also formulated to specify what capabilities and how to communicate.

This websocket-based debugging protocol is called the chrome devtools protocol. Because it has many functions, it is divided into multiple domains (complex things are usually divided into domains), including DOM, Debugger, Network, Page, etc., each with a different debugging protocol. Chrome devtools implements debugging through this protocol.

The new version of Chrome (Canary version) can turn on the Protocol Monitor panel of the experimental features in the settings.

You can see the transmitted CDP data:

This is the principle of chrome devtools.

What is the use of understanding this principle?

We can re-implement the server, and as long as the debugging protocol is connected, we can use chrome devtools to debug.

For example, how does Kraken (rendering CSS to Flutter) use Chrome DevTools to debug DOM and styles? It is by connecting to this protocol.

We can re-implement the client, and as long as it is connected to this protocol, we can use any tool to debug web pages/Node.js.

You can debug Node.js and web pages with chrome devtools, vscode debugger, or webstorm debugger. Why? Because they are all connected to this protocol.

So can we connect to this protocol to implement a debugging tool similar to Chrome DevTools?

Let's experiment:

We start Chrome and specify the debugging port via --remote-debugging-port:

  1. /Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary --remote-debugging-port=9222  

Then connect it.

Here we don't need to connect to the protocol directly. Chrome provides SDKs in various languages ​​and we can just call the API:

Let's connect to Chrome first:

  1. const CDP = require( 'chrome-remote-interface' );
  2.  
  3. async function test() {
  4. let client;
  5. try {
  6. client = await CDP();
  7. const { Page, DOM, Debugger } = client;
  8. //...
  9. } catch(err) {
  10. console.error(err);
  11. }
  12. }
  13. test();

Then open baidu.com, wait 2 seconds, and take a screenshot:

  1. const CDP = require( 'chrome-remote-interface' );
  2. const fs = require( 'fs' );
  3.  
  4. async function test() {
  5. let client;
  6. try {
  7. client = await CDP();
  8. const { Page, DOM, Debugger } = client;
  9.  
  10. await Page.enable();
  11. await Page.navigate({url: 'https://baidu.com' });
  12.  
  13. await new Promise(resolve => setTimeout(resolve, 2000));
  14.   
  15. const res = await Page.captureScreenshot();
  16. fs.writeFileSync( './screenshot.jpg' , res.data, {
  17. encoding: 'base64'  
  18. });
  19. } catch(err) {
  20. console.error(err);
  21. }
  22. }
  23. test();

Check out the effect:

In this way, we have run through the first section of CDP code.

The rest of the functions, including Network, Debugger, DOM, etc., can also be realized. Let's try it briefly:

  1. await DOM.enable();
  2.  
  3. const { root } = await DOM.getDocument({
  4. depth: -1
  5. });

Depth is the depth. Setting it to -1 means returning the entire DOM:

With this data, can we browse the DOM?

There is also DOM.setAttributeValue to set attributes, DOM.getBoxModel to get the box model size, etc.

Based on these, it should be no problem for us to make a DOM editor.

And the network part:

  1. await Network.enable();
  2. Network. on ( 'responseReceived' , async evt => {
  3. const res = await Network.getResponseBody({
  4. requestId: evt.requestId
  5. });
  6.  
  7. console.log(evt.response.url);
  8. console.log(res.body);
  9. });

We listen to each response through the responseReceived event, and then get the content of the response through Network.getResponseBody:

Based on this, we should have no problem implementing the Network panel functionality.

You can also connect to the profiler:

  1. await Profiler.start();
  2. await new Promise(resolve => setTimeout(resolve,2000));
  3. const { profile } = await Profiler.stop();

With this data, we can draw a flame graph through canvas.

Theoretically, we can implement all the functions of Chrome DevTools. Moreover, it is possible to debug a web page with multiple debugging tools at the same time, because websocket can have multiple clients.

You might ask, what's the point of implementing Chrome DevTools yourself?

When people do open source front-end projects, they usually write a NetEase Cloud Music client because there is ready-made data to use. So why not make a Chrome DevTools? There is also ready-made data, just start the browser, and it is so high-end.

We don't need to implement the complete chrome devtools. We can implement the network part, the DOM part, and the debugger part separately. We can make different UIs and have functions and interactions that chrome devtools don't have.

For example, if you are interviewing for a visualization position, and you say that you have connected to the profiler part of the chrome devtools protocol and used canvas to draw a flame chart, it will add a lot of extra points.

Summarize

Chrome debugging communicates with the debugging client through WebSocket, and has developed the Chrome Devtools Protocol, which is also used by Node.js, but the protocol is called V8 debugger protocol. We can see all CDP protocol request responses through the protocol monitor panel.

The CDP server can be implemented to connect to the debugging function of chrome devtools and debug different targets, such as the kraken rendering engine.

You can implement CDP client to debug with different tools, such as vscode debugger, webstorm debugger, etc.

We can also connect to the CDP server through the SDK API to get data and implement debugging functions. For example, we can implement DOM editor, Network viewer, JS Debugger, Profiler and flame graph separately, and we can achieve more powerful functions and better interaction than Chrome DevTools.

When you want to do an open source project but don't have data, you might as well consider making a CDP client. Isn't this better than the Cloud Music project?

<<:  6G is coming? Is it too early to start 6G research now?

>>:  What does the increasingly popular 5G public network dedicated service mean?

Recommend

Under the trend of "new infrastructure", the cybersecurity industry is booming

In the next few years, the general direction of n...

Diagram: 5G millimeter wave peak rate calculation

[[390044]] This article is reprinted from the WeC...

Surge in mobile data usage puts Wi-Fi performance under severe test

According to the policies of communication regula...

What is edge computing from a hardware perspective?

Edge computing has exploded due to the massive am...

10 things to know about MU-MIMO Wi-Fi

Multi-User MIMO allows multiple Wi-Fi devices to ...

The 5G R16 standard is frozen. What does it mean?

On July 3, 3GPP announced the completion of the s...

Why does WiFi 7 depend on the 6GHz band?

Over the past 20 years, as the most commonly used...

The future development trend of the Internet will transition from HTTP to IPFS

We know that IPFS is a new Internet underlying pr...

Is 4G enough? More than 40% of users turn off 5G function in new smartphones

So is 5G really that important? Is 4G no longer a...