Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Add event listener tracking to BrowserSession initialization

jonathan.qiu4b71289b5fd7
diff --git a/browser_use/browser/context.py b/browser_use/browser/context.py
index 44b7595d5..bfe18d680 100644
--- a/browser_use/browser/context.py
+++ b/browser_use/browser/context.py
@@ -136,11 +136,54 @@ class BrowserContextConfig:
_force_keep_context_alive: bool = False
-@dataclass
class BrowserSession:
- context: PlaywrightBrowserContext
- cached_state: BrowserState | None
+
+ def __init__(
+ self,
+ context: PlaywrightBrowserContext,
+ cached_state: BrowserState | None=None
+ ):
+ init_script = """
+ (() => {
+ if (!window.getEventListeners) {
+ window.getEventListeners = function (node) {
+ return node.__listeners || {};
+ };
+
+ // Save the original addEventListener
+ const originalAddEventListener = Element.prototype.addEventListener;
+
+ const eventProxy = {
+ addEventListener: function (type, listener, options = { once: false, passive: false, capture: false }) {
+ // Initialize __listeners if not exists
+ if (!this.__listeners) {
+ this.__listeners = {};
+ }
+
+ // Initialize array for this event type if not exists
+ if (!this.__listeners[type]) {
+ this.__listeners[type] = [];
+ }
+ // Add the listener to __listeners
+ this.__listeners[type].push({
+ listener: listener,
+ type: type,
+ ...options
+ });
+
+ // Call original addEventListener using the saved reference
+ return originalAddEventListener.call(this, type, listener, options);
+ }
+ };
+
+ Element.prototype.addEventListener = eventProxy.addEventListener;
+ }
+ })()
+ """
+ self.context = context
+ self.cached_state = cached_state
+ self.context.on('page', lambda page: page.add_init_script(init_script))
@dataclass
class BrowserContextState: