Expand KDocs QR detection

This commit is contained in:
2026-01-07 13:21:19 +08:00
parent 95d7cbc825
commit d8897f893a

View File

@@ -307,6 +307,14 @@ class KDocsUploader:
"canvas[class*='qr']", "canvas[class*='qr']",
"svg", "svg",
"div[role='img']", "div[role='img']",
"div[class*='qr']",
"div[id*='qr']",
"div[class*='qrcode']",
"div[id*='qrcode']",
"div[class*='wechat']",
"div[class*='weixin']",
"div[class*='wx']",
"img[src*='wx']",
] ]
best = None best = None
best_score = None best_score = None
@@ -326,10 +334,10 @@ class KDocsUploader:
continue continue
width = box.get("width", 0) width = box.get("width", 0)
height = box.get("height", 0) height = box.get("height", 0)
if width < 120 or height < 120 or width > 420 or height > 420: if width < 80 or height < 80 or width > 520 or height > 520:
continue continue
aspect_diff = abs(width - height) aspect_diff = abs(width - height)
if aspect_diff > 40: if aspect_diff > 80:
continue continue
score = aspect_diff + abs(width - 260) + abs(height - 260) score = aspect_diff + abs(width - 260) + abs(height - 260)
if best_score is None or score < best_score: if best_score is None or score < best_score:
@@ -337,6 +345,41 @@ class KDocsUploader:
best = el best = el
except Exception: except Exception:
continue continue
if best:
return best
handle = None
try:
handle = frame.evaluate_handle(
"""() => {
const patterns = [/qr/i, /qrcode/i, /weixin/i, /wechat/i, /wx/i, /data:image/i];
const elements = Array.from(document.querySelectorAll('*'));
for (const el of elements) {
const style = window.getComputedStyle(el);
const bg = style.backgroundImage || '';
if (!bg || bg === 'none') continue;
if (!patterns.some((re) => re.test(bg))) continue;
const rect = el.getBoundingClientRect();
if (!rect.width || !rect.height) continue;
if (rect.width < 80 || rect.height < 80 || rect.width > 520 || rect.height > 520) continue;
const diff = Math.abs(rect.width - rect.height);
if (diff > 80) continue;
return el;
}
return null;
}"""
)
element = handle.as_element() if handle else None
if element:
return element
except Exception:
pass
finally:
try:
if handle:
handle.dispose()
except Exception:
pass
return best return best
def _capture_dialog_image(self, page) -> Optional[bytes]: def _capture_dialog_image(self, page) -> Optional[bytes]:
@@ -398,13 +441,30 @@ class KDocsUploader:
self._ensure_login_dialog() self._ensure_login_dialog()
qr_image = None qr_image = None
for _ in range(5): for _ in range(10):
qr_image = self._capture_qr_image() qr_image = self._capture_qr_image()
if qr_image: if qr_image:
break break
time.sleep(1) time.sleep(1)
if not qr_image: if not qr_image:
self._last_error = "二维码获取失败" self._last_error = "二维码获取失败"
try:
pages = self._iter_pages()
page_urls = [getattr(p, "url", "") for p in pages]
logger.warning(f"[KDocs] 二维码未捕获,页面: {page_urls}")
ts = int(time.time())
saved = []
for idx, page in enumerate(pages[:3]):
try:
path = f"data/kdocs_debug_{ts}_{idx}.png"
page.screenshot(path=path, full_page=True)
saved.append(path)
except Exception:
continue
if saved:
logger.warning(f"[KDocs] 已保存调试截图: {saved}")
except Exception:
pass
return {"success": False, "error": self._last_error} return {"success": False, "error": self._last_error}
self._last_qr_image = qr_image self._last_qr_image = qr_image