hkfires commited on
Commit
2ceecec
·
verified ·
1 Parent(s): 2221e12

feat: auto dismiss overlay

Browse files
Files changed (2) hide show
  1. browser/navigation.py +22 -0
  2. browser/ws_helper.py +193 -0
browser/navigation.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  from playwright.sync_api import Page, expect
4
  from utils.paths import logs_dir
5
  from utils.common import ensure_dir
 
6
 
7
  class KeepAliveError(Exception):
8
  pass
@@ -79,6 +80,10 @@ def handle_successful_navigation(page: Page, logger, cookie_file_config, shutdow
79
  # 等待页面加载和渲染
80
  time.sleep(15)
81
 
 
 
 
 
82
  # 添加Cookie验证计数器
83
  click_counter = 0
84
 
@@ -89,9 +94,26 @@ def handle_successful_navigation(page: Page, logger, cookie_file_config, shutdow
89
  break
90
 
91
  try:
 
 
 
92
  page.click('body')
93
  click_counter += 1
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  # 每360次点击(1小时)执行一次完整的Cookie验证
96
  if cookie_validator and click_counter >= 360: # 360 * 10秒 = 3600秒 = 1小时
97
  is_valid = cookie_validator.validate_cookies_in_main_thread()
 
3
  from playwright.sync_api import Page, expect
4
  from utils.paths import logs_dir
5
  from utils.common import ensure_dir
6
+ from browser.ws_helper import reconnect_ws, get_ws_status, dismiss_interaction_modal
7
 
8
  class KeepAliveError(Exception):
9
  pass
 
80
  # 等待页面加载和渲染
81
  time.sleep(15)
82
 
83
+ # 记录初始WS状态
84
+ last_ws_status = get_ws_status(page, logger)
85
+ logger.info(f"初始WS状态: {last_ws_status}")
86
+
87
  # 添加Cookie验证计数器
88
  click_counter = 0
89
 
 
94
  break
95
 
96
  try:
97
+ # 检测并关闭interaction-modal遮罩层(如果出现)
98
+ dismiss_interaction_modal(page, logger)
99
+
100
  page.click('body')
101
  click_counter += 1
102
 
103
+ # 检查WS状态是否发生变化
104
+ current_ws_status = get_ws_status(page, logger)
105
+ if current_ws_status != last_ws_status:
106
+ logger.warning(f"WS状态变更: {last_ws_status} -> {current_ws_status}")
107
+
108
+ # 如果不是CONNECTED状态,尝试重连
109
+ if current_ws_status != "CONNECTED":
110
+ logger.info("WS断开,尝试重连...")
111
+ reconnect_ws(page, logger)
112
+ current_ws_status = get_ws_status(page, logger)
113
+ logger.info(f"重连后WS状态: {current_ws_status}")
114
+
115
+ last_ws_status = current_ws_status
116
+
117
  # 每360次点击(1小时)执行一次完整的Cookie验证
118
  if cookie_validator and click_counter >= 360: # 360 * 10秒 = 3600秒 = 1小时
119
  is_valid = cookie_validator.validate_cookies_in_main_thread()
browser/ws_helper.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import random
3
+ from playwright.sync_api import Page, FrameLocator
4
+
5
+
6
+ def get_preview_frame(page: Page, logger=None) -> FrameLocator:
7
+ """
8
+ 获取预览iframe的FrameLocator。
9
+ """
10
+ try:
11
+ # 查找title为"Preview"的iframe
12
+ frame = page.frame_locator('iframe[title="Preview"]')
13
+ return frame
14
+ except Exception as e:
15
+ if logger:
16
+ logger.warning(f"获取Preview iframe失败: {e}")
17
+ return None
18
+
19
+
20
+ def get_ws_status(page: Page, logger=None) -> str:
21
+ """
22
+ 获取页面中WS连接状态(在iframe内部)。
23
+ 返回: CONNECTED, IDLE, CONNECTING 或 UNKNOWN
24
+ """
25
+ try:
26
+ frame = get_preview_frame(page, logger)
27
+ if not frame:
28
+ return "UNKNOWN"
29
+
30
+ # 在iframe内查找包含 "WS:" 的状态文本元素
31
+ # 根据截图,状态显示为 "WS: CONNECTED" 等格式
32
+ status_element = frame.locator('text=/WS:\\s*(CONNECTED|IDLE|CONNECTING)/i').first
33
+ if status_element.is_visible(timeout=3000):
34
+ text = status_element.text_content()
35
+ if text:
36
+ if "CONNECTED" in text.upper():
37
+ return "CONNECTED"
38
+ elif "IDLE" in text.upper():
39
+ return "IDLE"
40
+ elif "CONNECTING" in text.upper():
41
+ return "CONNECTING"
42
+ return "UNKNOWN"
43
+ except Exception as e:
44
+ if logger:
45
+ logger.warning(f"获取WS状态时出错: {e}")
46
+ return "UNKNOWN"
47
+
48
+
49
+ def click_disconnect(page: Page, logger=None) -> bool:
50
+ """
51
+ 点击Disconnect按钮断开WS连接(在iframe内部)。
52
+ """
53
+ try:
54
+ frame = get_preview_frame(page, logger)
55
+ if not frame:
56
+ return False
57
+
58
+ disconnect_btn = frame.locator('button:has-text("Disconnect")')
59
+ if disconnect_btn.count() > 0 and disconnect_btn.first.is_visible(timeout=3000):
60
+ disconnect_btn.first.click(timeout=5000)
61
+ if logger:
62
+ logger.info("已点击 Disconnect 按钮")
63
+ time.sleep(1)
64
+ return True
65
+ if logger:
66
+ logger.warning("未找到可见的 Disconnect 按钮")
67
+ return False
68
+ except Exception as e:
69
+ if logger:
70
+ logger.warning(f"点击 Disconnect 按钮失败: {e}")
71
+ return False
72
+
73
+
74
+ def click_connect(page: Page, logger=None) -> bool:
75
+ """
76
+ 点击Connect按钮建立WS连接(在iframe内部)。
77
+ """
78
+ try:
79
+ frame = get_preview_frame(page, logger)
80
+ if not frame:
81
+ return False
82
+
83
+ connect_btn = frame.locator('button:has-text("Connect")')
84
+ if connect_btn.count() > 0 and connect_btn.first.is_visible(timeout=3000):
85
+ connect_btn.first.click(timeout=5000)
86
+ if logger:
87
+ logger.info("已点击 Connect 按钮")
88
+ time.sleep(1)
89
+ return True
90
+ if logger:
91
+ logger.warning("未找到可见的 Connect 按钮")
92
+ return False
93
+ except Exception as e:
94
+ if logger:
95
+ logger.warning(f"点击 Connect 按钮失败: {e}")
96
+ return False
97
+
98
+
99
+ def wait_for_ws_connected(page: Page, logger=None, timeout: int = 30) -> bool:
100
+ """
101
+ 等待WS状态变为CONNECTED。
102
+ """
103
+ start_time = time.time()
104
+ while time.time() - start_time < timeout:
105
+ status = get_ws_status(page, logger)
106
+ if status == "CONNECTED":
107
+ return True
108
+ time.sleep(1)
109
+ return False
110
+
111
+
112
+ def reconnect_ws(page: Page, logger=None) -> str:
113
+ """
114
+ 执行断开再连接的流程,并返回最终WS状态。
115
+ 流程:关闭遮罩 -> Disconnect -> 等待IDLE -> Connect -> 等待CONNECTED -> 获取状态
116
+ """
117
+ if logger:
118
+ logger.info("开始执行WS重连流程: Disconnect -> Connect")
119
+
120
+ # 先关闭 interaction-modal 遮罩层(如果存在)
121
+ dismiss_interaction_modal(page, logger)
122
+
123
+ # 先断开连接
124
+ click_disconnect(page, logger)
125
+ time.sleep(2)
126
+
127
+ # 检查是否变为IDLE
128
+ status = get_ws_status(page, logger)
129
+ if logger:
130
+ logger.info(f"断开后WS状态: {status}")
131
+
132
+ # 再连接
133
+ click_connect(page, logger)
134
+ time.sleep(2)
135
+
136
+ # 等待连接成功
137
+ if wait_for_ws_connected(page, logger, timeout=15):
138
+ status = get_ws_status(page, logger)
139
+ if logger:
140
+ logger.info(f"重连后WS状态: {status}")
141
+ return status
142
+ else:
143
+ status = get_ws_status(page, logger)
144
+ if logger:
145
+ logger.warning(f"WS重连超时,当前状态: {status}")
146
+ return status
147
+
148
+
149
+ def dismiss_interaction_modal(page: Page, logger=None) -> bool:
150
+ """
151
+ 检测并关闭 interaction-modal 遮罩层。
152
+ 通过在 iframe 区域内模拟鼠标移动来触发遮罩层关闭。
153
+
154
+ 返回: True 如果成功关闭遮罩,False 如果未找到遮罩或关闭失败
155
+ """
156
+ try:
157
+ modal = page.locator('div.interaction-modal')
158
+ if modal.count() == 0 or not modal.first.is_visible(timeout=500):
159
+ return False
160
+
161
+ if logger:
162
+ logger.info("检测到 interaction-modal 遮罩层,尝试关闭...")
163
+
164
+ iframe = page.locator('iframe[title="Preview"]')
165
+ if iframe.count() > 0:
166
+ iframe_box = iframe.first.bounding_box()
167
+ if iframe_box:
168
+ # 随机起点
169
+ curr_x = iframe_box['x'] + random.randint(50, int(iframe_box['width']) - 50)
170
+ curr_y = iframe_box['y'] + random.randint(50, int(iframe_box['height']) - 50)
171
+
172
+ # 持续连续移动直到遮罩关闭,最多尝试30次
173
+ for i in range(30):
174
+ # 从当前位置随机移动一段距离
175
+ delta_x = random.randint(-30, 30)
176
+ delta_y = random.randint(-20, 20)
177
+ curr_x = max(iframe_box['x'] + 20, min(iframe_box['x'] + iframe_box['width'] - 20, curr_x + delta_x))
178
+ curr_y = max(iframe_box['y'] + 20, min(iframe_box['y'] + iframe_box['height'] - 20, curr_y + delta_y))
179
+
180
+ page.mouse.move(curr_x, curr_y)
181
+ time.sleep(0.05)
182
+
183
+ # 每次移动后检查遮罩是否关闭
184
+ if modal.count() == 0 or not modal.first.is_visible(timeout=100):
185
+ if logger:
186
+ logger.info("已成功关闭 interaction-modal 遮罩层")
187
+ return True
188
+
189
+ return False
190
+ except Exception as e:
191
+ if logger:
192
+ logger.debug(f"关闭 interaction-modal 时出错: {e}")
193
+ return False