AI 界面:支持 JS + 页面链接可点进去 (v2.0.36)
用户要求:"弹出的网页可再点进去。支持js"。
## JS 开了
`javaScriptEnabled = true`。模型可以在 show_ui 的 HTML 里写 <script>:
canvas 画图、动态生成界面、算完再渲染。实测柱状图正常。
回传用户选择两条路都留着:
· `<button data-doubao-result="值">` —— App 侧字符串替换成 doubao://pick,
由 shouldOverrideUrlLoading 拦。**不依赖 JS**,页面脚本出错也能用
· `Doubao.pick("值")` / `Doubao.close()` —— 给会写 JS 的模型用
## 链接能点进去
http/https 在 WebView 里直接打开(以前 return true 吃掉了),
顶部自动出现「←」返回键;系统返回键也先回退网页、退不了才关界面。
## 安全:JS 开着但 App 接口不漏
开 JS 之后最大的风险是 `addJavascriptInterface` 那个桥 —— 它对**之后加载的
每一个页面**都可见。AI 页面里点个链接落到外部站点,那个站点的脚本就能调我们的桥。
按页面收口:
· 桥只挂在 AI 那一页(初始 load 前挂,保证内联脚本能用)
· 一导航出去立刻摘 —— **实测外部页面里 typeof Doubao === 'undefined'**
· 返回 AI 页时自动恢复(否则"点进去再返回,按钮全失灵")
· 桥只暴露 pick / close 两个方法
· allowFileAccess / allowContentAccess / 混合内容 依然全关
## 这段时序改了三版(每版失败都很隐蔽,记下来)
第一版:load 前挂,导航时摘。
→ 外部页面在 onPageStarted 摘桥**之前**有"已开始加载、桥还在"的窗口。
第二版:只在 onPageFinished 里判 url 才挂。
→ 实测 `Uncaught ReferenceError: Doubao is not defined`。
**onPageFinished 的回调比页面内联 <script> 执行还晚**,
脚本跑的时候桥还没挂上。
第三版(现在):load 前挂 + `payloadLoaded` 标志挡窗口期 +
返回时用**页面里的标记变量**判"是不是我们的页"再重挂。
(用 url 判会漏:从外部页 goBack 回来时 url 不是 about:blank,
表现是"返回后按钮全失灵")
## 验证
新增 `scripts/verify-ai-webview.py`(17 项)钉住上面这些:
JS 开着、危险开关关着、初始 add 在 load 之前、onPageFinished 里有重挂、
重挂走探针而不是 url、**探针返回值按字符串处理**(evaluateJavascript 的回调
就是字符串,按 Boolean 判条件永远不成立 —— 我也踩了)、
http(s) 放行、BackHandler、桥只暴露两个方法。
★ 写这个验证脚本时它自己也错了一次:去注释的正则把字符串里的 `"http://"`
从中间截断,于是"http 有没有放行"永远判不合格。**验证脚本自己有 bug 比没有
验证脚本更坏**(它会让人去改本来对的代码),所以只去块注释、不去行注释。
真机实测(Android 14 x86_64):
初始页 → typeof Doubao = object | pick 可用: true
JS 按钮回传 → 日志 "界面里点了:优先级=中"
点链接 → example.com 在界面里打开,顶部出现返回键
导航后外部页 → typeof Doubao = undefined(桥确实摘了)
返回后再点按钮 → 回传仍然工作(桥恢复了)
This commit is contained in:
84
scripts/probe-show-ui-js.py
Normal file
84
scripts/probe-show-ui-js.py
Normal file
@@ -0,0 +1,84 @@
|
||||
"""测 JS 支持和"点进去"。
|
||||
|
||||
一段同时用这两个能力的页面:
|
||||
· <script> 画 canvas 柱状图 + 动态生成按钮 + 调 Doubao.pick
|
||||
· 一个 http 链接(点进去应该能在界面里打开,顶部出现返回键)
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
try:
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
URL = "http://127.0.0.1:15199"
|
||||
CONV = "c_main"
|
||||
|
||||
|
||||
def call(path, payload=None, method="POST", timeout=180):
|
||||
data = json.dumps(payload, ensure_ascii=False).encode("utf-8") if payload is not None else None
|
||||
req = urllib.request.Request(
|
||||
URL + path, data=data, method=method,
|
||||
headers={"Content-Type": "application/json; charset=utf-8"},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=timeout) as r:
|
||||
return json.loads(r.read().decode("utf-8"))
|
||||
|
||||
|
||||
HTML = """
|
||||
<h1>JS + 链接测试</h1>
|
||||
|
||||
<p>下面这根柱状图是 <b><script></b> 用 canvas 画的:</p>
|
||||
<canvas id="c" width="300" height="140" style="background:#1d1f25;border-radius:8px"></canvas>
|
||||
|
||||
<div id="btns" style="margin-top:14px"></div>
|
||||
|
||||
<p style="margin-top:14px">一个链接:<a href="https://example.com">点我进 example.com</a></p>
|
||||
|
||||
<script>
|
||||
// ① canvas 画图
|
||||
// ★ 注意:改 canvas.width/height **会清空画布**(and 重置变换)。
|
||||
// 我第一版在画完之后设了 canvas.height,结果柱子全没了 ——
|
||||
// 那是测试 HTML 自己的 bug,不是 App 的。尺寸要在画之前定好。
|
||||
var cv = document.getElementById('c'), g = cv.getContext('2d');
|
||||
cv.height = 150;
|
||||
var data = [40, 90, 60, 130, 75];
|
||||
g.fillStyle = '#4c8dff';
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
g.fillRect(20 + i * 55, 130 - data[i], 34, data[i]);
|
||||
}
|
||||
g.fillStyle = '#e8eaed';
|
||||
g.font = '12px sans-serif';
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
g.fillText('#' + (i + 1), 24 + i * 55, 146);
|
||||
}
|
||||
|
||||
// ② 动态生成按钮,用 Doubao.pick 回传(这条是 JS-only 的能力)
|
||||
var box = document.getElementById('btns');
|
||||
['低', '中', '高'].forEach(function (lv) {
|
||||
var b = document.createElement('button');
|
||||
b.textContent = '优先级:' + lv;
|
||||
b.style.cssText = 'margin:4px 8px 0 0;padding:10px 16px;border-radius:10px;'
|
||||
+ 'border:0;background:#2a2d34;color:#e8eaed;font-size:15px';
|
||||
b.onclick = function () { Doubao.pick('优先级=' + lv); };
|
||||
box.appendChild(b);
|
||||
});
|
||||
|
||||
// ③ 证明 JS 真的跑了 —— 把当前时间写进去
|
||||
var p = document.createElement('p');
|
||||
p.style.cssText = 'color:#8c939d;font-size:12px;margin-top:16px';
|
||||
p.textContent = 'JS 执行于 ' + new Date().toLocaleTimeString();
|
||||
document.body.appendChild(p);
|
||||
</script>
|
||||
"""
|
||||
|
||||
print("=== 弹一个用 JS + 带链接的界面 ===")
|
||||
res = call("/api/tools/run", {
|
||||
"name": "show_ui",
|
||||
"args": {"title": "JS 测试", "html": HTML},
|
||||
"conversationId": CONV,
|
||||
})
|
||||
print(json.dumps(res, ensure_ascii=False)[:300])
|
||||
Reference in New Issue
Block a user