Enhance browser configuration and path retrieval

- Updated `config.py` to include a comprehensive browser configuration section, allowing for dynamic retrieval of browser paths and drivers for Chrome, Edge, Firefox, and Brave.
- Refactored `new_signup.py`, `new_tempemail.py`, and `oauth_auth.py` to utilize the new browser configuration, improving flexibility and maintainability.
- Removed deprecated `get_default_chrome_path` function and replaced it with a more generalized `get_default_browser_path` function in `utils.py`.
- Updated localization files to include new strings related to browser path validation and selection.
This commit is contained in:
yeongpin
2025-04-12 14:11:22 +08:00
parent c42d7d5422
commit db3a2032dc
7 changed files with 582 additions and 201 deletions

139
utils.py
View File

@@ -9,24 +9,129 @@ def get_user_documents_path():
return os.path.expanduser("~\\Documents")
else:
return os.path.expanduser("~/Documents")
def get_default_chrome_path():
"""Get default Chrome path"""
if sys.platform == "win32":
# Trying to find chrome in PATH
try:
import shutil
chrome_in_path = shutil.which("chrome")
if chrome_in_path:
return chrome_in_path
except:
pass
# Going to default path
return r"C:\Program Files\Google\Chrome\Application\chrome.exe"
elif sys.platform == "darwin":
return "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
def get_default_driver_path(browser_type='chrome'):
"""Get default driver path based on browser type"""
browser_type = browser_type.lower()
if browser_type == 'chrome':
return get_default_chrome_driver_path()
elif browser_type == 'edge':
return get_default_edge_driver_path()
elif browser_type == 'firefox':
return get_default_firefox_driver_path()
elif browser_type == 'brave':
# Brave 使用 Chrome 的 driver
return get_default_chrome_driver_path()
else:
return "/usr/bin/google-chrome"
# Default to Chrome if browser type is unknown
return get_default_chrome_driver_path()
def get_default_chrome_driver_path():
"""Get default Chrome driver path"""
if sys.platform == "win32":
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "drivers", "chromedriver.exe")
elif sys.platform == "darwin":
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "drivers", "chromedriver")
else:
return "/usr/local/bin/chromedriver"
def get_default_edge_driver_path():
"""Get default Edge driver path"""
if sys.platform == "win32":
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "drivers", "msedgedriver.exe")
elif sys.platform == "darwin":
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "drivers", "msedgedriver")
else:
return "/usr/local/bin/msedgedriver"
def get_default_firefox_driver_path():
"""Get default Firefox driver path"""
if sys.platform == "win32":
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "drivers", "geckodriver.exe")
elif sys.platform == "darwin":
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "drivers", "geckodriver")
else:
return "/usr/local/bin/geckodriver"
def get_default_brave_driver_path():
"""Get default Brave driver path (uses Chrome driver)"""
# Brave 浏览器基于 Chromium所以使用相同的 chromedriver
return get_default_chrome_driver_path()
def get_default_browser_path(browser_type='chrome'):
"""Get default browser executable path"""
browser_type = browser_type.lower()
if sys.platform == "win32":
if browser_type == 'chrome':
# 尝试在 PATH 中找到 Chrome
try:
import shutil
chrome_in_path = shutil.which("chrome")
if chrome_in_path:
return chrome_in_path
except:
pass
# 使用默认路径
return r"C:\Program Files\Google\Chrome\Application\chrome.exe"
elif browser_type == 'edge':
return r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
elif browser_type == 'firefox':
return r"C:\Program Files\Mozilla Firefox\firefox.exe"
elif browser_type == 'brave':
# Brave 浏览器的默认安装路径
paths = [
os.path.join(os.environ.get('PROGRAMFILES', ''), 'BraveSoftware/Brave-Browser/Application/brave.exe'),
os.path.join(os.environ.get('PROGRAMFILES(X86)', ''), 'BraveSoftware/Brave-Browser/Application/brave.exe'),
os.path.join(os.environ.get('LOCALAPPDATA', ''), 'BraveSoftware/Brave-Browser/Application/brave.exe')
]
for path in paths:
if os.path.exists(path):
return path
return paths[0] # 返回第一个路径,即使它不存在
elif sys.platform == "darwin":
if browser_type == 'chrome':
return "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
elif browser_type == 'edge':
return "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"
elif browser_type == 'firefox':
return "/Applications/Firefox.app/Contents/MacOS/firefox"
elif browser_type == 'brave':
return "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"
else: # Linux
if browser_type == 'chrome':
# 尝试多种可能的名称
chrome_names = ["google-chrome", "chrome", "chromium", "chromium-browser"]
for name in chrome_names:
try:
import shutil
path = shutil.which(name)
if path:
return path
except:
pass
return "/usr/bin/google-chrome"
elif browser_type == 'edge':
return "/usr/bin/microsoft-edge"
elif browser_type == 'firefox':
return "/usr/bin/firefox"
elif browser_type == 'brave':
# 尝试常见的 Brave 路径
brave_names = ["brave", "brave-browser"]
for name in brave_names:
try:
import shutil
path = shutil.which(name)
if path:
return path
except:
pass
return "/usr/bin/brave-browser"
# 如果找不到指定的浏览器类型,则返回 Chrome 的路径
return get_default_browser_path('chrome')
def get_linux_cursor_path():
"""Get Linux Cursor path"""