Files
GenericAgent/memory/web_setup_sop.md

74 lines
3.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Web 工具链初始化执行 SOP
若 web_scan 和 web_execute_js 已测试可用,无需执行此 SOP。
仅供初始安装时code_run 可用但 web 工具尚未配置的场景。
## 目标
在仅具备系统级权限code_run建立 Web 交互能力web_scan / web_execute_js
## 前置:检测浏览器
```python
import shutil, subprocess
browser = "chrome" if shutil.which("chrome") else "msedge" # Edge内置必存在Chrome可选
```
## 阶段一:安装 Tampermonkey (手动)
**状态**: 尚未实现自动化,需用户手动操作。
1.`start` 打开扩展商店页面(自动适配浏览器):
- Chrome: `start "" "https://chromewebstore.google.com/detail/篡改猴测试版/gcalenpjmijncebpfijmoaglllgpjagf"`
- Edge: `start "" "https://microsoftedge.microsoft.com/addons/detail/tampermonkey/iikmkjmpaadaobahmlepeloendndfphd"`
2. 提示用户点击"安装"并确认。
## 阶段 1.5:开启「允许运行用户脚本」
**前置**TM 已安装,但 Chrome 可能默认未开启此权限。
需打开 TM 的扩展详情页,手动开启相关开关。
### 自动打开详情页
1. 从文件系统读取 TM 扩展 ID
```python
import os, json, glob
ext_dir = os.path.expandvars(r'%LOCALAPPDATA%\Google\Chrome\User Data\Default\Extensions')
for eid in os.listdir(ext_dir):
for ver in glob.glob(os.path.join(ext_dir, eid, '*')):
mf = os.path.join(ver, 'manifest.json')
if os.path.isfile(mf):
with open(mf, encoding='utf-8') as f:
m = json.load(f)
if 'tampermonkey' in m.get('name','').lower() or 'tampermonkey' in m.get('description','').lower():
tm_id = eid; break
```
2. 导航到 `chrome://extensions/?id={tm_id}`
- ⚠️ `chrome://` 协议无法通过命令行参数或 JS(`window.open`) 打开
- ✅ 用 ljqCtrl需先打开一个 Chrome 窗口并置顶)或剪贴板+地址栏方案:
```python
# 剪贴板方案写入URL → Ctrl+L → Ctrl+V → Enter
import win32clipboard
win32clipboard.OpenClipboard(); win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(f'chrome://extensions/?id={tm_id}')
win32clipboard.CloseClipboard()
# 然后用 ljqCtrl 或 SendKeys 发送 Ctrl+L, Ctrl+V, Enter
```
3. 提示用户在详情页中开启「允许运行用户脚本」开关。
## 阶段二:安装 ljq_web_driver.user.js
**脚本路径**: `../assets/ljq_web_driver.user.js`
### 方案A自动化优先
本地 HTTP 服务器 + TM 中间页,用 `start` 命令打开:
1. Python 启动 `http.server` 托管脚本Content-Type: text/javascript
2. `start "" "https://www.tampermonkey.net/script_installation.php#url=http://127.0.0.1:{port}/ljq_web_driver.user.js"`
- ⚠️ 以上步骤均须用 `Popen` 非阻塞执行,禁止 `subprocess.run`,否则阻塞 agent
3. TM 秒弹安装确认,用户点"安装"即可
### 方案B手动 fallback
若方案A失败用剪贴板
1. 读取脚本内容 → `pyperclip.copy()`
2. 通知用户在 TM 中【新建脚本 → 全选 → 粘贴 → 保存】
## 阶段三:验证
调用 `web_scan` 或注入 JS 心跳检测,确认脚本已生效。
## 避坑 (Chromium untrusted 拦截)
- ❌ 直接导航到 `localhost/.user.js` → Chromium 弹 untrusted 拦截 + "另存为"延迟约1分钟
- ✅ 必须用 `start` 命令(系统级)打开 TM 中间页 URL → 秒弹安装,无拦截
- 此问题 Chrome 和 Edge 均存在Chromium 内核通病)