Skip to content
This repository was archived by the owner on Sep 28, 2025. It is now read-only.

Commit fe8fbd2

Browse files
committed
Update v1.1 (Deprecation)
Fixes #17 Fixes #21
1 parent 156d4a8 commit fe8fbd2

17 files changed

+697
-155
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<h1 align="center">
2-
CDP-Patches v1.0
2+
CDP-Patches v1.1
33
</h1>
44

55

@@ -34,6 +34,14 @@
3434
</a>
3535
</p>
3636

37+
---
38+
39+
> [!CAUTION]
40+
> The [crbug#1477537](https://bugs.chromium.org/p/chromium/issues/detail?id=1477537) causing the Input Leak has been fixed in [#6917162](https://chromium-review.googlesource.com/c/chromium/src/+/6917162). It will probably be implemented in Chrome-Stable v142+. <br>
41+
> CoalescedEvents are now also emitted by Input Events. There is no reason to use this package anymore, except for Select Elements ([crbug#40943840](https://issues.chromium.org/issues/40943840)).
42+
43+
---
44+
3745
## Install it from PyPI
3846

3947
```bash

cdp_patches/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@
33

44
VERSION = "1.1"
55

6+
warnings.warn(
7+
"""
8+
The [crbug#1477537](https://bugs.chromium.org/p/chromium/issues/detail?id=1477537) causing the Input Leak has been fixed in [#6917162](https://chromium-review.googlesource.com/c/chromium/src/+/6917162). It will probably be implemented in Chrome-Stable v142+. <br>
9+
CoalescedEvents are now also emitted by Input Events. There is no reason to use this package anymore, except for Select Elements ([crbug#40943840](https://issues.chromium.org/issues/40943840)).
10+
""",
11+
DeprecationWarning,
12+
)
13+
614
system_name = platform.system()
715
if system_name == "Windows":
816
is_windows = True
917
elif system_name == "Linux":
1018
is_windows = False
1119
else:
1220
is_windows = False
13-
warnings.warn("Unknown system (You´re probably using MacOS, which is currently not supported).", RuntimeWarning)
21+
warnings.warn(
22+
"Unknown system (You´re probably using MacOS, which is currently not supported).",
23+
RuntimeWarning,
24+
)
1425

1526
__all__ = ["VERSION", "is_windows"]

cdp_patches/input/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,11 @@ class LinuxKeyboardCodes:
175175

176176

177177
KeyboardCodes = WinKeyboardCodes if is_windows else LinuxKeyboardCodes
178-
__all__ = ["SyncInput", "AsyncInput", "KeyboardCodes", "WinKeyboardCodes", "LinuxKeyboardCodes", "is_windows"]
178+
__all__ = [
179+
"SyncInput",
180+
"AsyncInput",
181+
"KeyboardCodes",
182+
"WinKeyboardCodes",
183+
"LinuxKeyboardCodes",
184+
"is_windows",
185+
]

cdp_patches/input/async_input.py

Lines changed: 102 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import time
99
from typing import Any, Generator, Literal, Optional, Union
1010

11-
from pywinauto.mouse import press
12-
1311
if sys.version_info.minor >= 10:
1412
from typing import TypeAlias
1513
else:
@@ -27,15 +25,25 @@
2725

2826
LinuxBase: TypeAlias = WindowsBase # type: ignore[no-redef]
2927
InputBase = WindowsBase # type: ignore
30-
WindowErrors = (ValueError, ElementNotEnabled, ProcessNotFoundError, WindowClosedException) # type: ignore[assignment]
28+
WindowErrors = (
29+
ValueError,
30+
ElementNotEnabled,
31+
ProcessNotFoundError,
32+
WindowClosedException,
33+
) # type: ignore[assignment]
3134
else:
3235
from cdp_patches.input.os_base.linux import LinuxBase # type: ignore[assignment]
3336

3437
WindowsBase: TypeAlias = LinuxBase # type: ignore[no-redef]
3538
InputBase = LinuxBase # type: ignore
3639
WindowErrors = (AssertionError, ValueError, WindowClosedException) # type: ignore[assignment]
3740

38-
from .browsers import DriverlessAsyncChrome, async_browsers, get_async_browser_pid, get_async_scale_factor
41+
from .browsers import (
42+
DriverlessAsyncChrome,
43+
async_browsers,
44+
get_async_browser_pid,
45+
get_async_scale_factor,
46+
)
3947
from .mouse_trajectory import HumanizeMouseTrajectory
4048

4149

@@ -52,10 +60,17 @@ class AsyncInput:
5260
selective_modifiers_regex = re.compile(r"{[^{}]*}|.")
5361

5462
def __init__(
55-
self, pid: Optional[int] = None, browser: Optional[async_browsers] = None, scale_factor: Optional[float] = 1.0, emulate_behaviour: Optional[bool] = True, window_timeout: Optional[float] = 30.0
63+
self,
64+
pid: Optional[int] = None,
65+
browser: Optional[async_browsers] = None,
66+
scale_factor: Optional[float] = 1.0,
67+
emulate_behaviour: Optional[bool] = True,
68+
window_timeout: Optional[float] = 30.0,
5669
) -> None:
5770
if platform.system() not in ("Windows", "Linux"):
58-
raise SystemError("Unknown system (You´re probably using MacOS, which is currently not supported).")
71+
raise SystemError(
72+
"Unknown system (You´re probably using MacOS, which is currently not supported)."
73+
)
5974

6075
self.pid = pid
6176
self.browser = browser
@@ -106,29 +121,59 @@ async def _wait_for_window(self) -> None:
106121
pass
107122
await self._sleep_timeout(0.1)
108123

109-
raise TimeoutError(f"Chrome Window (PID: {self.pid}) not found in {self.window_timeout} seconds.")
124+
raise TimeoutError(
125+
f"Chrome Window (PID: {self.pid}) not found in {self.window_timeout} seconds."
126+
)
110127

111128
async def _sleep_timeout(self, timeout: Optional[float] = None) -> None:
112129
timeout = timeout or self.sleep_timeout
113130
if not random.randint(0, 10):
114131
timeout_random = self.sleep_timeout / 10
115-
timeout = timeout or random.uniform(self.sleep_timeout, self.sleep_timeout + timeout_random)
132+
timeout = timeout or random.uniform(
133+
self.sleep_timeout, self.sleep_timeout + timeout_random
134+
)
116135

117136
await asyncio.sleep(timeout)
118137

119138
async def click(
120-
self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], pressed: str = "", emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None
139+
self,
140+
button: Literal["left", "right", "middle"],
141+
x: Union[int, float],
142+
y: Union[int, float],
143+
pressed: str = "",
144+
emulate_behaviour: Optional[bool] = True,
145+
timeout: Optional[float] = None,
121146
) -> None:
122147
x, y = int(x), int(y)
123148

124-
await self.down(button=button, x=x, y=y, emulate_behaviour=emulate_behaviour, timeout=timeout, pressed=pressed)
149+
await self.down(
150+
button=button,
151+
x=x,
152+
y=y,
153+
emulate_behaviour=emulate_behaviour,
154+
timeout=timeout,
155+
pressed=pressed,
156+
)
157+
await self.down(
158+
button=button,
159+
x=x,
160+
y=y,
161+
emulate_behaviour=emulate_behaviour,
162+
timeout=timeout,
163+
)
125164
if self.emulate_behaviour and emulate_behaviour:
126165
await self._sleep_timeout(timeout=timeout)
127166
await self.up(button=button, x=x, y=y, pressed=pressed)
128167
self.last_x, self.last_y = x, y
129168

130169
async def double_click(
131-
self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], pressed: str = "", emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None
170+
self,
171+
button: Literal["left", "right", "middle"],
172+
x: Union[int, float],
173+
y: Union[int, float],
174+
emulate_behaviour: Optional[bool] = True,
175+
timeout: Optional[float] = None,
176+
pressed: str = "",
132177
) -> None:
133178
x, y = int(x), int(y)
134179

@@ -138,34 +183,68 @@ async def double_click(
138183
self._base.move(x=x, y=y)
139184

140185
kwargs = _mk_kwargs(pressed)
141-
self._base.double_click(button=button, x=x, y=y, press_timeout=press_timeout, click_timeout=click_timeout, **kwargs)
186+
self._base.double_click(
187+
button=button,
188+
x=x,
189+
y=y,
190+
press_timeout=press_timeout,
191+
click_timeout=click_timeout,
192+
**kwargs,
193+
)
142194
self.last_x, self.last_y = x, y
143195

144196
async def down(
145-
self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], pressed: str = "", emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None
197+
self,
198+
button: Literal["left", "right", "middle"],
199+
x: Union[int, float],
200+
y: Union[int, float],
201+
emulate_behaviour: Optional[bool] = True,
202+
timeout: Optional[float] = None,
203+
pressed: str = "",
146204
) -> None:
147205
x, y = int(x), int(y)
148206

149207
if self.emulate_behaviour and emulate_behaviour:
150-
await self.move(x=x, y=y, timeout=timeout, emulate_behaviour=emulate_behaviour, pressed=pressed)
208+
await self.move(
209+
x=x,
210+
y=y,
211+
timeout=timeout,
212+
emulate_behaviour=emulate_behaviour,
213+
pressed=pressed,
214+
)
151215
kwargs = _mk_kwargs(pressed)
152216
self._base.down(button=button, x=x, y=y, **kwargs)
153217
self.last_x, self.last_y = x, y
154218

155-
async def up(self, button: Literal["left", "right", "middle"], x: Union[int, float], y: Union[int, float], pressed: str = "") -> None:
219+
async def up(
220+
self,
221+
button: Literal["left", "right", "middle"],
222+
x: Union[int, float],
223+
y: Union[int, float],
224+
pressed: str = "",
225+
) -> None:
156226
x, y = int(x), int(y)
157227

158228
kwargs = _mk_kwargs(pressed)
159229
self._base.up(button=button, x=x, y=y, **kwargs)
160230
self.last_x, self.last_y = x, y
161231

162-
async def move(self, x: Union[int, float], y: Union[int, float], emulate_behaviour: Optional[bool] = True, timeout: Optional[float] = None, pressed: str = "") -> None:
232+
async def move(
233+
self,
234+
x: Union[int, float],
235+
y: Union[int, float],
236+
emulate_behaviour: Optional[bool] = True,
237+
timeout: Optional[float] = None,
238+
pressed: str = "",
239+
) -> None:
163240
kwargs = _mk_kwargs(pressed)
164241
async with self._move_lock:
165242
x, y = int(x), int(y)
166243

167244
if self.emulate_behaviour and emulate_behaviour:
168-
humanized_points = HumanizeMouseTrajectory((self.last_x, self.last_y), (x, y))
245+
humanized_points = HumanizeMouseTrajectory(
246+
(self.last_x, self.last_y), (x, y)
247+
)
169248

170249
# Move Mouse to new random locations
171250
for i, (human_x, human_y) in enumerate(humanized_points.points):
@@ -176,10 +255,14 @@ async def move(self, x: Union[int, float], y: Union[int, float], emulate_behavio
176255
self._base.move(x=x, y=y, **kwargs)
177256
self.last_x, self.last_y = x, y
178257

179-
async def scroll(self, direction: Literal["up", "down", "left", "right"], amount: int) -> None:
258+
async def scroll(
259+
self, direction: Literal["up", "down", "left", "right"], amount: int
260+
) -> None:
180261
self._base.scroll(direction=direction, amount=amount)
181262

182-
async def type(self, text: str, fill: Optional[bool] = False, timeout: Optional[float] = None) -> None:
263+
async def type(
264+
self, text: str, fill: Optional[bool] = False, timeout: Optional[float] = None
265+
) -> None:
183266
if self.emulate_behaviour and not fill:
184267
for i, char in enumerate(self.selective_modifiers_regex.findall(text)):
185268
# If new word is started wait some more time

0 commit comments

Comments
 (0)