Class: Obxcura::Page

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/obxcura/page.rb

Overview

A single page/tab: a CDP target with its own attached session. Created via Browser#create_page — you rarely instantiate this directly.

Page owns the CDP session, navigation, in-page POSTs and lifecycle. Reading the DOM and executing JS belong to its main Frame (see Frame::DOM and Frame::Runtime), and Page delegates those (#evaluate, #html, #title, #at_css, ...).

Obscura has no paint engine, so there is deliberately no screenshot API.

Examples:

page = browser.create_page
page.goto("https://example.com")
page.html                 # rendered DOM after JS
page.title                # "Example Domain"
page.at_css("h1").text    # => "Example Domain"
page.close

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser, target_id:, session_id:) ⇒ Page

Returns a new instance of Page.

Parameters:

  • browser (Obxcura::Browser)

    the owning browser.

  • target_id (String)

    the CDP target id.

  • session_id (String)

    the CDP session attached to the target.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/obxcura/page.rb', line 39

def initialize(browser, target_id:, session_id:)
  @browser = browser
  @client = browser.client
  @target_id = target_id
  @session_id = session_id
  @frame = Frame.new(target_id, self)
  @load_queue = Queue.new
  @network_log = []
  @network_mutex = Mutex.new

  @client.subscribe(@session_id) { |method, params| dispatch_event(method, params) }
end

Instance Attribute Details

#clientString, ... (readonly)

Returns:

  • (String)

    the CDP target id backing this page.

  • (String)

    the CDP session id attached to the target.

  • (Obxcura::Client)

    the shared CDP transport.

  • (Obxcura::Frame)

    the page's main frame.



30
31
32
# File 'lib/obxcura/page.rb', line 30

def client
  @client
end

#frameString, ... (readonly)

Returns:

  • (String)

    the CDP target id backing this page.

  • (String)

    the CDP session id attached to the target.

  • (Obxcura::Client)

    the shared CDP transport.

  • (Obxcura::Frame)

    the page's main frame.



30
31
32
# File 'lib/obxcura/page.rb', line 30

def frame
  @frame
end

#session_idString, ... (readonly)

Returns:

  • (String)

    the CDP target id backing this page.

  • (String)

    the CDP session id attached to the target.

  • (Obxcura::Client)

    the shared CDP transport.

  • (Obxcura::Frame)

    the page's main frame.



30
31
32
# File 'lib/obxcura/page.rb', line 30

def session_id
  @session_id
end

#target_idString, ... (readonly)

Returns:

  • (String)

    the CDP target id backing this page.

  • (String)

    the CDP session id attached to the target.

  • (Obxcura::Client)

    the shared CDP transport.

  • (Obxcura::Frame)

    the page's main frame.



30
31
32
# File 'lib/obxcura/page.rb', line 30

def target_id
  @target_id
end

Instance Method Details

#closeObxcura::Page?

Close this page's target and stop listening for its events.

Returns:



68
69
70
71
72
73
74
75
76
# File 'lib/obxcura/page.rb', line 68

def close
  @client.unsubscribe(@session_id)
  @client.command("Network.clearBrowserCookies", { targetId: @target_id })
  @client.command("Target.closeTarget", { targetId: @target_id })
rescue ProtocolError
  # Target already gone — nothing to do.
ensure
  @browser.remove_page(self)
end

#close_connectionvoid

This method returns an undefined value.

Drop the underlying WebSocket connection (affects the whole browser).



81
82
83
# File 'lib/obxcura/page.rb', line 81

def close_connection
  @client.close
end

#command(method, params = {}) ⇒ Hash

Send a CDP command scoped to this page's session.

Parameters:

  • method (String)

    the CDP method name.

  • params (Hash) (defaults to: {})

    the method parameters.

Returns:

  • (Hash)

    the command's result object.



104
105
106
# File 'lib/obxcura/page.rb', line 104

def command(method, params = {})
  @client.command(method, params, session_id: @session_id)
end

#cookiesArray<Hash>

Returns the page's cookies as CDP cookie hashes.

Returns:

  • (Array<Hash>)

    the page's cookies as CDP cookie hashes.



95
96
97
# File 'lib/obxcura/page.rb', line 95

def cookies
  command("Storage.getCookies")["cookies"]
end

#goto(url) ⇒ self Also known as: go_to

Navigate to url and block until the page's load event fires. Aliased as go_to.

Parameters:

  • url (String)

    the URL to navigate to.

Returns:

  • (self)


57
58
59
60
61
62
# File 'lib/obxcura/page.rb', line 57

def goto(url)
  @load_queue = Queue.new
  command("Page.navigate", url: url)
  wait_for_load
  self
end

#refreshHash Also known as: reload

Reload the page and block until it loads again. Aliased as reload.

Returns:

  • (Hash)

    the CDP Page.reload result.



88
89
90
91
# File 'lib/obxcura/page.rb', line 88

def refresh
  command("Page.reload")
  wait_for_load
end

#xhr_post(url, payload, content_type, headers, timeout: nil) ⇒ Hash

POST via XMLHttpRequest from the page context. Obscura routes XHR but not fetch, so this is the reliable POST path. All values cross as arguments, never interpolated into the JS. Returns { status, ok, body } on any HTTP reply (including 4xx/5xx). A transport failure — the request never reached the server (blocked by CORS / private-network SSRF guard, mixed origin, or a dead host) — raises ConnectionError instead of silently returning nil.

timeout (seconds) bounds how long we wait for the reply. If the server accepts the connection but never answers the XHR (some anti-bot endpoints tarpit non-stealth clients), the wait ends with a TimeoutError that points at the likely cause. Note: Obscura ignores XMLHttpRequest#timeout, so the effective bound is this CDP-level one.

Parameters:

  • url (String)

    the URL to POST to.

  • payload (String)

    the raw request body.

  • content_type (String)

    the Content-Type header value.

  • headers (Hash{String=>String})

    extra request headers.

  • timeout (Integer, nil) (defaults to: nil)

    seconds to wait for the reply.

Returns:

  • (Hash)

    { "status" => Integer, "ok" => Boolean, "body" => String }.

Raises:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/obxcura/page.rb', line 129

def xhr_post(url, payload, content_type, headers, timeout: nil)
  result = evaluate_func(<<~JS, url, payload, content_type, headers, timeout:)
    function(url, payload, contentType, headers) {
      return new Promise((resolve) => {
        const x = new XMLHttpRequest();
        x.onreadystatechange = () => {
          if (x.readyState === 4) {
            resolve({ status: x.status, ok: x.status >= 200 && x.status < 300, body: x.responseText });
          }
        };
        x.onerror = () => resolve({ error: "network error or request blocked (CORS / private network / mixed origin)" });
        try {
          x.open("POST", url, true);
          x.setRequestHeader("Content-Type", contentType);
          Object.keys(headers).forEach((k) => x.setRequestHeader(k, headers[k]));
          x.send(payload);
        } catch (e) {
          resolve({ error: String(e) });
        }
      });
    }
  JS

  if result.nil? || result["error"]
    reason = result&.dig("error") || "no response (request blocked or never settled)"
    raise ConnectionError, "POST #{url} failed: #{reason}"
  end

  result
rescue TimeoutError
  raise TimeoutError,
    "POST #{url} did not complete in time. The server accepted the connection " \
    "but never answered the XHR — likely anti-bot tarpitting. Try submitting the " \
    "real form with #type/#submit, run `obscura serve --stealth`, or pass a larger timeout:."
end