使用新的命令编辑器构建 Chrome 开发者工具协议 (CDP) 命令
Craft Chrome Devtools Protocol (CDP) commands with the new command editor

原始链接: https://developer.chrome.com/blog/cdp-command-editor

## Chrome DevTools CDP 编辑器:摘要 Chrome DevTools 协议 (CDP) 允许开发者远程与 Chrome 浏览器交互,用于调试和自动化。为了简化此过程,DevTools 引入了一个新的 CDP 编辑器,旨在简化 CDP 命令的编写和发送。 此前,手动构建命令因复杂的语法和不断参考文档的需求而繁琐。新的编辑器通过**命令和参数的自动补全**解决了这个问题,减少了错误并节省了时间。它动态显示必需(红色)和可选(蓝色)参数,为字符串、数字、枚举、布尔值、数组和对象提供直观的输入字段。 主要功能包括**实时错误检查**、**带有文档链接的工具提示**以及**编辑和重新发送**以前使用的命令的能力。用户还可以轻松**将命令复制为 JSON 格式**。 该编辑器的目标是使 CDP 更易于访问,提高原型设计速度和整体开发者体验。鼓励用户在 Chrome Canary、Dev 或 Beta 渠道中尝试新功能并提供反馈。

## Chrome DevTools 协议命令编辑器发布 Chrome 最近发布了一个新的 DevTools 协议 (CDP) 命令编辑器,目前隐藏在实验标志之后(可通过 DevTools 设置访问)。这允许开发者直接编写 CDP 命令。 讨论强调了 Chrome 和 Firefox 开发工具的一个显著区别:CDP 拥有完整的文档、稳定的 API 和强大的工具,而 Firefox 的同等功能缺乏这些特性,通常需要逆向工程,并且容易因更新而损坏。 虽然 CDP 功能强大,但一些用户认为它不一致且文档编写不足,尽管它在一段时间内保持稳定。Puppeteer 和 Playwright 等工具是*基于* CDP 构建的,新的编辑器在这些框架缺乏特定功能或参数控制时很有用。用例包括高级调试、注入标头和自定义浏览器扩展(如 SteamOS 中使用的扩展)。 对话还涉及 Firefox 的替代方案以及由于隐私功能而跟踪浏览器使用情况的挑战。目前 Firefox 没有原生类似的工具,但有一些第三方选项可用。
相关文章

原文

Chrome DevTools Protocol (CDP) is a remote debugging protocol (API) that lets developers communicate with a running Chrome browser. Chrome DevTools uses CDP to help you inspect the browser's state, control its behavior, and collect debugging information. You can also build Chrome extensions that use CDP.

For example, this is a CDP command that inserts a new rule with the given ruleText in a stylesheet with given styleSheetId, at the position specified by location.

{ 
    command: 'CSS.addRule', 
    parameters: {
        styleSheetId: '2',
        ruleText:'Example', 
        location: {
            startLine: 1,
            startColumn: 1,
            endLine: 1,
            endColumn: 1
        }
    }
}

The Protocol monitor drawer tab provides you with a way to send CDP requests and view all the CDP requests and responses DevTools sends and receives.

The command line bar at the bottom of Protocol monitor.

Previously it was difficult to craft the command by hand, especially a command with many parameters. Not only did you need to be mindful of opening and closing brackets and quotation marks, you also had to remember the command's parameters which, in turn, makes you look up the CDP documentation.

To solve this problem, DevTools introduced a new CDP editor whose main goals are to:

  • Auto-complete commands. Simplify your CDP command input by providing you with the list of available commands via an auto completion feature.
  • Auto-populate command parameters. Reduce the need to check the CDP documentation for the list of available command parameters.
  • Simplify the typing of parameter. You just have to fill in the values of the parameters you want to send.
  • Edit and resend. Improve prototyping speed by making it quicker to modify a CDP command.

Now, let's have a look at what this new editor offers, and how you can make use of it!

Autocompletion feature

The autocompletion drop-down menu.

An auto completion feature now powers the command input bar. It helps you write the names of the CDP commands you have access to. This can be very handy for commands that don't accept parameters.

String and number parameters

With this new editor, you can now easily edit the values of primitive parameters. To open the editor, click the Open left panel. icon next to the command input.

Once you enter the command name, the editor shows you the corresponding parameters automatically. You don't have to look up documentation to know what parameters go with what commands. Furthermore, the editor displays the parameters in a given order: the mandatory ones first (in red) and the optional ones next (in blue).

To add a value to an optional parameter, hover over its name and click the + button. To reset the parameter to undefined, click the Reset to default value button.

The + and 'Reset to default value' buttons.

Enum and boolean parameters

When editing enum or boolean parameters, you'll see a drop-down menu that provides a selection of potential values (for enums) or the straightforward true or false option for booleans. This feature reduces the possibility of typing the wrong value for enum parameters and maintains accuracy and simplicity.

The boolean and enum drop-down menus.

Array parameters

For array parameters, you can manually add values to the array. Hover over the parameter's row and click the + button.

The + button that adds an array item.

To delete array items one by one, click the bin button next to the items. You can also clear all the parameters from the array with the block button. In this case, the array parameter is reset to [].

The 'Delete parameter' and 'Reset to default' buttons.

Object parameters

When you enter a command that accepts object parameters, the editor lists the keys of this object and you can edit their values directly. This works for all types of nested parameters.

Nested parameters.

Discover what the command and parameters do in the editor

Were you ever uncertain about the purpose of a parameter or command? Now, you can hover over a command or parameter, and a descriptive tooltip will pop up, complete with a link to the online documentation.

The descriptive tooltip that appears when you hover over a command.

Be warned before sending incorrect parameters

Previously, if you didn't know if a parameter's value was of the correct type and had to wait to read the error response, this new editor is for you. It shows you real-time errors if the parameter can't accept the value you entered.

An error icon next to a parameter with an incorrect value.

Resend a command

If you need to tweak a parameter of the command you just sent, you don't have to type it again. To edit and resend the command, right-click an item in the datagrid and select Edit and resend from the drop-down menu. This will automatically reopen the CDP editor and prefill it with the command you selected.

The drop-down menu of a command in the datagrid with the 'Edit and resend' option.

Copy a command to JSON format

To copy the CDP command in JSON format to your clipboard, click the Copy. copy icon at the leftmost end of the toolbar. Additionally, keep in mind that if you input a command directly into the input bar, it will seamlessly populate the editor, and the other way around.

Conclusion

The DevTools team's goal behind the design of this new CDP editor was to simplify the typing of CDP commands. The new editor can also be used to view parameters alongside the documentation and to provide you with an easier way to send your CDP commands.

Download the preview channels

Consider using the Chrome Canary, Dev, or Beta as your default development browser. These preview channels give you access to the latest DevTools features, let you test cutting-edge web platform APIs, and help you find issues on your site before your users do!

Use the following options to discuss the new features, updates, or anything else related to DevTools.

联系我们 contact @ memedata.com