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 included from Disposable::DisposableMixin

#dispose

Methods inherited from EventEmitter

#dispose, #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

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

#dispose_prompt(reason) ⇒ Object

Parameters:

  • reason (Object)

Returns:

  • (Object)


96
97
98
99
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 96

def dispose_prompt(reason)
  @reason = reason
  dispose
end

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

Handle the user prompt

Parameters:

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

Returns:

  • (Hash[String, untyped])


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

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

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

  session.send_command('browsingContext.handleUserPrompt', params)

  # The result is set by the userPromptClosed event before this returns
  @result
end

#handled?Boolean

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

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)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 80

def initialize_prompt
  # Listen for browsing context closure
  @browsing_context.once(:closed) do |reason|
    dispose_prompt("User prompt already closed: #{reason}")
  end

  # Listen for prompt closed event
  session.on('browsingContext.userPromptClosed') do |params|
    next unless params['context'] == @browsing_context.id

    @result = params
    emit(:handled, params)
    dispose_prompt('User prompt handled')
  end
end

#perform_disposeObject

Returns:

  • (Object)


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

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

#sessionObject

Returns:

  • (Object)


76
77
78
# File 'lib/puppeteer/bidi/core/user_prompt.rb', line 76

def session
  @browsing_context.user_context.browser.session
end