Update src/browser/custom_context.py
Browse files- src/browser/custom_context.py +83 -88
src/browser/custom_context.py
CHANGED
|
@@ -1,88 +1,83 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import logging
|
| 3 |
-
import os
|
| 4 |
-
|
| 5 |
-
from
|
| 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 |
-
Object.defineProperty(navigator, '
|
| 62 |
-
get: () =>
|
| 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 |
-
return context
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import logging
|
| 3 |
+
import os
|
| 4 |
+
from selenium import webdriver
|
| 5 |
+
from selenium.webdriver.chrome.options import Options
|
| 6 |
+
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
+
class CustomBrowserContext:
|
| 10 |
+
def __init__(self, browser, config=None):
|
| 11 |
+
self.browser = browser
|
| 12 |
+
self.config = config or {}
|
| 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 |
+
with open(self.config["cookies_file"], "r") as f:
|
| 42 |
+
cookies = json.load(f)
|
| 43 |
+
logger.info(f"Loaded {len(cookies)} cookies from {self.config['cookies_file']}")
|
| 44 |
+
for cookie in cookies:
|
| 45 |
+
self.driver.add_cookie(cookie)
|
| 46 |
+
|
| 47 |
+
# Ejecutar scripts anti-detecci贸n
|
| 48 |
+
self.driver.execute_script(
|
| 49 |
+
"""
|
| 50 |
+
// Webdriver property
|
| 51 |
+
Object.defineProperty(navigator, 'webdriver', {
|
| 52 |
+
get: () => undefined
|
| 53 |
+
});
|
| 54 |
+
|
| 55 |
+
// Languages
|
| 56 |
+
Object.defineProperty(navigator, 'languages', {
|
| 57 |
+
get: () => ['en-US', 'en']
|
| 58 |
+
});
|
| 59 |
+
|
| 60 |
+
// Plugins
|
| 61 |
+
Object.defineProperty(navigator, 'plugins', {
|
| 62 |
+
get: () => [1, 2, 3, 4, 5]
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
// Chrome runtime
|
| 66 |
+
window.chrome = { runtime: {} };
|
| 67 |
+
|
| 68 |
+
// Permissions
|
| 69 |
+
const originalQuery = window.navigator.permissions.query;
|
| 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 |
+
self.driver.quit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|