Class: Puppeteer::Bidi::Core::UserPrompt

Inherits:
EventEmitter show all
Includes:
Disposable::DisposableMixin
Defined in:
lib/puppeteer/bidi/core/user_prompt.rb,
sig/puppeteer/bidi/core/user_prompt.rbs

Overview

UserPrompt represents a user prompt (alert, confirm, prompt)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EventEmitter

#emit, #off, #on, #once, #remove_all_listeners

Constructor Details

#initialize(browsing_context, info) ⇒ UserPrompt

Returns a new instance of UserPrompt.

Parameters:

  • browsing_context (Object)
  • info (Object)


23
24
25
26
27
28
29
30
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 23

def initialize(browsing_context, info)
  super()
  @browsing_context = browsing_context
  @info = info
  @reason = nil
  @result = nil
  @disposables = Disposable::DisposableStack.new
end

Instance Attribute Details

#browsing_contextObject (readonly)

Returns the value of attribute browsing_context.

Returns:

  • (Object)


21
22
23
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 21

def browsing_context
  @browsing_context
end

#infoObject (readonly)

Returns the value of attribute info.

Returns:

  • (Object)


21
22
23
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 21

def info
  @info
end

#resultObject (readonly)

Returns the value of attribute result.

Returns:

  • (Object)


21
22
23
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 21

def result
  @result
end

Class Method Details

.from(browsing_context, info) ⇒ UserPrompt

Create a user prompt instance

RBS:

  • browsing_context: BrowsingContext -- The browsing context

  • info: Hash[String, untyped] -- The userPromptOpened event data

  • return: UserPrompt -- New user prompt instance

Parameters:

Returns:



15
16
17
18
19
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 15

def self.from(browsing_context, info)
  prompt = new(browsing_context, info)
  prompt.send(:initialize_prompt)
  prompt
end

Instance Method Details

#closed?Object Also known as: disposed?

Check if the prompt is closed

Returns:

  • (Object)


33
34
35
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 33

def closed?
  !@reason.nil?
end

#disposeObject

Returns:

  • (Object)


67
68
69
70
71
72
73
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 67

def dispose
  return if @disposed

  @reason ||= "User prompt already closed, probably because the associated browsing context was destroyed."
  emit(:closed, @reason)
  super
end

#dispose_prompt(reason) ⇒ Object

Parameters:

  • reason (Object)

Returns:

  • (Object)


110
111
112
113
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 110

def dispose_prompt(reason)
  @reason = reason
  dispose
end

#handle(accept: nil, user_text: nil) ⇒ Async::Task[Hash[String, untyped]]

Handle the user prompt

RBS:

  • accept: bool? -- Whether to accept the prompt

  • user_text: String? -- Text to enter (for prompt dialogs)

  • return: Async::Task[Hash[String, untyped]] -- Result of handling the prompt

Parameters:

  • accept: (Boolean, nil) (defaults to: nil)
  • user_text: (String, nil) (defaults to: nil)

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 52

def handle(accept: nil, user_text: nil)
  Async do
    raise UserPromptClosedError, @reason if closed?

    params = { context: @info["context"] }
    params[:accept] = accept unless accept.nil?
    params[:userText] = user_text if user_text

    session.async_send_command("browsingContext.handleUserPrompt", params).wait

    # The handled event is emitted before the command response resolves.
    @result
  end
end

#handle_prompt_closed(params) ⇒ Object

Parameters:

  • params (Object)

Returns:

  • (Object)


102
103
104
105
106
107
108
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 102

def handle_prompt_closed(params)
  return unless params["context"] == @browsing_context.id

  @result = params
  emit(:handled, params)
  dispose_prompt("User prompt already handled.")
end

#handled?Boolean

Check if the prompt has been handled Auto-handled prompts return true immediately

RBS:

  • return: bool -- Whether the prompt is handled

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 42

def handled?
  handler = @info['handler']
  return true if handler == 'accept' || handler == 'dismiss'
  !@result.nil?
end

#initialize_promptObject

Returns:

  • (Object)


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 90

def initialize_prompt
  # Listen for browsing context closure
  @context_closed_listener = proc do |reason|
    dispose_prompt("User prompt already closed: #{reason}")
  end
  @browsing_context.on(:closed, &@context_closed_listener)

  # Listen for prompt closed event
  @prompt_closed_listener = method(:handle_prompt_closed).to_proc
  session.on("browsingContext.userPromptClosed", &@prompt_closed_listener)
end

#perform_disposeObject

Returns:

  • (Object)


77
78
79
80
81
82
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 77

def perform_dispose
  @browsing_context.off(:closed, &@context_closed_listener) if @context_closed_listener
  session.off("browsingContext.userPromptClosed", &@prompt_closed_listener) if @prompt_closed_listener
  @disposables.dispose
  remove_all_listeners
end

#sessionObject

Returns:

  • (Object)


86
87
88
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 86

def session
  @browsing_context.user_context.browser.session
end