6ed762f7 by 柴进

客户唤醒营销工具迭代,不再进行频繁的浏览器关闭

1 parent 9d72c970
......@@ -30,16 +30,34 @@ from selenium.common.exceptions import TimeoutException, NoSuchElementException
class ChromeController:
"""Chrome浏览器控制器"""
"""Chrome浏览器控制器 - 单例模式,全局共享浏览器实例"""
_instance = None # 单例实例
def __new__(cls):
"""单例模式: 确保全局只有一个浏览器实例"""
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
# 防止重复初始化
if hasattr(self, '_initialized'):
return
self.driver = None
self.wait = None
self.network_logs = []
self.is_running = False
self._initialized = True
def start_browser(self) -> bool:
"""启动Chrome浏览器"""
"""启动Chrome浏览器 - 如果已启动则跳过"""
# 如果浏览器已经运行,直接返回成功
if self.is_running and self.driver:
print("Chrome浏览器已在运行,复用现有实例")
return True
try:
if USING_UNDETECTED:
# 使用undetected-chromedriver(推荐)
......@@ -84,15 +102,23 @@ class ChromeController:
return False
def stop_browser(self):
"""关闭浏览器"""
"""关闭浏览器 - 仅在程序退出时调用"""
try:
if self.driver:
self.driver.quit()
self.driver = None
self.is_running = False
print("Chrome浏览器已关闭")
except Exception as e:
print(f"关闭浏览器异常: {e}")
def ensure_browser_running(self) -> bool:
"""确保浏览器正在运行 - 供外部调用"""
if not self.is_running or not self.driver:
print("检测到浏览器未运行,正在启动...")
return self.start_browser()
return True
def navigate_to(self, url: str) -> bool:
"""导航到指定URL"""
try:
......
......@@ -46,7 +46,8 @@ class EtsyManager:
}
try:
if not self.chrome.start_browser():
# 确保浏览器运行
if not self.chrome.ensure_browser_running():
return result
# 导航到消息页面
......@@ -83,15 +84,14 @@ class EtsyManager:
except Exception as e:
print(f"登录状态检查异常: {e}")
finally:
self.chrome.stop_browser()
return result
def get_user_tags(self) -> Optional[Dict[str, int]]:
"""获取用户标签"""
try:
if not self.chrome.start_browser():
# 确保浏览器运行
if not self.chrome.ensure_browser_running():
return None
if not self.chrome.navigate_to(self.PAGES["messages"]):
......@@ -117,8 +117,6 @@ class EtsyManager:
except Exception as e:
print(f"获取用户标签异常: {e}")
finally:
self.chrome.stop_browser()
return None
......@@ -130,7 +128,8 @@ class EtsyManager:
daily_limit = self.config.get("limits.daily_limit", 100)
try:
if not self.chrome.start_browser():
# 确保浏览器运行
if not self.chrome.ensure_browser_running():
status_callback("浏览器启动失败")
return
......@@ -175,7 +174,6 @@ class EtsyManager:
except Exception as e:
status_callback(f"对话营销异常: {e}")
finally:
self.chrome.stop_browser()
self.is_marketing_active = False
def start_order_marketing(self, start_date: str, end_date: str,
......@@ -185,7 +183,8 @@ class EtsyManager:
sent_count = 0
try:
if not self.chrome.start_browser():
# 确保浏览器运行
if not self.chrome.ensure_browser_running():
status_callback("浏览器启动失败")
return
......@@ -223,7 +222,6 @@ class EtsyManager:
except Exception as e:
status_callback(f"订单营销异常: {e}")
finally:
self.chrome.stop_browser()
self.is_marketing_active = False
def stop_marketing(self):
......
......@@ -29,6 +29,21 @@ class MainWindow:
self.create_widgets()
self.load_config_data()
# 绑定窗口关闭事件
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
def on_closing(self):
"""窗口关闭事件处理"""
from ..etsy.chrome_controller import ChromeController
# 停止浏览器
chrome = ChromeController()
if chrome.is_running:
chrome.stop_browser()
# 关闭窗口
self.root.destroy()
def setup_window(self):
"""设置窗口属性"""
self.root.title("Etsy客户营销工具 v1.0")
......