agent-webview 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agent_webview-0.1.0/.github/dependabot.yml +10 -0
- agent_webview-0.1.0/.github/workflows/ci.yml +42 -0
- agent_webview-0.1.0/.github/workflows/publish.yml +41 -0
- agent_webview-0.1.0/.gitignore +19 -0
- agent_webview-0.1.0/CHANGELOG.md +29 -0
- agent_webview-0.1.0/CONTRIBUTING.md +18 -0
- agent_webview-0.1.0/LICENSE +21 -0
- agent_webview-0.1.0/PKG-INFO +397 -0
- agent_webview-0.1.0/README.md +345 -0
- agent_webview-0.1.0/RELEASING.md +29 -0
- agent_webview-0.1.0/SECURITY.md +18 -0
- agent_webview-0.1.0/docs/api-stability.md +23 -0
- agent_webview-0.1.0/examples/debug_page.html +23 -0
- agent_webview-0.1.0/pyproject.toml +94 -0
- agent_webview-0.1.0/scripts/export_public_repo.py +77 -0
- agent_webview-0.1.0/scripts/release_check.py +283 -0
- agent_webview-0.1.0/scripts/smoke_service.py +190 -0
- agent_webview-0.1.0/src/agent_webview/__init__.py +3 -0
- agent_webview-0.1.0/src/agent_webview/__main__.py +4 -0
- agent_webview-0.1.0/src/agent_webview/auth.py +19 -0
- agent_webview-0.1.0/src/agent_webview/bridge.py +94 -0
- agent_webview-0.1.0/src/agent_webview/cli.py +211 -0
- agent_webview-0.1.0/src/agent_webview/controller_api.py +319 -0
- agent_webview-0.1.0/src/agent_webview/cookies.py +131 -0
- agent_webview-0.1.0/src/agent_webview/errors.py +14 -0
- agent_webview-0.1.0/src/agent_webview/events.py +83 -0
- agent_webview-0.1.0/src/agent_webview/files.py +68 -0
- agent_webview-0.1.0/src/agent_webview/http_errors.py +24 -0
- agent_webview-0.1.0/src/agent_webview/js.py +618 -0
- agent_webview-0.1.0/src/agent_webview/logging.py +34 -0
- agent_webview-0.1.0/src/agent_webview/models.py +224 -0
- agent_webview-0.1.0/src/agent_webview/runtime.py +290 -0
- agent_webview-0.1.0/src/agent_webview/sessions.py +293 -0
- agent_webview-0.1.0/src/agent_webview/worker.py +141 -0
- agent_webview-0.1.0/src/agent_webview/worker_api.py +171 -0
- agent_webview-0.1.0/tests/test_bridge.py +11 -0
- agent_webview-0.1.0/tests/test_cli.py +86 -0
- agent_webview-0.1.0/tests/test_controller_api.py +141 -0
- agent_webview-0.1.0/tests/test_cookies.py +61 -0
- agent_webview-0.1.0/tests/test_events.py +27 -0
- agent_webview-0.1.0/tests/test_files.py +37 -0
- agent_webview-0.1.0/tests/test_js.py +44 -0
- agent_webview-0.1.0/tests/test_models.py +21 -0
- agent_webview-0.1.0/tests/test_runtime.py +167 -0
- agent_webview-0.1.0/tests/test_sessions.py +122 -0
- agent_webview-0.1.0/tests/test_worker_api.py +93 -0
- agent_webview-0.1.0/uv.lock +2086 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
pull_request:
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
test:
|
|
12
|
+
name: Python ${{ matrix.python }} / ${{ matrix.os }}
|
|
13
|
+
runs-on: ${{ matrix.os }}
|
|
14
|
+
strategy:
|
|
15
|
+
fail-fast: false
|
|
16
|
+
matrix:
|
|
17
|
+
os: [ubuntu-latest, windows-latest, macos-latest]
|
|
18
|
+
python: ["3.10", "3.13"]
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
|
21
|
+
- uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6
|
|
22
|
+
with:
|
|
23
|
+
python-version: ${{ matrix.python }}
|
|
24
|
+
enable-cache: true
|
|
25
|
+
- run: uv sync --extra dev
|
|
26
|
+
- run: uv run ruff check src tests scripts
|
|
27
|
+
- run: uv run mypy
|
|
28
|
+
- run: >-
|
|
29
|
+
uv run pytest
|
|
30
|
+
--cov=agent_webview
|
|
31
|
+
--cov-report=term
|
|
32
|
+
--cov-fail-under=70
|
|
33
|
+
|
|
34
|
+
release-check:
|
|
35
|
+
runs-on: ubuntu-latest
|
|
36
|
+
steps:
|
|
37
|
+
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
|
38
|
+
- uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6
|
|
39
|
+
with:
|
|
40
|
+
python-version: "3.13"
|
|
41
|
+
enable-cache: true
|
|
42
|
+
- run: python scripts/release_check.py
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
|
15
|
+
with:
|
|
16
|
+
ref: ${{ github.event.release.tag_name }}
|
|
17
|
+
- uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6
|
|
18
|
+
with:
|
|
19
|
+
python-version: "3.13"
|
|
20
|
+
- run: python scripts/release_check.py
|
|
21
|
+
- run: uv build --out-dir dist
|
|
22
|
+
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
|
23
|
+
with:
|
|
24
|
+
name: python-package
|
|
25
|
+
path: dist/
|
|
26
|
+
if-no-files-found: error
|
|
27
|
+
|
|
28
|
+
publish:
|
|
29
|
+
needs: build
|
|
30
|
+
runs-on: ubuntu-latest
|
|
31
|
+
environment:
|
|
32
|
+
name: pypi
|
|
33
|
+
url: https://pypi.org/p/agent-webview
|
|
34
|
+
permissions:
|
|
35
|
+
id-token: write
|
|
36
|
+
steps:
|
|
37
|
+
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
|
|
38
|
+
with:
|
|
39
|
+
name: python-package
|
|
40
|
+
path: dist/
|
|
41
|
+
- uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # release/v1
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/.venv/
|
|
2
|
+
/.pytest_cache/
|
|
3
|
+
/.coverage
|
|
4
|
+
/htmlcov/
|
|
5
|
+
/dist/
|
|
6
|
+
/build/
|
|
7
|
+
/*.egg-info/
|
|
8
|
+
/.agent-webview-runtime.json
|
|
9
|
+
/.agent-webview-runtime.json.lock
|
|
10
|
+
/.agent-webview-data/
|
|
11
|
+
/src/agent_webview/__pycache__/
|
|
12
|
+
/tests/__pycache__/
|
|
13
|
+
*.pyc
|
|
14
|
+
*.har
|
|
15
|
+
*.storage-state.json
|
|
16
|
+
.env
|
|
17
|
+
.env.*
|
|
18
|
+
!.env.example
|
|
19
|
+
.DS_Store
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# 变更日志
|
|
2
|
+
|
|
3
|
+
本项目遵循 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/) 和语义化版本。
|
|
4
|
+
|
|
5
|
+
## [Unreleased]
|
|
6
|
+
|
|
7
|
+
## [0.1.0] - 2026-08-09
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- 增加公共 API 兼容策略、发布检查和跨平台持续集成。
|
|
12
|
+
- 增加发布前依赖漏洞审计和干净环境安装验证。
|
|
13
|
+
- 增加源码敏感信息扫描。
|
|
14
|
+
- 增加静态类型检查门禁。
|
|
15
|
+
- 为控制器核心接口增加明确的响应模型。
|
|
16
|
+
- 增加 Linux GTK 与 Qt 可选依赖。
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- 默认数据和运行文件迁移到操作系统当前用户目录。
|
|
21
|
+
- 版本号改为单一来源。
|
|
22
|
+
- 移除 pywebview 无法跨平台可靠处理的 `data:` URL 输入。
|
|
23
|
+
|
|
24
|
+
### Security
|
|
25
|
+
|
|
26
|
+
- Token、worker 配置和 Cookie 快照在 POSIX 平台使用 `0600` 权限。
|
|
27
|
+
- 敏感目录在 POSIX 平台使用 `0700` 权限。
|
|
28
|
+
- 监听非本机地址必须显式使用 `--allow-remote`。
|
|
29
|
+
- 并发控制器不能再覆盖或删除彼此的运行信息文件。
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# 参与开发
|
|
2
|
+
|
|
3
|
+
需要 Python 3.10 以上版本和 [uv](https://docs.astral.sh/uv/)。
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
uv sync --extra dev
|
|
7
|
+
uv run ruff check src tests
|
|
8
|
+
uv run mypy
|
|
9
|
+
uv run pytest
|
|
10
|
+
python scripts/release_check.py
|
|
11
|
+
uv run python scripts/smoke_service.py
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
代码注释、日志、调试输出和用户可见错误使用简短中文。浏览器页面规则、账号流程
|
|
15
|
+
和具体平台业务不得进入本包。
|
|
16
|
+
|
|
17
|
+
提交前请确认没有加入 Cookie、Token、Storage State、HAR、请求日志、运行信息文件
|
|
18
|
+
或真实业务域名。
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 agent-webview contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agent-webview
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A hidden pywebview browser controlled through an agent-friendly HTTP debugging API.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Yuv96/agent-webview
|
|
6
|
+
Project-URL: Repository, https://github.com/Yuv96/agent-webview.git
|
|
7
|
+
Project-URL: Issues, https://github.com/Yuv96/agent-webview/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/Yuv96/agent-webview/blob/main/CHANGELOG.md
|
|
9
|
+
Project-URL: Security, https://github.com/Yuv96/agent-webview/security/policy
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: agent,browser,debugging,pywebview,webview
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Environment :: MacOS X
|
|
15
|
+
Classifier: Environment :: Win32 (MS Windows)
|
|
16
|
+
Classifier: Framework :: FastAPI
|
|
17
|
+
Classifier: Intended Audience :: Developers
|
|
18
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
19
|
+
Classifier: Operating System :: MacOS
|
|
20
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
21
|
+
Classifier: Programming Language :: Python :: 3
|
|
22
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
27
|
+
Classifier: Topic :: Software Development :: Debuggers
|
|
28
|
+
Requires-Python: >=3.10
|
|
29
|
+
Requires-Dist: fastapi<1,>=0.115
|
|
30
|
+
Requires-Dist: filelock<4,>=3.16
|
|
31
|
+
Requires-Dist: httpx<1,>=0.28
|
|
32
|
+
Requires-Dist: platformdirs<5,>=4.3
|
|
33
|
+
Requires-Dist: psutil<8,>=6.1
|
|
34
|
+
Requires-Dist: pydantic<3,>=2.9
|
|
35
|
+
Requires-Dist: pywebview<7,>=6.2.1
|
|
36
|
+
Requires-Dist: structlog<26,>=25.1
|
|
37
|
+
Requires-Dist: uvicorn<1,>=0.34
|
|
38
|
+
Provides-Extra: dev
|
|
39
|
+
Requires-Dist: detect-secrets<2,>=1.5; extra == 'dev'
|
|
40
|
+
Requires-Dist: httpx2<3,>=2; extra == 'dev'
|
|
41
|
+
Requires-Dist: mypy<2,>=1.17; extra == 'dev'
|
|
42
|
+
Requires-Dist: pip-audit<3,>=2.9; extra == 'dev'
|
|
43
|
+
Requires-Dist: pytest-cov<8,>=6; extra == 'dev'
|
|
44
|
+
Requires-Dist: pytest<10,>=8.3; extra == 'dev'
|
|
45
|
+
Requires-Dist: ruff<1,>=0.12; extra == 'dev'
|
|
46
|
+
Requires-Dist: twine<7,>=6; extra == 'dev'
|
|
47
|
+
Provides-Extra: linux-gtk
|
|
48
|
+
Requires-Dist: pywebview[gtk]<7,>=6.2.1; (sys_platform == 'linux') and extra == 'linux-gtk'
|
|
49
|
+
Provides-Extra: linux-qt
|
|
50
|
+
Requires-Dist: pywebview[qt]<7,>=6.2.1; (sys_platform == 'linux') and extra == 'linux-qt'
|
|
51
|
+
Description-Content-Type: text/markdown
|
|
52
|
+
|
|
53
|
+
# Agent Webview
|
|
54
|
+
|
|
55
|
+
一个面向 Agent 的本地网页调试服务。控制器通过 HTTP 管理多个独立的
|
|
56
|
+
pywebview worker 进程,每个调试会话都使用全新的无痕浏览器进程。
|
|
57
|
+
|
|
58
|
+
## 核心行为
|
|
59
|
+
|
|
60
|
+
- 控制器本身不创建可见窗口。
|
|
61
|
+
- `POST /v1/sessions` 每次创建一个新的 worker 进程和无痕窗口。
|
|
62
|
+
- 新窗口默认隐藏,设置 `visible: true` 才会显示。
|
|
63
|
+
- 每个会话返回 `session_id`、`window_id`、`native_window_id` 和 `pid`。
|
|
64
|
+
- 多个 Agent 可以创建多个会话并行调试,彼此不共享进程内 Cookie。
|
|
65
|
+
- 会话内的后续 HTTP 操作复用该窗口,直到删除会话。
|
|
66
|
+
- Cookie 快照保存为 JSON,可在新会话中尽力恢复。
|
|
67
|
+
- `/docs` 和 `/openapi.json` 可供 Agent 自动发现接口。
|
|
68
|
+
|
|
69
|
+
## 安装
|
|
70
|
+
|
|
71
|
+
需要 Python 3.10 或更高版本。
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
python -m pip install agent-webview
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
项目的 `requirements.txt` 建议固定在同一次版本系列:
|
|
78
|
+
|
|
79
|
+
```text
|
|
80
|
+
agent-webview~=0.1.0
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
正式发布前也可以固定到独立仓库的不可变提交:
|
|
84
|
+
|
|
85
|
+
```text
|
|
86
|
+
agent-webview @ git+https://github.com/Yuv96/agent-webview.git@COMMIT_SHA
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
发行包名、Python 模块名和命令名分别是:
|
|
90
|
+
|
|
91
|
+
- `pip install agent-webview`
|
|
92
|
+
- `import agent_webview`
|
|
93
|
+
- `agent-webview`
|
|
94
|
+
|
|
95
|
+
平台说明:
|
|
96
|
+
|
|
97
|
+
- Windows 使用 WebView2,需要已安装 WebView2 Runtime;现代 Windows 通常已自带。
|
|
98
|
+
- macOS 使用系统 WebKit。
|
|
99
|
+
- Linux 需要自行选择 pywebview 后端,例如
|
|
100
|
+
`pip install "agent-webview[linux-gtk]"` 或
|
|
101
|
+
`pip install "agent-webview[linux-qt]"`。
|
|
102
|
+
|
|
103
|
+
## 启动
|
|
104
|
+
|
|
105
|
+
```powershell
|
|
106
|
+
agent-webview
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
默认监听 `http://127.0.0.1:8765`。服务会生成包含地址、进程号和 Bearer Token
|
|
110
|
+
的运行信息文件。默认文件位于操作系统的
|
|
111
|
+
当前用户运行目录,不会写入业务项目。可用下面的命令查询路径:
|
|
112
|
+
|
|
113
|
+
```powershell
|
|
114
|
+
agent-webview --print-runtime-file
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
需要项目自定义路径时显式传入:
|
|
118
|
+
|
|
119
|
+
```powershell
|
|
120
|
+
agent-webview --runtime-file .agent-webview-runtime.json
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
不要把服务直接暴露到不可信网络。调试接口允许执行任意页面 JavaScript,
|
|
124
|
+
Cookie 快照也可能包含敏感信息。默认只允许监听本机地址;监听其他地址必须同时
|
|
125
|
+
传入 `--allow-remote`。固定令牌时优先使用 `AGENT_WEBVIEW_TOKEN` 环境变量,
|
|
126
|
+
避免令牌出现在命令历史和进程参数中。
|
|
127
|
+
|
|
128
|
+
## Agent 快速流程
|
|
129
|
+
|
|
130
|
+
以下 PowerShell 示例先读取运行信息:
|
|
131
|
+
|
|
132
|
+
```powershell
|
|
133
|
+
$runtimeFile = agent-webview --print-runtime-file
|
|
134
|
+
$runtime = Get-Content $runtimeFile | ConvertFrom-Json
|
|
135
|
+
$headers = @{ Authorization = "Bearer $($runtime.token)" }
|
|
136
|
+
$base = $runtime.base_url
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### 1. 创建隐藏的隔离会话
|
|
140
|
+
|
|
141
|
+
```powershell
|
|
142
|
+
$session = Invoke-RestMethod `
|
|
143
|
+
-Method Post `
|
|
144
|
+
-Uri "$base/v1/sessions" `
|
|
145
|
+
-Headers $headers `
|
|
146
|
+
-ContentType "application/json" `
|
|
147
|
+
-Body (@{
|
|
148
|
+
url = "https://example.com"
|
|
149
|
+
visible = $false
|
|
150
|
+
} | ConvertTo-Json)
|
|
151
|
+
|
|
152
|
+
$sid = $session.session_id
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
响应包含:
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
{
|
|
159
|
+
"session_id": "会话标识",
|
|
160
|
+
"window_id": "稳定窗口标识",
|
|
161
|
+
"pid": 12345,
|
|
162
|
+
"launcher_pid": 12340,
|
|
163
|
+
"state": "running",
|
|
164
|
+
"window": {
|
|
165
|
+
"native_window_id": 67890,
|
|
166
|
+
"ready": true,
|
|
167
|
+
"visible": false,
|
|
168
|
+
"url": "https://example.com/"
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
`pid` 是实际 pywebview worker 进程。Windows 虚拟环境启动器产生额外进程时,
|
|
174
|
+
`launcher_pid` 会返回启动器进程号,否则为 `null`。
|
|
175
|
+
|
|
176
|
+
创建响应中的 `window` 可能暂时为 `null`。轮询
|
|
177
|
+
`GET /v1/sessions/{session_id}`,直到 `window.ready` 为 `true`。
|
|
178
|
+
|
|
179
|
+
### 2. 查询 DOM
|
|
180
|
+
|
|
181
|
+
```powershell
|
|
182
|
+
Invoke-RestMethod `
|
|
183
|
+
-Method Post `
|
|
184
|
+
-Uri "$base/v1/sessions/$sid/dom/query" `
|
|
185
|
+
-Headers $headers `
|
|
186
|
+
-ContentType "application/json" `
|
|
187
|
+
-Body '{"selector":"a","limit":20}'
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### 3. 模拟输入和点击
|
|
191
|
+
|
|
192
|
+
```powershell
|
|
193
|
+
Invoke-RestMethod `
|
|
194
|
+
-Method Post `
|
|
195
|
+
-Uri "$base/v1/sessions/$sid/dom/input" `
|
|
196
|
+
-Headers $headers `
|
|
197
|
+
-ContentType "application/json" `
|
|
198
|
+
-Body '{"selector":"input[name=q]","value":"pywebview"}'
|
|
199
|
+
|
|
200
|
+
Invoke-RestMethod `
|
|
201
|
+
-Method Post `
|
|
202
|
+
-Uri "$base/v1/sessions/$sid/dom/click" `
|
|
203
|
+
-Headers $headers `
|
|
204
|
+
-ContentType "application/json" `
|
|
205
|
+
-Body '{"selector":"button[type=submit]"}'
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
输入操作会设置元素值,并触发冒泡的 `input` 和 `change` 事件。点击操作使用
|
|
209
|
+
DOM `click()`,不等同于操作系统级鼠标事件。
|
|
210
|
+
|
|
211
|
+
### 4. 注入 JavaScript
|
|
212
|
+
|
|
213
|
+
返回表达式结果:
|
|
214
|
+
|
|
215
|
+
```powershell
|
|
216
|
+
$body = @{
|
|
217
|
+
code = "({title: document.title, url: location.href})"
|
|
218
|
+
timeout = 30
|
|
219
|
+
} | ConvertTo-Json
|
|
220
|
+
|
|
221
|
+
Invoke-RestMethod `
|
|
222
|
+
-Method Post `
|
|
223
|
+
-Uri "$base/v1/sessions/$sid/javascript/evaluate" `
|
|
224
|
+
-Headers $headers `
|
|
225
|
+
-ContentType "application/json" `
|
|
226
|
+
-Body $body
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
执行语句:
|
|
230
|
+
|
|
231
|
+
```powershell
|
|
232
|
+
$body = @{
|
|
233
|
+
code = "document.body.dataset.agent = 'ready';"
|
|
234
|
+
timeout = 30
|
|
235
|
+
} | ConvertTo-Json
|
|
236
|
+
|
|
237
|
+
Invoke-RestMethod `
|
|
238
|
+
-Method Post `
|
|
239
|
+
-Uri "$base/v1/sessions/$sid/javascript/execute" `
|
|
240
|
+
-Headers $headers `
|
|
241
|
+
-ContentType "application/json" `
|
|
242
|
+
-Body $body
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### 5. 监听 DOM 事件
|
|
246
|
+
|
|
247
|
+
```powershell
|
|
248
|
+
$listener = @{
|
|
249
|
+
id = "submit-watch"
|
|
250
|
+
selector = "form"
|
|
251
|
+
event = "submit"
|
|
252
|
+
capture = $true
|
|
253
|
+
} | ConvertTo-Json
|
|
254
|
+
|
|
255
|
+
Invoke-RestMethod `
|
|
256
|
+
-Method Post `
|
|
257
|
+
-Uri "$base/v1/sessions/$sid/dom/listeners" `
|
|
258
|
+
-Headers $headers `
|
|
259
|
+
-ContentType "application/json" `
|
|
260
|
+
-Body $listener
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### 6. 监听 DOM 变更和页面网络
|
|
264
|
+
|
|
265
|
+
```powershell
|
|
266
|
+
$instrumentation = @{
|
|
267
|
+
network = $true
|
|
268
|
+
mutations = $true
|
|
269
|
+
mutation_selector = "body"
|
|
270
|
+
capture_request_bodies = $false
|
|
271
|
+
capture_response_bodies = $false
|
|
272
|
+
max_body_chars = 20000
|
|
273
|
+
} | ConvertTo-Json
|
|
274
|
+
|
|
275
|
+
Invoke-RestMethod `
|
|
276
|
+
-Method Put `
|
|
277
|
+
-Uri "$base/v1/sessions/$sid/instrumentation" `
|
|
278
|
+
-Headers $headers `
|
|
279
|
+
-ContentType "application/json" `
|
|
280
|
+
-Body $instrumentation
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
长轮询事件:
|
|
284
|
+
|
|
285
|
+
```powershell
|
|
286
|
+
$events = Invoke-RestMethod `
|
|
287
|
+
-Method Get `
|
|
288
|
+
-Uri "$base/v1/sessions/$sid/events?after=0&timeout=20" `
|
|
289
|
+
-Headers $headers
|
|
290
|
+
|
|
291
|
+
$next = $events.latest_sequence
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
下一次使用 `after=$next`。`kinds=dom,network,mutation,lifecycle` 可以过滤类型。
|
|
295
|
+
|
|
296
|
+
网络探针覆盖页面内的 `fetch`、`XMLHttpRequest`、WebSocket 和
|
|
297
|
+
Performance Resource 记录。它不是浏览器底层代理,不能保证拿到
|
|
298
|
+
Service Worker、缓存命中、扩展流量或所有响应体。需要完整协议级调试时,创建会话时设置
|
|
299
|
+
`remote_debugging_port`,并使用 WebView2 或 Qt 的 DevTools 协议。
|
|
300
|
+
|
|
301
|
+
### 7. 保存 Cookie 快照
|
|
302
|
+
|
|
303
|
+
```powershell
|
|
304
|
+
$snapshot = Invoke-RestMethod `
|
|
305
|
+
-Method Post `
|
|
306
|
+
-Uri "$base/v1/sessions/$sid/cookie-snapshots" `
|
|
307
|
+
-Headers $headers `
|
|
308
|
+
-ContentType "application/json" `
|
|
309
|
+
-Body '{"name":"登录后状态"}'
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
快照默认保存在操作系统的当前用户数据目录。也可以通过 `--data-dir` 指定目录。
|
|
313
|
+
|
|
314
|
+
### 8. 用快照创建新无痕会话
|
|
315
|
+
|
|
316
|
+
```powershell
|
|
317
|
+
$body = @{
|
|
318
|
+
url = "https://example.com/account"
|
|
319
|
+
snapshot_id = $snapshot.snapshot_id
|
|
320
|
+
visible = $false
|
|
321
|
+
} | ConvertTo-Json
|
|
322
|
+
|
|
323
|
+
$restored = Invoke-RestMethod `
|
|
324
|
+
-Method Post `
|
|
325
|
+
-Uri "$base/v1/sessions" `
|
|
326
|
+
-Headers $headers `
|
|
327
|
+
-ContentType "application/json" `
|
|
328
|
+
-Body $body
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
请求中的 `cookies` 会覆盖快照内同名、同域和同路径的 Cookie。
|
|
332
|
+
|
|
333
|
+
Cookie 恢复是尽力行为:
|
|
334
|
+
|
|
335
|
+
- pywebview 可以读取包含 HttpOnly 在内的 Cookie。
|
|
336
|
+
- 通用跨后端恢复通过 `document.cookie` 完成,无法恢复 HttpOnly Cookie。
|
|
337
|
+
- Domain、SameSite、Secure、过期时间和浏览器策略可能拒绝部分 Cookie。
|
|
338
|
+
- 只保存 Cookie,不保存 IndexedDB、Local Storage、Service Worker 或缓存。
|
|
339
|
+
|
|
340
|
+
### 9. 显示、隐藏和销毁
|
|
341
|
+
|
|
342
|
+
```powershell
|
|
343
|
+
Invoke-RestMethod -Method Post -Uri "$base/v1/sessions/$sid/window/show" -Headers $headers
|
|
344
|
+
Invoke-RestMethod -Method Post -Uri "$base/v1/sessions/$sid/window/hide" -Headers $headers
|
|
345
|
+
Invoke-RestMethod -Method Delete -Uri "$base/v1/sessions/$sid" -Headers $headers
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
Agent 应在任务结束时删除会话,释放 worker 进程和 WebView2 资源。
|
|
349
|
+
|
|
350
|
+
## 主要接口
|
|
351
|
+
|
|
352
|
+
| 方法 | 路径 | 用途 |
|
|
353
|
+
|---|---|---|
|
|
354
|
+
| `POST` | `/v1/sessions` | 创建独立无痕会话 |
|
|
355
|
+
| `GET` | `/v1/sessions` | 列出会话和进程信息 |
|
|
356
|
+
| `GET` | `/v1/sessions/{id}` | 获取窗口、句柄和进程状态 |
|
|
357
|
+
| `DELETE` | `/v1/sessions/{id}` | 销毁会话 |
|
|
358
|
+
| `POST` | `/v1/sessions/{id}/navigate` | 导航 |
|
|
359
|
+
| `POST` | `/v1/sessions/{id}/javascript/evaluate` | 执行表达式并返回结果 |
|
|
360
|
+
| `POST` | `/v1/sessions/{id}/javascript/execute` | 执行语句 |
|
|
361
|
+
| `POST` | `/v1/sessions/{id}/dom/query` | 查询元素 |
|
|
362
|
+
| `POST` | `/v1/sessions/{id}/dom/input` | 输入 |
|
|
363
|
+
| `POST` | `/v1/sessions/{id}/dom/click` | 点击 |
|
|
364
|
+
| `POST` | `/v1/sessions/{id}/dom/listeners` | 添加事件监听 |
|
|
365
|
+
| `PUT` | `/v1/sessions/{id}/instrumentation` | 配置网络和 DOM 探针 |
|
|
366
|
+
| `GET` | `/v1/sessions/{id}/events` | 获取事件 |
|
|
367
|
+
| `GET` | `/v1/sessions/{id}/cookies` | 获取 Cookie |
|
|
368
|
+
| `POST` | `/v1/sessions/{id}/cookie-snapshots` | 保存 Cookie 快照 |
|
|
369
|
+
| `GET` | `/v1/cookie-snapshots` | 列出 Cookie 快照 |
|
|
370
|
+
|
|
371
|
+
## 设计边界
|
|
372
|
+
|
|
373
|
+
- 无痕隔离依靠“一会话一进程”和 `private_mode=True`。
|
|
374
|
+
- `window_id` 是服务生成的稳定标识。
|
|
375
|
+
- `native_window_id` 依赖 GUI 后端,窗口尚未创建时可能为 `null`。
|
|
376
|
+
- 所有 worker 接口仅监听随机本机端口,并使用独立内部令牌。
|
|
377
|
+
- 控制器重启不会恢复运行中的 worker 会话。
|
|
378
|
+
- 当前不提供截图接口,因为 pywebview 没有统一的跨后端页面截图 API。
|
|
379
|
+
- 公共兼容契约是 `agent-webview` CLI、运行信息文件和控制器 `/v1` HTTP API。
|
|
380
|
+
- `agent_webview` 包内的其他 Python 模块暂不属于稳定公共 API。
|
|
381
|
+
|
|
382
|
+
具体兼容策略见
|
|
383
|
+
[`docs/api-stability.md`](https://github.com/Yuv96/agent-webview/blob/main/docs/api-stability.md)。
|
|
384
|
+
|
|
385
|
+
## 本地开发
|
|
386
|
+
|
|
387
|
+
```bash
|
|
388
|
+
uv sync --extra dev
|
|
389
|
+
uv run ruff check src tests
|
|
390
|
+
uv run mypy
|
|
391
|
+
uv run pytest
|
|
392
|
+
python scripts/release_check.py
|
|
393
|
+
uv run python scripts/smoke_service.py
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
发布流程见
|
|
397
|
+
[`RELEASING.md`](https://github.com/Yuv96/agent-webview/blob/main/RELEASING.md)。
|