1 2 3 4 5 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 62 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| import re import time import threading import ctypes from ctypes import wintypes import win32gui import win32con import win32api from typing import List, Dict, Optional from RPA.Desktop import Desktop
user32 = ctypes.WinDLL("user32", use_last_error=True)
desktop = Desktop()
class PopupPattern: def __init__(self, title_regex: str = ".*", class_name: Optional[str] = None, min_w: int = 200, min_h: int = 80): self.title_regex = re.compile(title_regex) self.class_name = class_name self.min_w = min_w self.min_h = min_h
class InterceptMetrics: def __init__(self): self.total_seen = 0 self.total_handled = 0 self.total_skipped = 0 self.handle_durations: List[float] = [] self.handle_drift = 0
class EphemeralPopupInterceptor: def __init__(self, patterns: List[PopupPattern], interval_ms: int = 150, ttl_ms: int = 3000): self.patterns = patterns self.interval_ms = interval_ms self.ttl_ms = ttl_ms self._stop = threading.Event() self._thread: Optional[threading.Thread] = None self._recent: Dict[int, float] = {} self.metrics = InterceptMetrics()
def _bring_foreground(self, hwnd: int) -> bool: try: win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL) ok = user32.SetForegroundWindow(hwnd) return bool(ok) except Exception: return False
def _press_enter(self): win32api.keybd_event(win32con.VK_RETURN, 0, 0, 0) time.sleep(0.02) win32api.keybd_event(win32con.VK_RETURN, 0, win32con.KEYEVENTF_KEYUP, 0)
def _match(self, hwnd: int) -> bool: try: if not win32gui.IsWindowVisible(hwnd): return False title = win32gui.GetWindowText(hwnd) or "" cls = win32gui.GetClassName(hwnd) or "" left, top, right, bottom = win32gui.GetWindowRect(hwnd) w, h = right - left, bottom - top for p in self.patterns: if p.class_name and p.class_name != cls: continue if not p.title_regex.match(title): continue if w < p.min_w or h < p.min_h: continue return True return False except Exception: return False
def _handle(self, hwnd: int) -> bool: start = time.time() seen_ts = self._recent.get(hwnd) now = time.time() * 1000 if seen_ts and (now - seen_ts) < self.ttl_ms: self.metrics.total_skipped += 1 return False self._recent[hwnd] = now self.metrics.total_seen += 1
fg_ok = self._bring_foreground(hwnd) if not fg_ok: self._press_enter() self.metrics.total_handled += 1 self.metrics.handle_durations.append(time.time() - start) return True
self._press_enter() self.metrics.total_handled += 1 self.metrics.handle_durations.append(time.time() - start) return True
def _loop(self): while not self._stop.is_set(): try: to_check = [] def enum_cb(hwnd, lparam): to_check.append(hwnd) return True win32gui.EnumWindows(enum_cb, None) for hwnd in to_check: if self._match(hwnd): try: self._handle(hwnd) except Exception: pass now = time.time() * 1000 self._recent = {h: ts for h, ts in self._recent.items() if now - ts < self.ttl_ms} except Exception: pass time.sleep(self.interval_ms / 1000.0)
def start(self): if self._thread and self._thread.is_alive(): return self._stop.clear() self._thread = threading.Thread(target=self._loop, name="popup-interceptor", daemon=True) self._thread.start()
def stop(self): if not self._thread: return self._stop.set() self._thread.join(timeout=2.0)
def intercept_once(self, timeout: float = 2.0) -> bool: end = time.time() + timeout while time.time() < end: to_check = [] win32gui.EnumWindows(lambda h, p: (to_check.append(h) or True), None) for hwnd in to_check: if self._match(hwnd) and self._handle(hwnd): return True time.sleep(self.interval_ms / 1000.0) return False
if __name__ == "__main__": patterns = [ PopupPattern(title_regex=r".*错误.*|.*失败.*|.*提示.*"), PopupPattern(title_regex=r".*确认.*|.*Confirm.*"), PopupPattern(title_regex=r".*保存.*|.*覆盖.*"), PopupPattern(title_regex=r".*", class_name="#32770"), ]
interceptor = EphemeralPopupInterceptor(patterns, interval_ms=120, ttl_ms=3000) interceptor.start()
try: handled = interceptor.intercept_once(timeout=1.5) print("弹窗是否被处理:", handled) finally: interceptor.stop() dur = interceptor.metrics.handle_durations print({ "seen": interceptor.metrics.total_seen, "handled": interceptor.metrics.total_handled, "skipped": interceptor.metrics.total_skipped, "p50_dur_ms": int((sorted(dur)[len(dur)//2]*1000) if dur else 0), })
|