Update src/browser/custom_context.py
Browse files- src/browser/custom_context.py +35 -43
src/browser/custom_context.py
CHANGED
|
@@ -13,71 +13,63 @@ class CustomBrowserContext:
|
|
| 13 |
self.driver = None
|
| 14 |
|
| 15 |
def create_context(self):
|
| 16 |
-
"""Crea un nuevo contexto de navegador con medidas anti-detecci贸n y carga cookies si est谩n disponibles."""
|
| 17 |
chrome_options = Options()
|
| 18 |
|
| 19 |
-
# Configuraci贸n de la ventana del navegador
|
| 20 |
if self.config.get("browser_window_size"):
|
| 21 |
chrome_options.add_argument(f"--window-size={self.config['browser_window_size'][0]},{self.config['browser_window_size'][1]}")
|
| 22 |
|
| 23 |
-
# Configuraci贸n de seguridad
|
| 24 |
if self.config.get("disable_security", False):
|
| 25 |
chrome_options.add_argument("--disable-web-security")
|
| 26 |
chrome_options.add_argument("--disable-site-isolation-trials")
|
| 27 |
chrome_options.add_argument("--disable-features=IsolateOrigins,site-per-process")
|
| 28 |
|
| 29 |
-
# Configuraci贸n del user-agent
|
| 30 |
user_agent = (
|
| 31 |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
| 32 |
"(KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
|
| 33 |
)
|
| 34 |
chrome_options.add_argument(f"user-agent={user_agent}")
|
| 35 |
|
| 36 |
-
# Inicializar el navegador
|
| 37 |
self.driver = webdriver.Chrome(options=chrome_options)
|
| 38 |
|
| 39 |
-
# Cargar cookies si existen
|
| 40 |
if self.config.get("cookies_file") and os.path.exists(self.config["cookies_file"]):
|
| 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 |
-
window.navigator.permissions.query = (parameters) => (
|
| 71 |
-
parameters.name === 'notifications' ?
|
| 72 |
-
Promise.resolve({ state: Notification.permission }) :
|
| 73 |
-
originalQuery(parameters)
|
| 74 |
-
);
|
| 75 |
-
"""
|
| 76 |
-
)
|
| 77 |
|
| 78 |
return self.driver
|
| 79 |
|
| 80 |
def close(self):
|
| 81 |
-
"""Cierra el contexto del navegador."""
|
| 82 |
if self.driver:
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
self.driver = None
|
| 14 |
|
| 15 |
def create_context(self):
|
|
|
|
| 16 |
chrome_options = Options()
|
| 17 |
|
|
|
|
| 18 |
if self.config.get("browser_window_size"):
|
| 19 |
chrome_options.add_argument(f"--window-size={self.config['browser_window_size'][0]},{self.config['browser_window_size'][1]}")
|
| 20 |
|
|
|
|
| 21 |
if self.config.get("disable_security", False):
|
| 22 |
chrome_options.add_argument("--disable-web-security")
|
| 23 |
chrome_options.add_argument("--disable-site-isolation-trials")
|
| 24 |
chrome_options.add_argument("--disable-features=IsolateOrigins,site-per-process")
|
| 25 |
|
|
|
|
| 26 |
user_agent = (
|
| 27 |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
| 28 |
"(KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
|
| 29 |
)
|
| 30 |
chrome_options.add_argument(f"user-agent={user_agent}")
|
| 31 |
|
|
|
|
| 32 |
self.driver = webdriver.Chrome(options=chrome_options)
|
| 33 |
|
|
|
|
| 34 |
if self.config.get("cookies_file") and os.path.exists(self.config["cookies_file"]):
|
| 35 |
+
try:
|
| 36 |
+
with open(self.config["cookies_file"], "r") as f:
|
| 37 |
+
cookies = json.load(f)
|
| 38 |
+
logger.info(f"Loaded {len(cookies)} cookies from {self.config['cookies_file']}")
|
| 39 |
+
for cookie in cookies:
|
| 40 |
+
self.driver.add_cookie(cookie)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
logger.error(f"Error loading cookies: {e}")
|
| 43 |
|
| 44 |
+
try:
|
| 45 |
+
self.driver.execute_script(
|
| 46 |
+
"""
|
| 47 |
+
Object.defineProperty(navigator, 'webdriver', {
|
| 48 |
+
get: () => undefined
|
| 49 |
+
});
|
| 50 |
+
Object.defineProperty(navigator, 'languages', {
|
| 51 |
+
get: () => ['en-US', 'en']
|
| 52 |
+
});
|
| 53 |
+
Object.defineProperty(navigator, 'plugins', {
|
| 54 |
+
get: () => [1, 2, 3, 4, 5]
|
| 55 |
+
});
|
| 56 |
+
window.chrome = { runtime: {} };
|
| 57 |
+
const originalQuery = window.navigator.permissions.query;
|
| 58 |
+
window.navigator.permissions.query = (parameters) => (
|
| 59 |
+
parameters.name === 'notifications' ?
|
| 60 |
+
Promise.resolve({ state: Notification.permission }) :
|
| 61 |
+
originalQuery(parameters)
|
| 62 |
+
);
|
| 63 |
+
"""
|
| 64 |
+
)
|
| 65 |
+
except Exception as e:
|
| 66 |
+
logger.error(f"Error executing anti-detection scripts: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
return self.driver
|
| 69 |
|
| 70 |
def close(self):
|
|
|
|
| 71 |
if self.driver:
|
| 72 |
+
try:
|
| 73 |
+
self.driver.quit()
|
| 74 |
+
except Exception as e:
|
| 75 |
+
logger.error(f"Error closing the browser: {e}")
|