Skip to content

Commit 1bd434b

Browse files
committed
Update UC Mode code
1 parent 5148599 commit 1bd434b

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ def _set_chrome_options(
734734
or IS_LINUX # switches to Xvfb (non-headless)
735735
)
736736
):
737+
chrome_options.add_argument("--disable-popup-blocking")
737738
# Skip remaining options that trigger anti-bot services
738739
return chrome_options
739740
chrome_options.add_argument("--test-type")

seleniumbase/undetected/__init__.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import selenium.webdriver.chrome.webdriver
1010
import selenium.webdriver.common.service
1111
import selenium.webdriver.remote.command
12+
from .cdp import CDP
13+
from .cdp import PageElement
1214
from .dprocess import start_detached
1315
from .options import ChromeOptions
1416
from .patcher import IS_POSIX
@@ -133,13 +135,12 @@ def __init__(
133135
constants.MultiBrowser.DRIVER_FIXING_LOCK
134136
)
135137
with uc_lock:
136-
patcher = Patcher(
138+
self.patcher = Patcher(
137139
executable_path=driver_executable_path,
138140
force=patcher_force_close,
139141
version_main=version_main,
140142
)
141-
patcher.auto()
142-
self.patcher = patcher
143+
self.patcher.auto()
143144
if not options:
144145
options = ChromeOptions()
145146
try:
@@ -280,7 +281,7 @@ def __init__(
280281
service_ = None
281282
if patch_driver:
282283
service_ = selenium.webdriver.chrome.service.Service(
283-
executable_path=patcher.executable_path,
284+
executable_path=self.patcher.executable_path,
284285
log_path=os.devnull,
285286
)
286287
else:
@@ -380,19 +381,28 @@ def clear_cdp_listeners(self):
380381
if self.reactor and isinstance(self.reactor, Reactor):
381382
self.reactor.handlers.clear()
382383

383-
def window_new(self):
384+
def window_new(self, url=None):
384385
self.execute(
385386
selenium.webdriver.remote.command.Command.NEW_WINDOW,
386387
{"type": "window"},
387388
)
389+
if url:
390+
self.remove_cdc_props_as_needed()
391+
return super().get(url)
392+
return None
388393

389394
def tab_new(self, url):
390395
"""This opens a url in a new tab."""
391396
if not hasattr(self, "cdp"):
392-
from .cdp import CDP
397+
self.cdp = CDP(self.options)
398+
self.cdp.tab_new(str(url))
393399

394-
cdp = CDP(self.options)
395-
cdp.tab_new(str(url))
400+
def tab_list(self):
401+
if not hasattr(self, "cdp"):
402+
self.cdp = CDP(self.options)
403+
404+
retval = self.get(self.cdp.endpoints["list"])
405+
return [PageElement(o) for o in retval]
396406

397407
def reconnect(self, timeout=0.1):
398408
try:

seleniumbase/undetected/cdp.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ async def send(self, method, params): # noqa
8989
self.log.info(self._last_json)
9090

9191
def get(self, uri):
92+
from urllib.parse import unquote
93+
94+
uri = unquote(uri, errors="strict")
9295
resp = self._session.get(self.server_addr + uri)
9396
try:
9497
self._last_resp = resp
@@ -99,6 +102,9 @@ def get(self, uri):
99102
return self._last_json
100103

101104
def post(self, uri, data=None):
105+
from urllib.parse import unquote
106+
107+
uri = unquote(uri, errors="strict")
102108
if not data:
103109
data = {}
104110
resp = self._session.post(self.server_addr + uri, json=data)

seleniumbase/undetected/patcher.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ def __init__(self, executable_path=None, force=False, version_main=0):
4747
version_main: 0 = auto
4848
Specify main chrome version (rounded, ex: 82)
4949
"""
50-
import secrets
51-
5250
self.force = force
5351
self.executable_path = None
54-
prefix = secrets.token_hex(8)
52+
prefix = "undetected"
5553
if not os.path.exists(self.data_path):
5654
os.makedirs(self.data_path, exist_ok=True)
5755
if not executable_path:
@@ -222,6 +220,33 @@ def gen_call_function_js_cache_name(match):
222220
fh.write(file_bin)
223221
return True
224222

223+
def is_binary_patched_new(self, executable_path=None):
224+
executable_path = executable_path or self.executable_path
225+
with io.open(executable_path, "rb") as fh:
226+
return fh.read().find(b"undetected chromedriver") != -1
227+
228+
def patch_exe_new(self):
229+
logger.info("patching driver executable %s" % self.executable_path)
230+
with io.open(self.executable_path, "r+b") as fh:
231+
content = fh.read()
232+
match_injected_codeblock = re.search(rb"{window.*;}", content)
233+
if match_injected_codeblock:
234+
target_bytes = match_injected_codeblock[0]
235+
new_target_bytes = (
236+
b'{console.log("undetected chromedriver 1337!")}'.ljust(
237+
len(target_bytes), b" "
238+
)
239+
)
240+
new_content = content.replace(target_bytes, new_target_bytes)
241+
if new_content == content:
242+
pass # Failure to patch driver
243+
else:
244+
# Patch now
245+
fh.seek(0)
246+
fh.write(new_content)
247+
else:
248+
pass # Already patched
249+
225250
def __repr__(self):
226251
return "{0:s}({1:s})".format(
227252
self.__class__.__name__,

0 commit comments

Comments
 (0)