跳到主要内容

Target

支持额外目标发现,并允许附加到这些目标。

EN
Supports additional targets discovery and allows to attach to them.

✅️️ Methods

🔨Target.activateTarget

激活(聚焦)目标。

EN
Activates (focuses) the target.
参数必填标记类型描述原文
targetId✔️TargetID

🔨Target.attachToTarget

附加到具有给定ID的目标。

EN
Attaches to the target with given id.
参数必填标记类型描述原文
targetId✔️TargetID
flattenboolean通过在命令中指定sessionId属性,启用对会话的"扁平化"访问。我们计划将此设为默认方式,弃用非扁平化模式,并最终将其淘汰。详情参见crbug.com/991325。
ENEnables "flat" access to the session via specifying sessionId attribute in the commands. We plan to make this the default, deprecate non-flattened mode, and eventually retire it. See crbug.com/991325.
返回对象类型描述原文
sessionIdSessionID分配给会话的Id。
ENId assigned to the session.

🔨Target.closeTarget

关闭目标。如果目标是页面,该页面也会被关闭。

EN
Closes the target. If the target is a page that gets closed too.
参数必填标记类型描述原文
targetId✔️TargetID
返回对象类型描述原文
successboolean始终设置为true。如果发生错误,响应将指示协议错误。
ENAlways set to true. If an error occurs, the response indicates protocol error.

🔨Target.createBrowserContext

创建一个新的空浏览器上下文。类似于无痕浏览模式,但您可以创建多个。

EN
Creates a new empty BrowserContext. Similar to an incognito profile but you can have more than one.
参数必填标记类型描述原文
disposeOnDetach🧪boolean如果指定,则在调试会话断开时释放此上下文。
ENIf specified, disposes this context when debugging session disconnects.
proxyServer🧪string代理服务器,类似于传递给 --proxy-server 的参数
ENProxy server, similar to the one passed to --proxy-server
proxyBypassList🧪string代理绕过列表,类似于传递给 --proxy-bypass-list 的列表
ENProxy bypass list, similar to the one passed to --proxy-bypass-list
originsWithUniversalNetworkAccess🧪array[string]授予无限跨域访问权限的可选来源列表。URL中除构成来源部分外的其他部分将被忽略。
ENAn optional list of origins to grant unlimited cross-origin access to. Parts of the URL other than those constituting origin are ignored.
返回对象类型描述原文
browserContextIdBrowser.BrowserContextID所创建上下文的id。
ENThe id of the context created.

🔨Target.createTarget

创建一个新页面。

