Class: Ferrum::Target
- Inherits:
-
Object
- Object
- Ferrum::Target
- Defined in:
- lib/ferrum/target.rb,
sig/ferrum/target.rbs
Overview
Represents a CDP target, e.g. a page, iframe, dedicated/shared worker or
service worker. Wraps the raw targetInfo params and lazily connects to
the target as a Page or Worker over its own Client/SessionClient
session, so a Target can exist without being connected to yet.
Constant Summary collapse
- NEW_WINDOW_WAIT =
ENV.fetch("FERRUM_NEW_WINDOW_WAIT", 0.3).to_f
Instance Attribute Summary collapse
-
#options ⇒ Browser::Options
readonly
Returns the value of attribute options.
- #session_id ⇒ String?
Instance Method Summary collapse
-
#build_page(**options) ⇒ Page
private
Builds this target's Page, and passes it to the blocks registered with
on_pagebefore returning it. -
#build_worker ⇒ Worker
private
Builds a Worker for this target.
-
#client ⇒ SessionClient
The client connected to this target's own session.
-
#command(method, timeout: nil, **params) ⇒ Hash
private
Sends a CDP command through #client.
- #connected? ⇒ Boolean
-
#context_id ⇒ String?
The id of the browser context this target belongs to,
nilfor the browser's implicit context, which Chrome doesn't let us address by id, see Context#implicit?. -
#id ⇒ String
The target's id.
-
#iframe? ⇒ Boolean
Whether this target is an iframe.
-
#initialize(browser_client, session_id = nil, params = nil, implicit: false) ⇒ Target
constructor
A new instance of Target.
-
#maybe_sleep_if_new_window ⇒ void
private
Chrome fires no events for a newly opened window, so we sleep a bit to give it a chance to load before connecting.
-
#opener_id ⇒ String?
The id of the target that opened this one, set only for windows/tabs opened via
window.open/links/etc. -
#page ⇒ Page
Connects to and returns this target's Page.
-
#page? ⇒ Boolean
Whether this target is a page.
-
#parent_id ⇒ String?
The id of the target that spawned this one, set for iframes and workers.
-
#release_waiters ⇒ void
private
Releases what waits on the target's page or worker, Chrome has detached, destroyed or lost it.
-
#service_worker? ⇒ Boolean
Whether this target is a service worker.
-
#shared_worker? ⇒ Boolean
Whether this target is a shared worker.
-
#title ⇒ String
The target's title.
-
#type ⇒ String
The target's type, e.g.
-
#update(params) ⇒ Hash
private
Merges freshly received CDP target info into this target's params, e.g.
-
#url ⇒ String
The target's URL.
-
#worker ⇒ Worker
Connects to and returns this target's Worker.
-
#worker? ⇒ Boolean
Whether this target is a dedicated worker.
Constructor Details
#initialize(browser_client, session_id = nil, params = nil, implicit: false) ⇒ Target
Returns a new instance of Target.
21 22 23 24 25 26 27 28 29 |
# File 'lib/ferrum/target.rb', line 21 def initialize(browser_client, session_id = nil, params = nil, implicit: false) @page = nil @worker = nil @session_id = session_id @params = params @implicit = implicit @browser_client = browser_client @options = browser_client. end |
Instance Attribute Details
#options ⇒ Browser::Options (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/ferrum/target.rb', line 13 def @options end |
#session_id ⇒ String?
The writer is a private API, Ferrum sets it.
16 17 18 |
# File 'lib/ferrum/target.rb', line 16 def session_id @session_id end |
Instance Method Details
#build_page(**options) ⇒ Page
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds this target's Page, and passes it to the blocks registered
with on_page before returning it. Called lazily by page, or
eagerly by Context#create_page.
99 100 101 102 103 104 |
# File 'lib/ferrum/target.rb', line 99 def build_page(**) maybe_sleep_if_new_window @page = Page.new(client, context_id: context_id, target_id: id, **) @options.page_handlers.each { |handler| handler.call(@page) } @page end |
#build_worker ⇒ Worker
111 112 113 114 115 116 117 |
# File 'lib/ferrum/target.rb', line 111 def build_worker if session_id.nil? raise NoSuchTargetError, "No session with #{type} #{id}, attach to it with Context#attach_target first" end Worker.new(client, target_id: id, url: url, type: type) end |
#client ⇒ SessionClient
The client connected to this target's own session.
86 87 88 |
# File 'lib/ferrum/target.rb', line 86 def client @client ||= build_client end |
#command(method, timeout: nil, **params) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sends a CDP command through #client.
236 237 238 |
# File 'lib/ferrum/target.rb', line 236 def command(method, timeout: nil, **params) client.command(method, timeout: timeout || client.timeout, **params) end |
#connected? ⇒ Boolean
47 48 49 |
# File 'lib/ferrum/target.rb', line 47 def connected? !!@page || !!@worker end |
#context_id ⇒ String?
The id of the browser context this target belongs to, nil for the
browser's implicit context, which Chrome doesn't let us address by id,
see Context#implicit?. Commands scoped to a browser context target it
by omitting browserContextId.
169 170 171 |
# File 'lib/ferrum/target.rb', line 169 def context_id @params["browserContextId"] unless @implicit end |
#id ⇒ String
The target's id.
122 123 124 |
# File 'lib/ferrum/target.rb', line 122 def id @params["targetId"] end |
#iframe? ⇒ Boolean
Whether this target is an iframe.
176 177 178 |
# File 'lib/ferrum/target.rb', line 176 def iframe? type == "iframe" end |
#maybe_sleep_if_new_window ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Chrome fires no events for a newly opened window, so we sleep a bit to give it a chance to load before connecting.
214 215 216 217 |
# File 'lib/ferrum/target.rb', line 214 def maybe_sleep_if_new_window # Dirty hack because new window doesn't have events at all sleep(NEW_WINDOW_WAIT) if opener_id end |
#opener_id ⇒ String?
The id of the target that opened this one, set only for
windows/tabs opened via window.open/links/etc.
152 153 154 |
# File 'lib/ferrum/target.rb', line 152 def opener_id @params["openerId"] end |
#page ⇒ Page
Connects to and returns this target's Page.
67 68 69 |
# File 'lib/ferrum/target.rb', line 67 def page @page || build_page end |
#page? ⇒ Boolean
Whether this target is a page.
183 184 185 |
# File 'lib/ferrum/target.rb', line 183 def page? type == "page" end |
#parent_id ⇒ String?
The id of the target that spawned this one, set for iframes and
workers. Unlike opener_id, which is only set for windows/tabs opened
via window.open/links/etc.
159 160 161 |
# File 'lib/ferrum/target.rb', line 159 def parent_id @params["parentId"] end |
#release_waiters ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Releases what waits on the target's page or worker, Chrome has detached, destroyed or lost it.
57 58 59 60 |
# File 'lib/ferrum/target.rb', line 57 def release_waiters @page&.release_waiters @worker&.release_waiters end |
#service_worker? ⇒ Boolean
Whether this target is a service worker.
204 205 206 |
# File 'lib/ferrum/target.rb', line 204 def service_worker? type == "service_worker" end |
#shared_worker? ⇒ Boolean
Whether this target is a shared worker.
197 198 199 |
# File 'lib/ferrum/target.rb', line 197 def shared_worker? type == "shared_worker" end |
#title ⇒ String
The target's title.
137 138 139 |
# File 'lib/ferrum/target.rb', line 137 def title @params["title"] end |
#type ⇒ String
The target's type, e.g. "page", "iframe", "worker",
"shared_worker", "service_worker".
130 131 132 |
# File 'lib/ferrum/target.rb', line 130 def type @params["type"] end |
#update(params) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Merges freshly received CDP target info into this target's params,
e.g. on Target.targetInfoChanged.
39 40 41 |
# File 'lib/ferrum/target.rb', line 39 def update(params) @params.merge!(params) end |
#url ⇒ String
The target's URL.
144 145 146 |
# File 'lib/ferrum/target.rb', line 144 def url @params["url"] end |
#worker ⇒ Worker
Connects to and returns this target's Worker.
79 80 81 |
# File 'lib/ferrum/target.rb', line 79 def worker @worker ||= build_worker end |
#worker? ⇒ Boolean
Whether this target is a dedicated worker.
190 191 192 |
# File 'lib/ferrum/target.rb', line 190 def worker? type == "worker" end |