fix: 修复 Chrome 用户数据目录权限问题

This commit is contained in:
along 2025-04-27 22:42:56 +08:00
parent d365e86952
commit 8d7407c72e
2 changed files with 27 additions and 1 deletions

View File

@ -640,7 +640,9 @@
"found_chrome_at": "找到 Chrome: {path}", "found_chrome_at": "找到 Chrome: {path}",
"found_browser_user_data_dir": "找到 {browser} 用户数据目录: {path}", "found_browser_user_data_dir": "找到 {browser} 用户数据目录: {path}",
"select_profile": "选择要使用的 {browser} 配置文件:", "select_profile": "选择要使用的 {browser} 配置文件:",
"profile_list": "可用 {browser} 配置文件:" "profile_list": "可用 {browser} 配置文件:",
"chrome_permissions_fixed": "已修复 Chrome 用户数据目录权限",
"chrome_permissions_fix_failed": "修复 Chrome 权限失败: {error}"
}, },
"browser_profile": { "browser_profile": {
"title": "浏览器配置文件选择", "title": "浏览器配置文件选择",

View File

@ -483,6 +483,25 @@ class OAuthHandler:
print(f"{Fore.RED}{EMOJI['ERROR']} {self.translator.get('oauth.error_configuring_browser_options', error=str(e)) if self.translator else f'Error configuring browser options: {e}'}{Style.RESET_ALL}") print(f"{Fore.RED}{EMOJI['ERROR']} {self.translator.get('oauth.error_configuring_browser_options', error=str(e)) if self.translator else f'Error configuring browser options: {e}'}{Style.RESET_ALL}")
raise raise
def _fix_chrome_permissions(self, user_data_dir):
"""Fix permissions for Chrome user data directory"""
try:
if sys.platform == 'darwin': # macOS
import subprocess
import pwd
# Get current user
current_user = pwd.getpwuid(os.getuid()).pw_name
# Fix permissions for Chrome directory
chrome_dir = os.path.expanduser('~/Library/Application Support/Google/Chrome')
if os.path.exists(chrome_dir):
subprocess.run(['chmod', '-R', 'u+rwX', chrome_dir])
subprocess.run(['chown', '-R', f'{current_user}:staff', chrome_dir])
print(f"{Fore.GREEN}{EMOJI['SUCCESS']} {self.translator.get('oauth.chrome_permissions_fixed') if self.translator else 'Fixed Chrome user data directory permissions'}{Style.RESET_ALL}")
except Exception as e:
print(f"{Fore.YELLOW}{EMOJI['WARNING']} {self.translator.get('oauth.chrome_permissions_fix_failed', error=str(e)) if self.translator else f'Failed to fix Chrome permissions: {str(e)}'}{Style.RESET_ALL}")
def handle_google_auth(self): def handle_google_auth(self):
"""Handle Google OAuth authentication""" """Handle Google OAuth authentication"""
try: try:
@ -493,6 +512,9 @@ class OAuthHandler:
print(f"{Fore.RED}{EMOJI['ERROR']} {self.translator.get('oauth.browser_failed') if self.translator else 'Browser failed to initialize'}{Style.RESET_ALL}") print(f"{Fore.RED}{EMOJI['ERROR']} {self.translator.get('oauth.browser_failed') if self.translator else 'Browser failed to initialize'}{Style.RESET_ALL}")
return False, None return False, None
# Get user data directory for later use
user_data_dir = self._get_user_data_directory()
# Navigate to auth URL # Navigate to auth URL
try: try:
print(f"{Fore.CYAN}{EMOJI['INFO']} {self.translator.get('oauth.navigating_to_authentication_page') if self.translator else 'Navigating to authentication page...'}{Style.RESET_ALL}") print(f"{Fore.CYAN}{EMOJI['INFO']} {self.translator.get('oauth.navigating_to_authentication_page') if self.translator else 'Navigating to authentication page...'}{Style.RESET_ALL}")
@ -556,6 +578,8 @@ class OAuthHandler:
try: try:
if self.browser: if self.browser:
self.browser.quit() self.browser.quit()
# Fix Chrome permissions after browser is closed
self._fix_chrome_permissions(user_data_dir)
except: except:
pass pass