EN
Creates a new page.
参数必填标记类型描述原文
url✔️string页面初始导航的URL。空字符串表示about:blank
ENThe initial URL the page will be navigated to. An empty string indicates about:blank.
left🧪integerFrame left origin in DIP (requires newWindow to be true or headless shell).
ENFrame left origin in DIP (requires newWindow to be true or headless shell).
top🧪integerFrame top origin in DIP (requires newWindow to be true or headless shell).
ENFrame top origin in DIP (requires newWindow to be true or headless shell).
widthinteger帧宽度,以DIP为单位(需要newWindow为true或使用headless shell)。
ENFrame width in DIP (requires newWindow to be true or headless shell).
heightinteger帧高度,单位为DIP(需要将newWindow设置为true或使用headless shell)。
ENFrame height in DIP (requires newWindow to be true or headless shell).
windowStateWindowState框架窗口状态(要求 newWindowtrue 或处于无头模式)。默认为 normal
ENFrame window state (requires newWindow to be true or headless shell). Default is normal.
browserContextId🧪Browser.BrowserContextID在指定的浏览器上下文中创建页面。
ENThe browser context to create the page in.
enableBeginFrameControl🧪boolean此目标是否将通过DevTools控制BeginFrames(仅限无头模式,MacOS暂不支持,默认值为false)。
ENWhether BeginFrames for this target will be controlled via DevTools (headless shell only, not supported on MacOS yet, false by default).
newWindowboolean是否创建新窗口或标签页(默认为 false,无头 shell 不支持)。
ENWhether to create a new Window or Tab (false by default, not supported by headless shell).
backgroundboolean是否在后台或前台创建目标(默认为false,无头shell不支持)。
ENWhether to create the target in background or foreground (false by default, not supported by headless shell).
forTab🧪boolean是否创建类型为"tab"的目标。
ENWhether to create the target of type "tab".
hidden🧪boolean是否创建隐藏目标。隐藏目标可通过协议观察到,但不会出现在标签页UI条中。不能与 forTab: truenewWindow: truebackground: false 同时使用。标签页的生命周期仅限于会话的生命周期。
ENWhether to create a hidden target. The hidden target is observable via protocol, but not present in the tab UI strip. Cannot be created with forTab: true, newWindow: true or background: false. The life-time of the tab is limited to the life-time of the session.
focus🧪boolean如果指定,该选项用于决定是否聚焦新目标。默认情况下,聚焦行为取决于background字段的值。例如,background=falsefocus=false将导致目标标签页被打开但浏览器窗口保持不变(如果窗口原本处于后台,则保持后台状态),而background=falsefocus=undefined将导致窗口获得焦点。不支持同时使用background: truefocus: true,否则将引发错误。
ENIf specified, the option is used to determine if the new target should be focused or not. By default, the focus behavior depends on the value of the background field. For example, background=false and focus=false will result in the target tab being opened but the browser window remain unchanged (if it was in the background, it will remain in the background) and background=false with focus=undefined will result in the window being focused. Using background: true and focus: true is not supported and will result in an error.
返回对象类型描述原文
targetIdTargetID打开的页面的id。
ENThe id of the page opened.

🔨Target.detachFromTarget

分离具有给定ID的会话。

EN
Detaches session with given id.
参数必填标记类型描述原文
sessionIdSessionID要分离的会话。
ENSession to detach.
targetId❌️TargetID已弃用。
ENDeprecated.

🔨Target.disposeBrowserContext

删除一个浏览器上下文。所有属于该上下文的页面将被关闭,且不会调用它们的 beforeunload 钩子。

EN
Deletes a BrowserContext. All the belonging pages will be closed without calling their beforeunload hooks.
参数必填标记类型描述原文
browserContextId✔️Browser.BrowserContextID

🔨Target.getBrowserContexts

返回所有使用Target.createBrowserContext方法创建的浏览器上下文。

EN
Returns all browser contexts created with Target.createBrowserContext method.
返回对象类型描述原文
browserContextIdsarray[Browser.BrowserContextID]一组浏览器上下文ID。
ENAn array of browser context ids.
defaultBrowserContextIdBrowser.BrowserContextID如果可用,默认浏览器上下文的id。
ENThe id of the default browser context if available.

🔨Target.getTargets

获取可用目标列表。

EN
Retrieves a list of available targets.
参数必填标记类型描述原文
filter🧪TargetFilter仅匹配筛选器的目标会被报告。如果未指定筛选器且当前启用了目标发现,则会使用用于目标发现的一致性筛选器。
ENOnly targets matching filter will be reported. If filter is not specified and target discovery is currently enabled, a filter used for target discovery is used for consistency.
返回对象类型描述原文
targetInfosarray[TargetInfo]目标列表。
ENThe list of targets.

🔨Target.setAutoAttach

控制是否自动附加到被认为与此目标直接相关的新目标(例如,iframe或worker)。开启时,也会附加到所有现有的相关目标。关闭时,会自动从所有当前附加的目标中分离。这还会从监视相关目标创建的列表中清除所有由autoAttachRelated添加的目标。你可能希望对自动附加的目标递归调用此方法,以附加到所有可用目标。

