Skip to content

Commit e1e8590

Browse files
committed
Update UC Mode
1 parent 48b553d commit e1e8590

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ def _set_chrome_options(
644644
chrome_options = add_chrome_ext_dir(chrome_options, recorder_dir)
645645
else:
646646
chrome_options = _add_chrome_recorder_extension(chrome_options)
647-
if is_using_uc(undetectable, browser_name):
647+
if chromium_arg and "sbase" in chromium_arg:
648648
sbase_ext_zip = SBASE_EXT_ZIP_PATH
649649
sbase_ext_dir = os.path.join(DOWNLOADS_FOLDER, "sbase_ext")
650650
_unzip_to_new_folder(sbase_ext_zip, sbase_ext_dir)

seleniumbase/undetected/patcher.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import random
66
import re
7+
import string
78
import sys
89
import time
910

@@ -182,28 +183,44 @@ def gen_random_cdc():
182183
def is_binary_patched(self, executable_path=None):
183184
executable_path = executable_path or self.executable_path
184185
with io.open(executable_path, "rb") as fh:
185-
for line in iter(lambda: fh.readline(), b""):
186-
if b"cdc_" in line:
187-
return False
188-
else:
189-
return True
186+
if b"window.cdc_adoQpoasnfa76pfcZLmcfl_" in fh.read():
187+
return False
188+
return True
190189

191190
def patch_exe(self):
192-
"""
193-
Patches the ChromeDriver binary
194-
:return: False on failure, binary name on success
195-
"""
196-
logger.info("patching driver executable %s" % self.executable_path)
197-
linect = 0
198-
replacement = self.gen_random_cdc()
191+
"""Patches the ChromeDriver binary"""
192+
def gen_js_whitespaces(match):
193+
return b"\n" * len(match.group())
194+
195+
def gen_call_function_js_cache_name(match):
196+
rep_len = len(match.group()) - 3
197+
ran_len = random.randint(6, rep_len)
198+
bb = b"'" + bytes(str().join(random.choices(
199+
population=string.ascii_letters, k=ran_len
200+
)), 'ascii') + b"';" + (b"\n" * (rep_len - ran_len))
201+
return bb
202+
199203
with io.open(self.executable_path, "r+b") as fh:
200-
for line in iter(lambda: fh.readline(), b""):
201-
if b"cdc_" in line:
202-
fh.seek(-len(line), 1)
203-
newline = re.sub(b"cdc_.{22}", replacement, line)
204-
fh.write(newline)
205-
linect += 1
206-
return linect
204+
file_bin = fh.read()
205+
file_bin = re.sub(
206+
b"window\\.cdc_[a-zA-Z0-9]{22}_(Array|Promise|Symbol)"
207+
b" = window\\.(Array|Promise|Symbol);",
208+
gen_js_whitespaces,
209+
file_bin,
210+
)
211+
file_bin = re.sub(
212+
b"window\\.cdc_[a-zA-Z0-9]{22}_(Array|Promise|Symbol) \\|\\|",
213+
gen_js_whitespaces,
214+
file_bin,
215+
)
216+
file_bin = re.sub(
217+
b"'\\$cdc_[a-zA-Z0-9]{22}_';",
218+
gen_call_function_js_cache_name,
219+
file_bin,
220+
)
221+
fh.seek(0)
222+
fh.write(file_bin)
223+
return True
207224

208225
def __repr__(self):
209226
return "{0:s}({1:s})".format(

0 commit comments

Comments
 (0)