Class: Puppeteer::CdpWebWorker

Inherits:
WebWorker show all
Includes:
DebugPrint
Defined in:
lib/puppeteer/web_worker.rb

Instance Attribute Summary

Attributes inherited from WebWorker

#timeout_settings

Instance Method Summary collapse

Methods included from DebugPrint

#debug_print, #debug_puts

Methods inherited from WebWorker

#evaluate, #evaluate_handle, #url

Methods included from EventCallbackable

#add_event_listener, #emit_event, #observe_first, #off, #on_event, #remove_event_listener

Constructor Details

#initialize(client, url, target_id, target_type, console_api_called, exception_thrown, network_manager: nil) ⇒ CdpWebWorker

Returns a new instance of CdpWebWorker.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/puppeteer/web_worker.rb', line 115

def initialize(client, url, target_id, target_type, console_api_called, exception_thrown, network_manager: nil)
  super(url)
  @client = client
  @target_id = target_id
  @target_type = target_type
  @world = Puppeteer::WorkerWorld.new(@client)

  @client.once('Runtime.executionContextCreated') do |event|
    @world.set_context(Puppeteer::ExecutionContext.new(@client, event['context'], @world))
  end
  if console_api_called
    @client.on_event('Runtime.consoleAPICalled') do |event|
      console_api_called.call(@world, event)
    end
  end
  if exception_thrown
    @client.on_event('Runtime.exceptionThrown') do |event|
      exception_thrown.call(event['exceptionDetails'])
    end
  end
  @client.once(CDPSessionEmittedEvents::Disconnected) do
    @world.dispose
  end

  if network_manager
    Async do
      begin
        network_manager.add_client(@client)
      rescue => err
        debug_puts(err)
      end
    end
  end

  @client.async_send_message('Runtime.enable')
end

Instance Method Details

#clientObject



158
159
160
# File 'lib/puppeteer/web_worker.rb', line 158

def client
  @client
end

#closeObject



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/puppeteer/web_worker.rb', line 163

def close
  connection = @client.connection
  case @target_type
  when 'service_worker'
    connection&.send_message('Target.closeTarget', targetId: @target_id)
    connection&.send_message('Target.detachFromTarget', sessionId: @client.id)
  when 'shared_worker'
    connection&.send_message('Target.closeTarget', targetId: @target_id)
  else
    evaluate('() => self.close()')
  end
end

#main_realmObject



153
154
155
# File 'lib/puppeteer/web_worker.rb', line 153

def main_realm
  @world
end