> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# DOM

本页面介绍 Lynx DevTool 支持的 `DOM` CDP domain。



:::warning 实验性参考
此生成的参考文档仍在完善中。

- CDP events 尚未包含。
- 部分 Lynx 扩展已列出，但尚未提供详细文档。
- 方法说明尚未根据实现自动校验，可能不完整或不准确。

在依赖本参考前，请根据实现及所链接的 upstream CDP reference 验证实际行为。
:::

## 概览

- 作用域：`instance`
- CDP domain：[DOM](https://chromedevtools.github.io/devtools-protocol/tot/DOM/)

## 标准方法

- [DOM.copyTo](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-copyTo)
- [DOM.describeNode](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-describeNode)
- [DOM.disable](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-disable)
- [DOM.discardSearchResults](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-discardSearchResults)
- [DOM.enable](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-enable)
- [DOM.focus](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-focus)
- [DOM.getAttributes](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getAttributes)
- [DOM.getBoxModel](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getBoxModel)
- [DOM.getDocument](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getDocument)
- [DOM.getNodeForLocation](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getNodeForLocation)
- [DOM.getOuterHTML](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getOuterHTML)
- [DOM.getSearchResults](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getSearchResults)
- [DOM.markUndoableState](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-markUndoableState)
- [DOM.moveTo](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-moveTo)
- [DOM.performSearch](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-performSearch)
- [DOM.pushNodesByBackendIdsToFrontend](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-pushNodesByBackendIdsToFrontend)
- [DOM.querySelector](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-querySelector)
- [DOM.querySelectorAll](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-querySelectorAll)
- [DOM.removeNode](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-removeNode)
- [DOM.requestChildNodes](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-requestChildNodes)
- [DOM.scrollIntoViewIfNeeded](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-scrollIntoViewIfNeeded)
- [DOM.setAttributesAsText](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-setAttributesAsText)
- [DOM.setInspectedNode](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-setInspectedNode)
- [DOM.setOuterHTML](https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-setOuterHTML)

## Lynx 扩展

### `DOM.disableDomTree`

Disables Lynx DOM tree capture and reloads the current page.

- Origin: `lynx-extension`
- Source: `devtool/lynx_devtool/agent/domain_agent/inspector_dom_agent_ng.cc`

###### 参数

| Name          | Type      | Required | Description                                                           |
| ------------- | --------- | -------- | --------------------------------------------------------------------- |
| `ignoreCache` | `boolean` | No       | Whether the page reload triggered by this method should ignore cache. |

###### 返回字段

无返回字段。

###### 备注

- Dispatches through LynxDevToolMediator to InspectorTasmExecutor on the TASM thread.
- Sets the local Lynx environment flag kLynxEnableDomTree to false.
- Calls Page.reload with params.ignoreCache. Missing params or a missing ignoreCache field are treated as false.
- Returns an empty result object after scheduling the reload.
- If the mediator has no TASM task runner, the current handler does not send an explicit CDP response.

###### 示例

**Disable DOM tree capture and reload**

Request:

```json
{
  "id": 1,
  "method": "DOM.disableDomTree",
  "params": {
    "ignoreCache": false
  }
}
```

Response:

```json
{
  "id": 1,
  "result": {}
}
```

### `DOM.enableDomTree`

Enables Lynx DOM tree capture and reloads the current page.

- Origin: `lynx-extension`
- Source: `devtool/lynx_devtool/agent/domain_agent/inspector_dom_agent_ng.cc`

###### 参数

| Name          | Type      | Required | Description                                                           |
| ------------- | --------- | -------- | --------------------------------------------------------------------- |
| `ignoreCache` | `boolean` | No       | Whether the page reload triggered by this method should ignore cache. |

###### 返回字段

无返回字段。

###### 备注

- Dispatches through LynxDevToolMediator to InspectorTasmExecutor on the TASM thread.
- Sets the local Lynx environment flag kLynxEnableDomTree to true.
- Calls Page.reload with params.ignoreCache. Missing params or a missing ignoreCache field are treated as false.
- Returns an empty result object after scheduling the reload.
- If the mediator has no TASM task runner, the current handler does not send an explicit CDP response.

###### 示例

**Enable DOM tree capture and reload**

Request:

```json
{
  "id": 1,
  "method": "DOM.enableDomTree",
  "params": {
    "ignoreCache": true
  }
}
```

Response:

```json
{
  "id": 1,
  "result": {}
}
```

### `DOM.getDocumentWithBoxModel`

Returns the Lynx DOM tree with per-node box model data when it can be computed.

- Origin: `lynx-extension`
- Source: `devtool/lynx_devtool/agent/domain_agent/inspector_dom_agent_ng.cc`

###### 参数

无参数。

###### 返回字段

| Name                     | Type             | Description                                                                                                                         |
| ------------------------ | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| `root`                   | `object\|string` | DOM root object when uncompressed, or a base64-encoded compressed string when result.compress is true.                              |
| `compress`               | `boolean`        | Whether result.root was zlib-compressed and base64-encoded by the native handler.                                                   |
| `root.nodeId`            | `integer`        | DevTool node id for the Lynx element.                                                                                               |
| `root.backendNodeId`     | `integer`        | Backend node id. Lynx currently uses the same value as nodeId.                                                                      |
| `root.nodeType`          | `integer`        | DOM node type value reported by ElementInspector.                                                                                   |
| `root.localName`         | `string`         | Local element name reported by ElementInspector.                                                                                    |
| `root.nodeName`          | `string`         | Node name reported by ElementInspector.                                                                                             |
| `root.nodeValue`         | `string`         | Node value reported by ElementInspector.                                                                                            |
| `root.parentId`          | `integer`        | Parent node id when the element has a parent.                                                                                       |
| `root.attributes`        | `array`          | Flat name/value array containing attributes, data attributes, event attributes, class, style, and selected Lynx inspector metadata. |
| `root.childNodeCount`    | `integer`        | Number of child nodes serialized for this node.                                                                                     |
| `root.children`          | `array`          | Child nodes serialized with the same shape as root.                                                                                 |
| `root.box_model`         | `object\|null`   | Box model for this node when Lynx can build and resolve the native layout query. Null when unavailable.                             |
| `root.box_model.width`   | `number`         | Element width after conversion from layout units to pixels.                                                                         |
| `root.box_model.height`  | `number`         | Element height after conversion from layout units to pixels.                                                                        |
| `root.box_model.content` | `array`          | Eight numeric coordinates for the content quad.                                                                                     |
| `root.box_model.padding` | `array`          | Eight numeric coordinates for the padding quad.                                                                                     |
| `root.box_model.border`  | `array`          | Eight numeric coordinates for the border quad.                                                                                      |
| `root.box_model.margin`  | `array`          | Eight numeric coordinates for the margin quad.                                                                                      |

###### 备注

- Dispatches through LynxDevToolMediator to InspectorTasmExecutor on the TASM thread, then asks the platform for native box models before sending the response.
- The returned tree is built from the current element root and uses the same node fields as the standard DOM tree, with Lynx's box_model field added to each serializable node.
- Coordinates are adjusted relative to the root box model in LynxView screenshot mode and converted by LayoutsUnitPerPx.
- If the serialized root exceeds the native DOM compression threshold and compression succeeds, result.compress is true and result.root is a compressed base64 string.
- If a node cannot produce a valid box model, that node's box_model field is null.
- The native implementation does not read request params.
- If the mediator has no TASM task runner or the element root is unavailable, the current handler does not send an explicit CDP response.

###### 示例

**Read the DOM tree with box models**

Request:

```json
{
  "id": 1,
  "method": "DOM.getDocumentWithBoxModel"
}
```

Response:

```json
{
  "id": 1,
  "result": {
    "compress": false,
    "root": {
      "nodeId": 1,
      "backendNodeId": 1,
      "nodeType": 1,
      "localName": "page",
      "nodeName": "page",
      "nodeValue": "",
      "attributes": [],
      "childNodeCount": 1,
      "box_model": {
        "width": 390,
        "height": 844,
        "content": [0, 0, 390, 0, 390, 844, 0, 844],
        "padding": [0, 0, 390, 0, 390, 844, 0, 844],
        "border": [0, 0, 390, 0, 390, 844, 0, 844],
        "margin": [0, 0, 390, 0, 390, 844, 0, 844]
      },
      "children": []
    }
  }
}
```

### `DOM.getOriginalNodeIndex`

Returns the Lynx source node index associated with a DevTool node id.

- Origin: `lynx-extension`
- Source: `devtool/lynx_devtool/agent/domain_agent/inspector_dom_agent_ng.cc`

###### 参数

| Name     | Type      | Required | Description                                                  |
| -------- | --------- | -------- | ------------------------------------------------------------ |
| `nodeId` | `integer` | Yes      | DevTool node id to resolve in the current Lynx element tree. |

###### 返回字段

| Name        | Type      | Description                                                                                                   |
| ----------- | --------- | ------------------------------------------------------------------------------------------------------------- |
| `nodeIndex` | `integer` | Raw node index stored on the resolved Lynx element. The field is omitted when the node id cannot be resolved. |

###### 备注

- Dispatches through LynxDevToolMediator to InspectorTasmExecutor on the TASM thread.
- The handler resolves params.nodeId with the current inspector element map and returns Element::NodeIndex().
- The response result is an empty object when no element is found for params.nodeId.
- If the mediator has no TASM task runner, the current handler does not send an explicit CDP response.

###### 示例

**Resolve a DevTool node to its Lynx source index**

Request:

```json
{
  "id": 1,
  "method": "DOM.getOriginalNodeIndex",
  "params": {
    "nodeId": 42
  }
}
```

Response:

```json
{
  "id": 1,
  "result": {
    "nodeIndex": 7
  }
}
```

### `DOM.getOriginalNodeSourceInfo`

返回与 DevTool 节点 ID 关联的源码信息。

- 来源类型：`lynx-extension`
- 源码：`devtool/lynx_devtool/agent/domain_agent/inspector_dom_agent_ng.cc`

###### 参数

| 名称       | 类型        | 必填 | 说明                               |
| -------- | --------- | -- | -------------------------------- |
| `nodeId` | `integer` | 是  | 要在当前 Lynx 元素树中解析的 DevTool 节点 ID。 |

###### 返回字段

| 名称                 | 类型             | 说明                                              |
| ------------------ | -------------- | ----------------------------------------------- |
| `nodeId`           | `integer`      | 解析得到的元素节点的 DevTool 节点 ID。                       |
| `tag`              | `string`       | 运行时元素的标签名，例如 `view`、`text`、`image` 或 `wrapper`。 |
| `nodeIndex`        | `integer`      | 存储在解析所得元素节点上的原始节点索引。                            |
| `debugMetadataUrl` | `string`       | 该元素节点所属模板包的调试元数据 URL。所属模板包未提供调试元数据时为空。          |
| `lazyBundleUrl`    | `string\|null` | 节点属于懒加载包时，为该懒加载包的 URL。节点来自主模板时，此字段为 `null`。     |

###### 备注

- 通过 LynxDevToolMediator 分发至 TASM 线程上的 InspectorTasmExecutor。
- 运行时生成的包装节点可通过值为 `wrapper` 的 `tag` 字段识别。
- 未找到 `params.nodeId` 对应的元素时，响应的 result 为空对象。
- 如果 mediator 没有 TASM 任务执行器，当前处理函数不会发送显式的 CDP 响应。

###### 示例

**读取元素节点的源码信息**

请求：

```json
{
  "id": 1,
  "method": "DOM.getOriginalNodeSourceInfo",
  "params": {
    "nodeId": 42
  }
}
```

响应：

```json
{
  "id": 1,
  "result": {
    "nodeId": 42,
    "tag": "view",
    "nodeIndex": 7,
    "debugMetadataUrl": "https://example.com/main.debug-metadata.json",
    "lazyBundleUrl": null
  }
}
```

### `DOM.innerText`

Returns raw text child values for a Lynx text node.

- Origin: `lynx-extension`
- Source: `devtool/lynx_devtool/agent/domain_agent/inspector_dom_agent_ng.cc`

###### 参数

| Name     | Type      | Required | Description                 |
| -------- | --------- | -------- | --------------------------- |
| `nodeId` | `integer` | Yes      | DevTool node id to inspect. |

###### 返回字段

| Name                     | Type      | Description                                                                                                                                                                    |
| ------------------------ | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `nodeId`                 | `integer` | The node id from the request.                                                                                                                                                  |
| `rawTextValues`          | `array`   | Raw text child entries collected from the requested text node. Empty when the node is missing, detached, not a text element, or has no raw-text children with text attributes. |
| `rawTextValues[].nodeId` | `integer` | DevTool node id of the raw-text child.                                                                                                                                         |
| `rawTextValues[].text`   | `string`  | Text attribute value from the raw-text child.                                                                                                                                  |

###### 备注

- Dispatches through LynxDevToolMediator to InspectorTasmExecutor on the TASM thread.
- The handler only scans attached elements whose localName is text.
- For matching text elements, the handler walks direct raw-text children and reads their text attributes.
- The method does not concatenate text; callers receive one rawTextValues item per raw-text child that contributes text.
- If the mediator has no TASM task runner, the current handler does not send an explicit CDP response.

###### 示例

**Read raw text children**

Request:

```json
{
  "id": 1,
  "method": "DOM.innerText",
  "params": {
    "nodeId": 42
  }
}
```

Response:

```json
{
  "id": 1,
  "result": {
    "nodeId": 42,
    "rawTextValues": [
      {
        "nodeId": 43,
        "text": "Hello Lynx"
      }
    ]
  }
}
```