EN
Controls whether to automatically attach to new targets which are considered to be directly related to this one (for example, iframes or workers). When turned on, attaches to all existing related targets as well. When turned off, automatically detaches from all currently attached targets. This also clears all targets added by autoAttachRelated from the list of targets to watch for creation of related targets. You might want to call this recursively for auto-attached targets to attach to all available targets.
参数必填标记类型描述原文
autoAttach✔️boolean是否自动附加到相关目标。
ENWhether to auto-attach to related targets.
waitForDebuggerOnStart✔️boolean是否在附加到新目标时暂停它们。使用 Runtime.runIfWaitingForDebugger 来运行已暂停的目标。
ENWhether to pause new targets when attaching to them. Use Runtime.runIfWaitingForDebugger to run paused targets.
flatten🧪boolean通过在命令中指定sessionId属性,实现对会话的"扁平化"访问。我们计划将此设为默认模式,弃用非扁平化模式,并最终将其移除。详情参见crbug.com/991325。
ENEnables "flat" access to the session via specifying sessionId attribute in the commands. We plan to make this the default, deprecate non-flattened mode, and eventually retire it. See crbug.com/991325.
filter🧪TargetFilter只有符合筛选条件的目标才会被附加。
ENOnly targets matching filter will be attached.

🔨Target.setDiscoverTargets

控制是否发现可用目标并通过targetCreated/targetInfoChanged/targetDestroyed事件进行通知。

EN
Controls whether to discover available targets and notify via targetCreated/targetInfoChanged/targetDestroyed events.
参数必填标记类型描述原文
discover✔️boolean是否发现可用目标。
ENWhether to discover available targets.
filter🧪TargetFilter只有符合筛选条件的目标才会被附加。如果 discover 为 false,则必须省略 filter 或将其置空。
ENOnly targets matching filter will be attached. If discover is false, filter must be omitted or empty.

🔨Target.sendMessageToTarget❌️

通过指定会话ID发送协议消息。建议改用扁平模式;请参阅命令attachToTarget、setAutoAttach以及crbug.com/991325。

EN
Sends protocol message over session with given id. Consider using flat mode instead; see commands attachToTarget, setAutoAttach, and crbug.com/991325.
参数必填标记类型描述原文
message✔️string
sessionIdSessionID会话的标识符。
ENIdentifier of the session.
targetId❌️TargetID已弃用。
ENDeprecated.

🔨Target.attachToBrowserTarget🧪

附加到浏览器目标,仅使用扁平化会话ID模式。

EN
Attaches to the browser target, only uses flat sessionId mode.
返回对象类型描述原文
sessionIdSessionID分配给会话的ID。
ENId assigned to the session.

🔨Target.autoAttachRelated🧪

将指定的目标添加到监视列表中,该列表会监控任何相关目标的创建(例如子框架、子工作线程和服务工作线程的新版本),并通过 attachedToTarget 事件进行报告。指定的目标也会被自动附加。此操作会取消之前任何 setAutoAttach 的效果,并且也会被后续的 setAutoAttach 取消。仅适用于浏览器目标。

EN
Adds the specified target to the list of targets that will be monitored for any related target creation (such as child frames, child workers and new versions of service worker) and reported through attachedToTarget. The specified target is also auto-attached. This cancels the effect of any previous setAutoAttach and is also cancelled by subsequent setAutoAttach. Only available at the Browser target.
参数必填标记类型描述原文
targetId✔️TargetID
waitForDebuggerOnStart✔️boolean是否在附加到新目标时暂停它们。使用 Runtime.runIfWaitingForDebugger 来运行已暂停的目标。
ENWhether to pause new targets when attaching to them. Use Runtime.runIfWaitingForDebugger to run paused targets.
filter🧪TargetFilter仅匹配筛选器的目标会被附加。
ENOnly targets matching filter will be attached.

🔨Target.exposeDevToolsProtocol🧪

向目标主框架注入一个对象,该对象提供与浏览器目标的通信通道。
注入的对象将作为 window[bindingName] 提供。
该对象具有以下 API:
- binding.send(json) - 通过远程调试协议发送消息的方法
- binding.onmessage = json => handleMessage(json) - 用于处理协议通知和命令响应的回调函数。

