Class: Bidi2pdf::Bidi::UserContext

Inherits:
Object
  • Object
show all
Defined in:
lib/bidi2pdf/bidi/user_context.rb

Overview

Represents a user context for managing browser interactions and cookies using the Bidi2pdf library. This class provides methods for creating user contexts, setting cookies, and creating browser windows.

Examples:

Creating a user context

user_context = Bidi2pdf::Bidi::UserContext.new(client)

Setting a cookie

user_context.set_cookie(
  name: "session",
  value: "abc123",
  domain: "example.com",
  source_origin: "http://example.com"
)

Creating a browser window

browser_window = user_context.create_browser_window

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ UserContext

Initializes a new user context.

Parameters:

  • client (Object)

    The WebSocket client for communication.



33
34
35
36
# File 'lib/bidi2pdf/bidi/user_context.rb', line 33

def initialize(client)
  @client = client
  @context_id = nil
end

Instance Attribute Details

#clientObject (readonly)

Returns The WebSocket client.

Returns:

  • (Object)

    The WebSocket client.



28
29
30
# File 'lib/bidi2pdf/bidi/user_context.rb', line 28

def client
  @client
end

Instance Method Details

#closenil

Closes the user context.

This method removes the user context from the browser, effectively cleaning up any associated resources. If the user context does not exist, the method does nothing.

Returns:

  • (nil)

Raises:

  • (RuntimeError)

    If an error occurs while removing the user context.



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bidi2pdf/bidi/user_context.rb', line 116

def close
  return unless context_id

  res = client.send_cmd_and_wait(Bidi2pdf::Bidi::Commands::BrowserRemoveUserContext.new(user_context_id: context_id)) do |response|
    raise "Error removing user context: #{response.inspect}" if response["error"]

    response["result"]
  end

  Bidi2pdf.logger.debug "User context deleted: #{res.inspect}"

  @context_id = nil
end

#context_idString

Retrieves the user context ID, creating it if it does not exist.

Returns:

  • (String)

    The user context ID.

Raises:

  • (RuntimeError)

    If an error occurs while creating the user context.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bidi2pdf/bidi/user_context.rb', line 42

def context_id
  @context_id ||= begin
                    res = client.send_cmd_and_wait(Bidi2pdf::Bidi::Commands::BrowserCreateUserContext.new) do |response|
                      raise "Error creating user context: #{response.inspect}" if response["error"]

                      response["result"]["userContext"]
                    end

                    Bidi2pdf.logger.debug "User context created: #{res.inspect}"

                    res
                  end
end

#create_browser_windowBrowserTab

Creates a new browser window in the user context.

Returns:



99
100
101
102
103
104
105
106
107
# File 'lib/bidi2pdf/bidi/user_context.rb', line 99

def create_browser_window
  cmd = Bidi2pdf::Bidi::Commands::CreateWindow.new(user_context_id: context_id)

  client.send_cmd_and_wait(cmd) do |response|
    browsing_context_id = response["result"]["context"]

    BrowserTab.new(client, browsing_context_id, context_id)
  end
end

Sets a cookie in the user context.

Parameters:

  • name (String)

    The name of the cookie.

  • value (String)

    The value of the cookie.

  • domain (String)

    The domain for the cookie.

  • source_origin (String)

    The source origin for the cookie.

  • path (String) (defaults to: "/")

    The path for the cookie. Defaults to “/”.

  • secure (Boolean) (defaults to: true)

    Whether the cookie is secure. Defaults to true.

  • http_only (Boolean) (defaults to: false)

    Whether the cookie is HTTP-only. Defaults to false.

  • same_site (String) (defaults to: "strict")

    The SameSite attribute for the cookie. Defaults to “strict”.

  • ttl (Integer) (defaults to: 30)

    The time-to-live for the cookie in seconds. Defaults to 30.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bidi2pdf/bidi/user_context.rb', line 67

def set_cookie(
  name:,
  value:,
  domain:,
  source_origin:,
  path: "/",
  secure: true,
  http_only: false,
  same_site: "strict",
  ttl: 30
)
  cmd = Bidi2pdf::Bidi::Commands::SetUsercontextCookie.new(
    user_context_id: context_id,
    source_origin: source_origin,
    name: name,
    value: value,
    domain: domain,
    path: path,
    secure: secure,
    http_only: http_only,
    same_site: same_site,
    ttl: ttl
  )

  client.send_cmd_and_wait(cmd) do |response|
    Bidi2pdf.logger.debug "Cookie set: #{response.inspect}"
  end
end