-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcdp.py
More file actions
397 lines (349 loc) · 12.2 KB
/
cdp.py
File metadata and controls
397 lines (349 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""
bluebox/data_models/cdp.py
Data models for async CDP events.
"""
from datetime import datetime, timezone
from enum import StrEnum
from typing import Any
from pydantic import BaseModel, Field, ConfigDict
from bluebox.data_models.ui_elements import UIElement
## Base event
class BaseCDPEvent(BaseModel):
"""
Base model for all CDP event details.
"""
timestamp: float = Field(
default_factory=lambda: datetime.now(tz=timezone.utc).timestamp(),
description="Unix timestamp (seconds) when the event occurred"
)
## Network models
class NetworkTransactionEvent(BaseCDPEvent):
"""
Model for network transaction monitoring events.
Captures detailed HTTP request and response information.
"""
model_config = ConfigDict(extra='allow')
request_id: str = Field(
...,
description="Unique identifier for the network request",
examples=["interception-job-1.0", "15BF081D76D2923D4AA7E645C41FB876"]
)
url: str = Field(
...,
description="The requested URL",
examples=["https://ycombinator.com/", "https://www.ycombinator.com/"]
)
method: str = Field(
...,
description="HTTP method used",
examples=["GET", "POST", "PUT"]
)
type: str | None = Field(
default=None,
description="Resource type (Document, Script, Image, etc.)",
examples=["Document", "Script", "XHR"]
)
status: int | None = Field(
default=None,
description="HTTP response status code",
examples=[200, 301, 404, 500]
)
status_text: str | None = Field(
default=None,
description="HTTP response status text",
examples=["OK", "Moved Permanently", "Not Found"]
)
request_headers: dict[str, str] | None = Field(
default=None,
description="HTTP request headers",
)
response_headers: dict[str, str] | None = Field(
default=None,
description="HTTP response headers",
)
post_data: str | dict | list | None = Field(
default=None,
description="Request body data (for POST/PUT requests). May be parsed JSON (dict/list) or raw string.",
)
response_body: str = Field(
default="",
description="Response body content",
)
response_body_base64: bool = Field(
default=False,
description="Whether response body is base64 encoded",
)
mime_type: str = Field(
default="",
description="MIME type of the response",
)
# Error fields for failed requests
errorText: str | None = Field(
default=None,
description="Error text if the request failed",
)
failed: bool = Field(
default=False,
description="Whether the request failed",
)
## Storage models
class StorageEventType(StrEnum):
"""Types of browser storage events."""
# Cookie events
INITIAL_COOKIES = "initialCookies"
COOKIE_CHANGE = "cookieChange"
# LocalStorage events
LOCAL_STORAGE_CLEARED = "localStorageCleared"
LOCAL_STORAGE_ITEM_ADDED = "localStorageItemAdded"
LOCAL_STORAGE_ITEM_REMOVED = "localStorageItemRemoved"
LOCAL_STORAGE_ITEM_UPDATED = "localStorageItemUpdated"
# SessionStorage events
SESSION_STORAGE_CLEARED = "sessionStorageCleared"
SESSION_STORAGE_ITEM_ADDED = "sessionStorageItemAdded"
SESSION_STORAGE_ITEM_REMOVED = "sessionStorageItemRemoved"
SESSION_STORAGE_ITEM_UPDATED = "sessionStorageItemUpdated"
# IndexedDB events
INDEXED_DB_EVENT = "indexedDBEvent"
@classmethod
def cookie_types(cls) -> set["StorageEventType"]:
"""Return all cookie-related event types."""
return {cls.INITIAL_COOKIES, cls.COOKIE_CHANGE}
@classmethod
def local_storage_types(cls) -> set["StorageEventType"]:
"""Return all localStorage-related event types."""
return {
cls.LOCAL_STORAGE_CLEARED,
cls.LOCAL_STORAGE_ITEM_ADDED,
cls.LOCAL_STORAGE_ITEM_REMOVED,
cls.LOCAL_STORAGE_ITEM_UPDATED,
}
@classmethod
def session_storage_types(cls) -> set["StorageEventType"]:
"""Return all sessionStorage-related event types."""
return {
cls.SESSION_STORAGE_CLEARED,
cls.SESSION_STORAGE_ITEM_ADDED,
cls.SESSION_STORAGE_ITEM_REMOVED,
cls.SESSION_STORAGE_ITEM_UPDATED,
}
@classmethod
def indexed_db_types(cls) -> set["StorageEventType"]:
"""Return all IndexedDB-related event types."""
return {cls.INDEXED_DB_EVENT}
# Factory methods for DOM storage events
@classmethod
def cleared(cls, is_local: bool) -> "StorageEventType":
"""Get the cleared event type for localStorage or sessionStorage."""
return cls.LOCAL_STORAGE_CLEARED if is_local else cls.SESSION_STORAGE_CLEARED
@classmethod
def item_added(cls, is_local: bool) -> "StorageEventType":
"""Get the item added event type for localStorage or sessionStorage."""
return cls.LOCAL_STORAGE_ITEM_ADDED if is_local else cls.SESSION_STORAGE_ITEM_ADDED
@classmethod
def item_removed(cls, is_local: bool) -> "StorageEventType":
"""Get the item removed event type for localStorage or sessionStorage."""
return cls.LOCAL_STORAGE_ITEM_REMOVED if is_local else cls.SESSION_STORAGE_ITEM_REMOVED
@classmethod
def item_updated(cls, is_local: bool) -> "StorageEventType":
"""Get the item updated event type for localStorage or sessionStorage."""
return cls.LOCAL_STORAGE_ITEM_UPDATED if is_local else cls.SESSION_STORAGE_ITEM_UPDATED
class StorageEvent(BaseCDPEvent):
"""
Model for browser storage monitoring events.
Handles cookies, localStorage, sessionStorage, and IndexedDB events.
"""
model_config = ConfigDict(extra='allow')
type: StorageEventType = Field(
...,
description="Type of storage event",
)
source: str | None = Field(
default=None,
description="Source of the event (for cookie events)",
)
origin: str | None = Field(
default=None,
description="Origin/domain for storage events",
)
# Cookie-specific fields
triggered_by: str | None = Field(
default=None,
description="What triggered the cookie change",
)
added: list[dict[str, Any]] | None = Field(
default=None,
description="Added cookies/items (for change events)",
)
modified: list[dict[str, Any]] | None = Field(
default=None,
description="Modified cookies/items (for change events)",
)
removed: list[dict[str, Any]] | None = Field(
default=None,
description="Removed cookies/items (for change events)",
)
total_count: int | None = Field(
default=None,
description="Total count of items after change",
)
# Storage item fields
key: str | None = Field(
default=None,
description="Storage key",
)
value: Any | None = Field(
default=None,
description="Storage value (for add/update events)",
)
old_value: Any | None = Field(
default=None,
description="Previous storage value (for update events)",
)
new_value: Any | None = Field(
default=None,
description="New storage value (for update events)",
)
# IndexedDB fields
params: dict[str, Any] | None = Field(
default=None,
description="Raw IndexedDB event parameters",
)
## Window property models
class WindowPropertyChange(BaseModel):
"""
Model for a single window property change.
"""
path: str = Field(
...,
description="The property path that changed",
)
value: Any | None = Field(
...,
description="The new value of the property (None for deletions)",
)
change_type: str = Field(
...,
description="Type of change: 'added', 'changed', or 'deleted'",
)
class WindowPropertyEvent(BaseCDPEvent):
"""
Model for window property monitoring events.
Emitted when window properties are collected and changes are detected.
"""
url: str = Field(
...,
description="The current page URL",
)
changes: list[WindowPropertyChange] = Field(
...,
description="List of property changes detected",
)
total_keys: int = Field(
...,
description="Total number of unique properties tracked in history"
)
## UI interaction models
class InteractionType(StrEnum):
"""Types of UI interactions that match real DOM event names."""
# Mouse events
CLICK = "click"
MOUSEDOWN = "mousedown"
MOUSEUP = "mouseup"
DBLCLICK = "dblclick"
CONTEXTMENU = "contextmenu"
MOUSEOVER = "mouseover"
# Keyboard events
KEYDOWN = "keydown"
KEYUP = "keyup"
KEYPRESS = "keypress" # deprecated but still emitted by browsers
# Form events
INPUT = "input"
CHANGE = "change"
# Focus events
FOCUS = "focus"
BLUR = "blur"
class Interaction(BaseModel):
"""
Details about how an interaction occurred.
Contains browser event properties like mouse coordinates, keyboard keys,
and modifier keys. These details provide the "how" of an interaction,
while InteractionType provides the "what".
"""
# Mouse properties
mouse_button: int | None = Field(
default=None,
description="Mouse button pressed (0=left, 1=middle, 2=right). None for non-mouse interactions."
)
mouse_x_viewport: int | None = Field(
default=None,
description="X coordinate relative to viewport. None for non-mouse interactions."
)
mouse_y_viewport: int | None = Field(
default=None,
description="Y coordinate relative to viewport. None for non-mouse interactions."
)
mouse_x_page: int | None = Field(
default=None,
description="X coordinate relative to page (includes scroll). None for non-mouse interactions."
)
mouse_y_page: int | None = Field(
default=None,
description="Y coordinate relative to page (includes scroll). None for non-mouse interactions."
)
# Keyboard properties
key_value: str | None = Field(
default=None,
description="The key value pressed (e.g., 'a', 'Enter', 'Shift'). None for non-keyboard interactions."
)
key_code: str | None = Field(
default=None,
description="The physical key code (e.g., 'KeyA', 'Enter', 'ShiftLeft'). None for non-keyboard interactions."
)
key_code_deprecated: int | None = Field(
default=None,
description="Deprecated numeric key code. None for non-keyboard interactions."
)
key_which_deprecated: int | None = Field(
default=None,
description="Deprecated numeric key code. None for non-keyboard interactions."
)
# Modifier keys (apply to both mouse and keyboard interactions)
ctrl_pressed: bool = Field(
default=False,
description="Whether the Ctrl key was pressed during the interaction."
)
shift_pressed: bool = Field(
default=False,
description="Whether the Shift key was pressed during the interaction."
)
alt_pressed: bool = Field(
default=False,
description="Whether the Alt key was pressed during the interaction."
)
meta_pressed: bool = Field(
default=False,
description="Whether the Meta/Cmd key was pressed during the interaction."
)
class UIInteractionEvent(BaseCDPEvent):
"""
Complete UI interaction event record.
Represents a single user interaction with a web element, including:
- What type of interaction occurred
- When it occurred (timestamp)
- What element was interacted with (UIElement)
- How it occurred (Interaction) - mouse position, keys pressed, modifiers, etc.
- Page context (URL)
"""
# Interaction type
type: InteractionType
# How the interaction occurred (mouse coordinates, keyboard keys, modifiers, etc.)
interaction: Interaction | None = Field(
default=None,
description="Details about how the interaction occurred (mouse position, keys pressed, modifiers, etc.)."
)
# Element that was interacted with
element: UIElement
# Page context
url: str = Field(
description="URL of the page where the interaction occurred."
)