EN
Inject object to the target's main frame that provides a communication channel with browser target.
Injected object will be available as window[bindingName].
The object has the following API:
- binding.send(json) - a method to send messages over the remote debugging protocol
- binding.onmessage = json => handleMessage(json) - a callback that will be called for the protocol notifications and command responses.
参数必填标记类型描述原文
targetId✔️TargetID
bindingNamestring绑定名称,如果未指定,则为 'cdp'。
ENBinding name, 'cdp' if not specified.
inheritPermissionsboolean如果为真,则继承当前根会话的权限(默认值:false)。
ENIf true, inherits the current root session's permissions (default: false).

🔨Target.getDevToolsTarget🧪

获取为指定目标(如果存在)打开的DevTools页面目标的targetId。

EN
Gets the targetId of the DevTools page target opened for the given target (if any).
参数必填标记类型描述原文
targetId✔️TargetID页面或标签页目标ID。
ENPage or tab target ID.
返回对象类型描述原文
targetIdTargetIDDevTools页面目标的targetId(如果存在)。
ENThe targetId of DevTools page target if exists.

🔨Target.getTargetInfo🧪

返回有关目标的信息。

EN
Returns information about a target.
参数必填标记类型描述原文
targetIdTargetID
返回对象类型描述原文
targetInfoTargetInfo

🔨Target.openDevTools🧪

为目标打开一个DevTools窗口。

EN
Opens a DevTools window for the target.
参数必填标记类型描述原文
targetId✔️TargetID这可以是页面或标签页的目标ID。
ENThis can be the page or tab target ID.
panelIdstring我们希望DevTools初始打开的调试面板ID。当前支持的调试面板包括''elements''、''console''、''network''、''sources''、''resources''和''performance''。
ENThe id of the panel we want DevTools to open initially. Currently supported panels are elements, console, network, sources, resources and performance.
返回对象类型描述原文
targetIdTargetIDDevTools 页面目标的 targetId。
ENThe targetId of DevTools page target.

🔨Target.setRemoteLocations🧪

setDiscoverTargets被设置为true时,为指定位置启用目标发现。

EN
Enables target discovery for the specified locations, when setDiscoverTargets was set to true.
参数必填标记类型描述原文
locations✔️array[RemoteLocation]远程位置列表。
ENList of remote locations.

✅️️ Events

👋Target.receivedMessageFromTarget

通知收到来自会话的新协议消息(如attachedToTarget事件中所述)。

EN
Notifies about a new protocol message received from the session (as reported in attachedToTarget event).
参数必填标记类型描述原文
sessionId✔️SessionID发送消息的会话标识符。
ENIdentifier of a session which sends a message.
message✔️string
targetId❌️TargetID已弃用。
ENDeprecated.

👋Target.targetCrashed

当目标崩溃时发出。

EN
Issued when a target has crashed.
参数必填标记类型描述原文
targetId✔️TargetID
status✔️string终止状态类型。
ENTermination status type.
errorCode✔️integer终止错误代码。
ENTermination error code.

👋Target.targetCreated

当一个可能的检查目标被创建时发出。

EN
Issued when a possible inspection target is created.
参数必填标记类型描述原文
targetInfo✔️TargetInfo

👋Target.targetDestroyed

当目标被销毁时发出。

EN
Issued when a target is destroyed.
参数必填标记类型描述原文
targetId✔️TargetID

👋Target.targetInfoChanged

当某个目标的信息发生更改时发出。这仅在 targetCreatedtargetDestroyed 之间发生。

EN
Issued when some information about a target has changed. This only happens between targetCreated and targetDestroyed.
参数必填标记类型描述原文
targetInfo✔️TargetInfo

👋Target.attachedToTarget🧪

当因自动附加或attachToTarget命令附加到目标时发出。

