Methods
page.on()
Listens for page events. Returns an AbortController for cleanup.
event: Event type to listen forhandler: Function to handle the event
Promise<AbortController> - Controller for stopping the listener
Example:
page.once()
Listens for a page event once, then automatically removes the listener.
event: Event type to listen forhandler: Function to handle the event
Promise<AbortController> - Controller (listener auto-removed after first event)
Example:
page.off()
Stops listening to events by providing the AbortController.
controller: AbortController returned fromon()oronce()
Available events
xhr
Emitted when an HTTP request (fetch/XHR) is made.
Event data:
started
Emitted when page load starts.
Example:
finished
Emitted when page load finishes.
Example:
locationchange
Emitted when the page URL changes.
Example:
domchange
Emitted when the DOM is mutated.
Example:
dispatch
Emitted for custom events dispatched by the page.
Example:
close
Emitted when the browser requests to close (e.g., user clicks close).
Example:
closed
Emitted when the page is actually closed.
Example:
hostblocked
Emitted when a host is blocked by an allowlist.
Example:
Complete examples
Monitor all HTTP requests
Filter API requests
Track page lifecycle
Count DOM changes
Wait for specific request
Detect failed requests
Monitor page navigation
Measure request performance
Handle custom events
Cleanup on page close
Debug mode logging
Best practices
Clean up listeners
Clean up listeners
Always remove event listeners when done:
Use once() for one-time events
Use once() for one-time events
Use
once() for events you only need to handle once:Filter events
Filter events
Filter events in your handler to avoid processing irrelevant data:
Handle async handlers
Handle async handlers
Event handlers can be async, but they don’t block the page: