lib/puppeteer/page.rb in puppeteer-ruby-0.0.11 vs lib/puppeteer/page.rb in puppeteer-ruby-0.0.12
- old
+ new
@@ -122,9 +122,48 @@
@client.async_send_message('Performance.enable'),
@client.async_send_message('Log.enable'),
)
end
+ EVENT_MAPPINGS = {
+ close: 'Events.Page.Close',
+ # console: 'Events.Page.Console',
+ # dialog: 'Events.Page.Dialog',
+ domcontentloaded: 'Events.Page.DOMContentLoaded',
+ # error:
+ frameattached: 'Events.Page.FrameAttached',
+ framedetached: 'Events.Page.FrameDetached',
+ framenavigated: 'Events.Page.FrameNavigated',
+ load: 'Events.Page.Load',
+ # metrics: 'Events.Page.Metrics',
+ # pageerror: 'Events.Page.PageError',
+ popup: 'Events.Page.Popup',
+ request: 'Events.Page.Request',
+ requestfailed: 'Events.Page.RequestFailed',
+ requestfinished: 'Events.Page.RequestFinished',
+ response: 'Events.Page.Response',
+ # workercreated: 'Events.Page.WorkerCreated',
+ # workerdestroyed: 'Events.Page.WorkerDestroyed',
+ }
+
+ # @param event_name [Symbol]
+ def on(event_name, &block)
+ unless EVENT_MAPPINGS.has_key?(event_name.to_sym)
+ raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{EVENT_MAPPINGS.keys.join(", ")}")
+ end
+
+ add_event_listener(EVENT_MAPPINGS[event_name.to_sym], &block)
+ end
+
+ # @param event_name [Symbol]
+ def once(event_name, &block)
+ unless EVENT_MAPPINGS.has_key?(event_name.to_sym)
+ raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{EVENT_MAPPINGS.keys.join(", ")}")
+ end
+
+ observe_first(EVENT_MAPPINGS[event_name.to_sym], &block)
+ end
+
def handle_file_chooser(event)
return if @file_chooser_interceptors.empty?
frame = @frame_manager.frame(event['frameId'])
context = frame.execution_context