EN
Issued when attached to target because of auto-attach or attachToTarget command.
参数必填标记类型描述原文
sessionId✔️SessionID分配给用于发送/接收消息的会话的标识符。
ENIdentifier assigned to the session used to send/receive messages.
targetInfo✔️TargetInfo
waitingForDebugger✔️boolean

👋Target.detachedFromTarget🧪

当因任何原因(包括执行detachFromTarget命令)与目标分离时发出。如果多个会话已附加到同一目标,则每个目标可能会多次发出此通知。

EN
Issued when detached from target for any reason (including detachFromTarget command). Can be issued multiple times per target if multiple sessions have been attached to it.
参数必填标记类型描述原文
sessionId✔️SessionID分离的会话标识符。
ENDetached session identifier.
targetId❌️TargetID已弃用。
ENDeprecated.

✅️️ Types

📌Target.SessionID

附加调试会话的唯一标识符。

EN
Unique identifier of attached debugging session.

返回类型:string


📌Target.TargetID

返回类型:string


📌Target.TargetInfo

返回类型:object

属性必填标记类型描述原文
targetId✔️TargetID
type✔️string类型列表:https://source.chromium.org/chromium/chromium/src/+/main:content/browser/devtools/devtools_agent_host_impl.cc?ss=chromium&q=f:devtools%20-f:out%20%22::kTypeTab%5B%5D%22
ENList of types: https://source.chromium.org/chromium/chromium/src/+/main:content/browser/devtools/devtools_agent_host_impl.cc?ss=chromium&q=f:devtools%20-f:out%20%22::kTypeTab%5B%5D%22
title✔️string
url✔️string
attached✔️boolean目标是否已附加客户端。
ENWhether the target has an attached client.
openerIdTargetIDOpener target Id
ENOpener target Id
canAccessOpener✔️🧪boolean目标是否能够访问原始窗口。
ENWhether the target has access to the originating window.
openerFrameId🧪Page.FrameId源窗口的帧ID(仅在目标窗口有打开者时设置)。
ENFrame id of originating window (is only set if target has an opener).
parentFrameId🧪Page.FrameId父框架的ID,仅存在于"iframe"目标中。
ENId of the parent frame, only present for the "iframe" targets.
browserContextId🧪Browser.BrowserContextID
subtype🧪string为特定目标类型提供额外详细信息。例如,对于"page"类型,此值可能被设置为"prerender"。
ENProvides additional details for specific target types. For example, for the type of "page", this may be set to "prerender".

📌Target.FilterEntry🧪

用于目标查询/发现/自动附加操作的过滤器。

EN
A filter used by target query/discovery/auto-attach operations.

返回类型:object

属性必填标记类型描述原文
excludeboolean如果设置,将导致从列表中排除匹配的目标。
ENIf set, causes exclusion of matching targets from the list.
typestring如果不存在,则匹配任何类型。
ENIf not present, matches any type.

📌Target.RemoteLocation🧪

返回类型:object

属性必填标记类型描述原文
host✔️string
port✔️integer

📌Target.TargetFilter🧪

TargetFilter 中的条目会按顺序与目标进行匹配,第一个匹配到的条目将决定目标是否被包含,这取决于该条目中 exclude 字段的值。如果未指定过滤器,则默认采用 [{type: "browser", exclude: true}, {type: "tab", exclude: true}, {}](即包含除 browsertab 之外的所有内容)。

EN
The entries in TargetFilter are matched sequentially against targets and the first entry that matches determines if the target is included or not, depending on the value of exclude field in the entry. If filter is not specified, the one assumed is [{type: "browser", exclude: true}, {type: "tab", exclude: true}, {}] (i.e. include everything but browser and tab).

返回类型:array


📌Target.WindowState🧪

目标窗口的状态。
允许的取值:normalminimizedmaximizedfullscreen

EN
The state of the target window.
Allowed Values: normal, minimized, maximized, fullscreen

返回类型:string