Files
youle_app_ohos/scripts/arkweb-devtools.sh
T
lanterngamescnandClaude Opus 4.8 f84988869c 新增真机 ArkWeb DevTools 一键脚本 + 文档补多设备/fport 坑
- scripts/arkweb-devtools.sh:自动找 hdc、选真机(排除模拟器)、取 pid(没运行则拉起)、
  清掉端口上所有旧转发并重建 tcp:9222、curl 自验。幂等可反复跑;应用重启后重跑即可重连。
  (修正:hdc `fport rm` 需 local/remote 两参数分开传,否则删不掉;会累积多条旧转发需逐条清。)
- 文档加「一键脚本」快速开始、「多设备必须带 -t」提示、fport rm 两参数/累积排查。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-27 01:03:35 +08:00

65 lines
3.2 KiB
Bash
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.
#!/usr/bin/env bash
# 一键建立真机 ArkWeb DevTools 端口转发,供 chrome://inspect 调试 H5。
# 自动:找 hdc → 选真机(排除模拟器) → 取应用 pid(没运行则拉起) → 重建 tcp:9222 转发 → 验证。
#
# 用法:
# bash scripts/arkweb-devtools.sh # 默认 com.lobby.daoqi, 端口 9222
# bash scripts/arkweb-devtools.sh <bundle> <port> [serial]
# 之后在 Chrome 打开 chrome://inspect → Discover network targets → 加 localhost:9222 → inspect。
# 应用一旦重启 pid 会变、转发失效,重跑本脚本即可(端口不变,chrome 配置不用改)。
set -u
BUNDLE="${1:-com.lobby.daoqi}"
PORT="${2:-9222}"
SERIAL="${3:-}"
export MSYS_NO_PATHCONV=1 # git-bash 下避免 localabstract 路径被转换
find_hdc() {
command -v hdc >/dev/null 2>&1 && { echo hdc; return 0; }
local p
for p in "$HOME"/AppData/Local/OpenHarmony/Sdk/*/toolchains/hdc \
"$HOME"/AppData/Local/Huawei/Sdk/*/*/toolchains/hdc; do
[ -x "$p" ] && { echo "$p"; return 0; }
done
p="/c/Program Files/Huawei/DevEco Studio/sdk/default/openharmony/toolchains/hdc.exe"
[ -x "$p" ] && { echo "$p"; return 0; }
return 1
}
HDC="$(find_hdc)" || { echo "✗ 找不到 hdc,请把 <SDK>/toolchains 加入 PATH"; exit 1; }
# 选真机:排除 127.0.0.1 模拟器;未指定 serial 时取第一台真机
if [ -z "$SERIAL" ]; then
SERIAL="$("$HDC" list targets 2>/dev/null | tr -d '\r' | grep -v '127.0.0.1' | grep -viE 'empty|^\s*$' | head -1)"
fi
[ -z "$SERIAL" ] && { echo "✗ 没有连接的真机(hdc list targets 为空或只有模拟器)"; exit 1; }
echo "真机: $SERIAL"
# 取 pid,没运行就拉起(注意:必须是 debug 包,才开了 setWebDebuggingAccess
PID="$("$HDC" -t "$SERIAL" shell pidof "$BUNDLE" 2>/dev/null | tr -d '\r')"
if [ -z "$PID" ]; then
echo "应用未运行,拉起 $BUNDLE …"
"$HDC" -t "$SERIAL" shell aa start -a EntryAbility -b "$BUNDLE" >/dev/null 2>&1
sleep 3
PID="$("$HDC" -t "$SERIAL" shell pidof "$BUNDLE" 2>/dev/null | tr -d '\r')"
fi
[ -z "$PID" ] && { echo "✗ 取不到应用 pid(确认是 debug 包且在前台运行)"; exit 1; }
echo "pid: $PID"
# 先删干净端口上【所有】旧转发(会积累多条指向已死 pid 的)。
# 注意 hdc 的 `fport rm` 要 local/remote 两个参数分开传(不能是单个带空格的字符串),故 $rule 不加引号让其分词。
"$HDC" -t "$SERIAL" fport ls 2>/dev/null | tr -d '\r' | awk -v p="tcp:$PORT" '$2==p{print $2" "$3}' | while IFS= read -r rule; do
# shellcheck disable=SC2086
[ -n "$rule" ] && "$HDC" -t "$SERIAL" fport rm $rule >/dev/null 2>&1
done
# 建新转发(指向当前 pid
"$HDC" -t "$SERIAL" fport "tcp:$PORT" "localabstract:webview_devtools_remote_$PID" 2>&1 | tr -d '\r'
# 验证
if curl -s -m 6 "http://localhost:$PORT/json" 2>/dev/null | grep -q '"type"'; then
echo "✓ 就绪:http://localhost:$PORT 已能列出页面"
echo "→ Chrome 打开 chrome://inspectDiscover network targets 加 localhost:$PORT,点页面的 inspect"
else
echo "⚠ 转发已建,但 /json 暂无页面(应用可能还没加载完)。稍等几秒再: curl http://localhost:$PORT/json"
fi