修改默认端口为16026

This commit is contained in:
2026-07-07 13:20:52 +08:00
parent fc43e6e806
commit ef4d26f11b
10 changed files with 155 additions and 28 deletions

66
app.py
View File

@@ -364,6 +364,59 @@ async def capture_with_playwright(
return {"success": False, "error": str(e)}
async def capture_with_cdp(
url_hint: str = "",
action: str = "screenshot",
cdp_port: int = 9222
):
"""
使用 CDP 连接到已打开的 Chrome 浏览器
用于方案三:手动验证后自动截图
"""
if not PLAYWRIGHT_AVAILABLE:
return {"success": False, "error": "Playwright not installed"}
try:
async with async_playwright() as p:
# 连接到已运行的 Chrome
browser = await p.chromium.connect_over_cdp(f"http://localhost:{cdp_port}")
# 获取所有页面
contexts = browser.contexts
result_data = []
for context in contexts:
for page in context.pages:
current_url = page.url
title = await page.title()
# 如果 URL 包含提示词或者是唯一页面
if url_hint in current_url or not url_hint:
if action == "screenshot":
temp_file = CAPTURE_DIR / f"cdp_capture_{uuid.uuid4()[:8]}.png"
await page.screenshot(path=str(temp_file), full_page=True)
result_data.append({
"url": current_url,
"title": title,
"file_path": str(temp_file)
})
elif action == "html":
html = await page.content()
result_data.append({
"url": current_url,
"title": title,
"html": html
})
if result_data:
return {"success": True, "pages": result_data}
else:
return {"success": False, "error": "No matching page found"}
except Exception as e:
return {"success": False, "error": f"CDP connection failed: {str(e)}"}
def capture_webpage(
url: str,
action: str = "screenshot",
@@ -467,7 +520,8 @@ def capture():
"full_page": false,
"viewport": {"width": 1920, "height": 1080},
"wait_time": 2000,
"backend": "auto" | "agent-browser" | "playwright"
"backend": "auto" | "agent-browser" | "playwright" | "chrome-cdp",
"cdp_port": 9222 // 用于 chrome-cdp 后端,连接到已打开的 Chrome
}
"""
data = request.get_json()
@@ -490,6 +544,8 @@ def capture():
viewport = data.get("viewport")
wait_time = int(data.get("wait_time", 2000))
backend = data.get("backend", "auto")
cdp_port = int(data.get("cdp_port", 9222))
url_hint = data.get("url_hint", "")
result = capture_webpage(
url=url,
@@ -499,7 +555,9 @@ def capture():
full_page=full_page,
viewport=viewport,
wait_time=wait_time,
backend=backend
backend=backend,
)
if not result["success"]:
@@ -525,5 +583,5 @@ if __name__ == '__main__':
print(" agent-browser: npm install -g agent-browser && agent-browser install")
print(" playwright: pip install playwright && playwright install chromium")
print("\n🚀 Server running on http://0.0.0.0:16025")
app.run(host='0.0.0.0', port=16025, debug=True)
print("\n🚀 Server running on http://0.0.0.0:16026")
app.run(host='0.0.0.0', port=16026, debug=True)