Packages

A browser automation tool and interface to the Chrome DevTools Protocol.

Current section

Files

Jump to
chrobot_extra src chrobot_extra protocol target.gleam
Raw

src/chrobot_extra/protocol/target.gleam

//// > ⚙️ This module was generated from the Chrome DevTools Protocol version **1.3**
//// ## Target Domain
////
//// Supports additional targets discovery and allows to attach to them.
////
//// [📖 View this domain on the DevTools Protocol API Docs](https://chromedevtools.github.io/devtools-protocol/1-3/Target/)
// ---------------------------------------------------------------------------
// | !!!!!! This is an autogenerated file - Do not edit manually !!!!!! |
// | Run `codegen.sh` to regenerate. |
// ---------------------------------------------------------------------------
import chrobot_extra/chrome
import chrobot_extra/internal/utils
import gleam/dynamic/decode
import gleam/json
import gleam/option
import gleam/result
pub type TargetID {
TargetID(String)
}
@internal
pub fn encode__target_id(value__: TargetID) {
case value__ {
TargetID(inner_value__) -> json.string(inner_value__)
}
}
@internal
pub fn decode__target_id() {
{
use value__ <- decode.then(decode.string)
decode.success(TargetID(value__))
}
}
/// Unique identifier of attached debugging session.
pub type SessionID {
SessionID(String)
}
@internal
pub fn encode__session_id(value__: SessionID) {
case value__ {
SessionID(inner_value__) -> json.string(inner_value__)
}
}
@internal
pub fn decode__session_id() {
{
use value__ <- decode.then(decode.string)
decode.success(SessionID(value__))
}
}
pub type TargetInfo {
TargetInfo(
target_id: TargetID,
/// List 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
type_: String,
title: String,
url: String,
/// Whether the target has an attached client.
attached: Bool,
/// Opener target Id
opener_id: option.Option(TargetID),
)
}
@internal
pub fn encode__target_info(value__: TargetInfo) {
json.object(
[
#("targetId", encode__target_id(value__.target_id)),
#("type", json.string(value__.type_)),
#("title", json.string(value__.title)),
#("url", json.string(value__.url)),
#("attached", json.bool(value__.attached)),
]
|> utils.add_optional(value__.opener_id, fn(inner_value__) {
#("openerId", encode__target_id(inner_value__))
}),
)
}
@internal
pub fn decode__target_info() {
{
use target_id <- decode.field("targetId", decode__target_id())
use type_ <- decode.field("type", decode.string)
use title <- decode.field("title", decode.string)
use url <- decode.field("url", decode.string)
use attached <- decode.field("attached", decode.bool)
use opener_id <- decode.optional_field(
"openerId",
option.None,
decode.optional(decode__target_id()),
)
decode.success(TargetInfo(
target_id: target_id,
type_: type_,
title: title,
url: url,
attached: attached,
opener_id: opener_id,
))
}
}
/// This type is not part of the protocol spec, it has been generated dynamically
/// to represent the response to the command `attach_to_target`
pub type AttachToTargetResponse {
AttachToTargetResponse(
/// Id assigned to the session.
session_id: SessionID,
)
}
@internal
pub fn decode__attach_to_target_response() {
{
use session_id <- decode.field("sessionId", decode__session_id())
decode.success(AttachToTargetResponse(session_id: session_id))
}
}
/// This type is not part of the protocol spec, it has been generated dynamically
/// to represent the response to the command `create_browser_context`
pub type CreateBrowserContextResponse {
CreateBrowserContextResponse(
/// The id of the context created.
browser_context_id: String,
)
}
@internal
pub fn decode__create_browser_context_response() {
{
use browser_context_id <- decode.field("browserContextId", decode.string)
decode.success(CreateBrowserContextResponse(
browser_context_id: browser_context_id,
))
}
}
/// This type is not part of the protocol spec, it has been generated dynamically
/// to represent the response to the command `get_browser_contexts`
pub type GetBrowserContextsResponse {
GetBrowserContextsResponse(
/// An array of browser context ids.
browser_context_ids: List(String),
)
}
@internal
pub fn decode__get_browser_contexts_response() {
{
use browser_context_ids <- decode.field(
"browserContextIds",
decode.list(decode.string),
)
decode.success(GetBrowserContextsResponse(
browser_context_ids: browser_context_ids,
))
}
}
/// This type is not part of the protocol spec, it has been generated dynamically
/// to represent the response to the command `create_target`
pub type CreateTargetResponse {
CreateTargetResponse(
/// The id of the page opened.
target_id: TargetID,
)
}
@internal
pub fn decode__create_target_response() {
{
use target_id <- decode.field("targetId", decode__target_id())
decode.success(CreateTargetResponse(target_id: target_id))
}
}
/// This type is not part of the protocol spec, it has been generated dynamically
/// to represent the response to the command `get_targets`
pub type GetTargetsResponse {
GetTargetsResponse(
/// The list of targets.
target_infos: List(TargetInfo),
)
}
@internal
pub fn decode__get_targets_response() {
{
use target_infos <- decode.field(
"targetInfos",
decode.list(decode__target_info()),
)
decode.success(GetTargetsResponse(target_infos: target_infos))
}
}
/// Activates (focuses) the target.
///
/// Parameters:
/// - `target_id`
///
/// Returns:
///
pub fn activate_target(callback__, target_id target_id: TargetID) {
callback__(
"Target.activateTarget",
option.Some(
json.object([
#("targetId", encode__target_id(target_id)),
]),
),
)
}
/// Attaches to the target with given id.
///
/// Parameters:
/// - `target_id`
/// - `flatten` : Enables "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.
///
/// Returns:
/// - `session_id` : Id assigned to the session.
///
pub fn attach_to_target(
callback__,
target_id target_id: TargetID,
flatten flatten: option.Option(Bool),
) {
use result__ <- result.try(callback__(
"Target.attachToTarget",
option.Some(json.object(
[
#("targetId", encode__target_id(target_id)),
]
|> utils.add_optional(flatten, fn(inner_value__) {
#("flatten", json.bool(inner_value__))
}),
)),
))
decode.run(result__, decode__attach_to_target_response())
|> result.replace_error(chrome.ProtocolError)
}
/// Closes the target. If the target is a page that gets closed too.
///
/// Parameters:
/// - `target_id`
///
/// Returns:
///
pub fn close_target(callback__, target_id target_id: TargetID) {
callback__(
"Target.closeTarget",
option.Some(
json.object([
#("targetId", encode__target_id(target_id)),
]),
),
)
}
/// Creates a new empty BrowserContext. Similar to an incognito profile but you can have more than
/// one.
///
/// Parameters:
///
/// Returns:
/// - `browser_context_id` : The id of the context created.
///
pub fn create_browser_context(callback__) {
use result__ <- result.try(callback__(
"Target.createBrowserContext",
option.None,
))
decode.run(result__, decode__create_browser_context_response())
|> result.replace_error(chrome.ProtocolError)
}
/// Returns all browser contexts created with `Target.createBrowserContext` method.
/// - `browser_context_ids` : An array of browser context ids.
///
pub fn get_browser_contexts(callback__) {
use result__ <- result.try(callback__(
"Target.getBrowserContexts",
option.None,
))
decode.run(result__, decode__get_browser_contexts_response())
|> result.replace_error(chrome.ProtocolError)
}
/// Creates a new page.
///
/// Parameters:
/// - `url` : The initial URL the page will be navigated to. An empty string indicates about:blank.
/// - `width` : Frame width in DIP (headless chrome only).
/// - `height` : Frame height in DIP (headless chrome only).
/// - `new_window` : Whether to create a new Window or Tab (chrome-only, false by default).
/// - `background` : Whether to create the target in background or foreground (chrome-only,
/// false by default).
///
/// Returns:
/// - `target_id` : The id of the page opened.
///
pub fn create_target(
callback__,
url url: String,
width width: option.Option(Int),
height height: option.Option(Int),
new_window new_window: option.Option(Bool),
background background: option.Option(Bool),
) {
use result__ <- result.try(callback__(
"Target.createTarget",
option.Some(json.object(
[
#("url", json.string(url)),
]
|> utils.add_optional(width, fn(inner_value__) {
#("width", json.int(inner_value__))
})
|> utils.add_optional(height, fn(inner_value__) {
#("height", json.int(inner_value__))
})
|> utils.add_optional(new_window, fn(inner_value__) {
#("newWindow", json.bool(inner_value__))
})
|> utils.add_optional(background, fn(inner_value__) {
#("background", json.bool(inner_value__))
}),
)),
))
decode.run(result__, decode__create_target_response())
|> result.replace_error(chrome.ProtocolError)
}
/// Detaches session with given id.
///
/// Parameters:
/// - `session_id` : Session to detach.
///
/// Returns:
///
pub fn detach_from_target(
callback__,
session_id session_id: option.Option(SessionID),
) {
callback__(
"Target.detachFromTarget",
option.Some(json.object(
[]
|> utils.add_optional(session_id, fn(inner_value__) {
#("sessionId", encode__session_id(inner_value__))
}),
)),
)
}
/// Deletes a BrowserContext. All the belonging pages will be closed without calling their
/// beforeunload hooks.
///
/// Parameters:
/// - `browser_context_id`
///
/// Returns:
///
pub fn dispose_browser_context(
callback__,
browser_context_id browser_context_id: String,
) {
callback__(
"Target.disposeBrowserContext",
option.Some(
json.object([
#("browserContextId", json.string(browser_context_id)),
]),
),
)
}
/// Retrieves a list of available targets.
///
/// Parameters:
///
/// Returns:
/// - `target_infos` : The list of targets.
///
pub fn get_targets(callback__) {
use result__ <- result.try(callback__("Target.getTargets", option.None))
decode.run(result__, decode__get_targets_response())
|> result.replace_error(chrome.ProtocolError)
}
/// Controls whether to automatically attach to new targets which are considered to be related to
/// this one. 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.
///
/// Parameters:
/// - `auto_attach` : Whether to auto-attach to related targets.
/// - `wait_for_debugger_on_start` : Whether to pause new targets when attaching to them. Use `Runtime.runIfWaitingForDebugger`
/// to run paused targets.
///
/// Returns:
///
pub fn set_auto_attach(
callback__,
auto_attach auto_attach: Bool,
wait_for_debugger_on_start wait_for_debugger_on_start: Bool,
) {
callback__(
"Target.setAutoAttach",
option.Some(
json.object([
#("autoAttach", json.bool(auto_attach)),
#("waitForDebuggerOnStart", json.bool(wait_for_debugger_on_start)),
]),
),
)
}
/// Controls whether to discover available targets and notify via
/// `targetCreated/targetInfoChanged/targetDestroyed` events.
///
/// Parameters:
/// - `discover` : Whether to discover available targets.
///
/// Returns:
///
pub fn set_discover_targets(callback__, discover discover: Bool) {
callback__(
"Target.setDiscoverTargets",
option.Some(
json.object([
#("discover", json.bool(discover)),
]),
),
)
}