1# 使用DevTools工具调试前端页面 2 3 4Web组件支持使用DevTools工具调试前端页面。DevTools是一个 Web前端开发调试工具,提供了电脑上调试移动设备前端页面的能力。开发者通过[setWebDebuggingAccess()](../reference/apis-arkweb/js-apis-webview.md#setwebdebuggingaccess)接口开启Web组件前端页面调试能力,利用DevTools工具可以在电脑上调试移动设备上的前端网页,设备需为4.1.0及以上版本。 5 6 7## 调试步骤 8 9### 应用代码开启Web调试开关 10 11调试网页前,需要应用侧代码调用[setWebDebuggingAccess()](../reference/apis-arkweb/js-apis-webview.md#setwebdebuggingaccess)接口开启Web调试开关。 12如果没有开启Web调试开关,则DevTools无法发现被调试的网页。 13 141. 在应用代码中开启Web调试开关,具体如下: 15 16 ```ts 17 // xxx.ets 18 import { webview } from '@kit.ArkWeb'; 19 20 @Entry 21 @Component 22 struct WebComponent { 23 controller: webview.WebviewController = new webview.WebviewController(); 24 25 aboutToAppear() { 26 // 配置Web开启调试模式 27 webview.WebviewController.setWebDebuggingAccess(true); 28 } 29 30 build() { 31 Column() { 32 Web({ src: 'www.example.com', controller: this.controller }) 33 } 34 } 35 } 36 ``` 372. 开启调试功能需要在DevEco Studio应用工程hap模块的module.json5文件中增加如下权限,添加方法请参考[在配置文件中声明权限](../security/AccessToken/declare-permissions.md)。 38 39 ``` 40 "requestPermissions":[ 41 { 42 "name" : "ohos.permission.INTERNET" 43 } 44 ] 45 ``` 46 47### 将设备连接至电脑 48 49请将设备连接至电脑,随后开启开发者模式,为后续的端口转发操作做好准备。 50 511. 请开启设备上的开发者模式,并启用USB调试功能。 52 53 (1) 终端系统查看“设置 > 系统”中是否有“开发者选项”,如果不存在,可在“设置 > 关于本机”连续七次单击“版本号”,直到提示“开启开发者模式”,点击“确认开启”后输入PIN码(如果已设置),设备将自动重启。 54 55 (2) USB数据线连接终端和电脑,在“设置 > 系统 > 开发者选项”中,打开“USB调试”开关,弹出的“允许USB调试”的弹框,点击“允许”。 56 572. 使用hdc命令连接上设备。 58 打开命令行执行如下命令,查看hdc能否发现设备。 59 ```shell 60 hdc list targets 61 ``` 62 - 如果命令有返回设备的ID,则说明hdc已连接上设备。 63  64 - 如果命令返回 `[Empty]`,则说明hdc还没有发现设备。 65  66 673. 进入hdc shell。 68 当hdc命令连接上设备后,执行如下命令,进入hdc shell。 69 ```shell 70 hdc shell 71 ``` 72 73### 端口转发 74当应用代码调用setWebDebuggingAccess接口开启Web调试开关后,ArkWeb内核将启动一个domain socket的监听,以此实现DevTools对网页的调试功能。 75但是Chrome浏览器无法直接访问到设备上的domain socket, 所以需要将设备上的domain socket转发到电脑上。 76 771. 先在hdc shell里执行如下命令,查询ArkWeb在设备里创建的domain socket。 78 ```shell 79 cat /proc/net/unix | grep devtools 80 ``` 81 * 如果前几步操作无误,该命令的执行结果将显示用于查询的domain socket端口。 82  83 84 * 如果没有查询到结果, 请再次确认。 85 (1) 应用开启了Web调试开关。 86 (2) 应用使用Web组件加载了网页。 87 882. 将查询到的domain socket转发至电脑的TCP 9222端口。 89 执行exit退出hdc shell。 90 ```shell 91 exit 92 ``` 93 在命令行里执行如下命令转发端口。 94 ```shell 95 hdc fport tcp:9222 localabstract:webview_devtools_remote_38532 96 ``` 97 > **说明:** 98 > 99 > "webview_devtools_remote_" 后面的数字,代表ArkWeb所在应用的进程号, 该数字不是固定的。请将数字改为自己查询到的值。 100 > 如果应用的进程号发生变化(例如,应用重新启动),则需要重新进行端口转发。 101 102 命令执行成功示意图: 103  104 1053. 在命令行里执行如下命令,检查端口是否转发成功。 106 ```shell 107 hdc fport ls 108 ``` 109 * 如果有返回端口转发的任务,则说明端口转发成功。 110  111 * 如果返回 `[Empty]`, 则说明端口转发失败。 112  113 114### 在Chrome浏览器上打开调试工具页面 115 1. 在电脑端Chrome浏览器地址栏中输入调试工具地址 chrome://inspect/\#devices 并打开该页面。 116 2. 修改Chrome调试工具的配置。 117 需要从本地的TCP 9222端口发现被调试网页,所以请确保已勾选 "Discover network targets"。然后再进行网络配置。 118 (1) 点击 "Configure" 按钮。 119 (2) 在 "Target discovery settings" 中添加要监听的本地端口localhost:9222。 120 121  122 123 3. 为了同时调试多个应用,请在Chrome浏览器的调试工具网页内,于“Devices”选项中的“configure”部分添加多个端口号。 124 125  126 127### 等待发现被调试页面 128 129 如果前面的步骤执行成功,稍后,Chrome的调试页面将显示待调试的网页。 130  131 132### 开始网页调试 133 134  135 136## 便捷脚本 137### Windows平台 138请复制以下信息建立bat文件,开启调试应用后执行。 139 ``` 140 @echo off 141 setlocal enabledelayedexpansion 142 143 :: Initialize port number and PID list 144 set PORT=9222 145 set PID_LIST= 146 147 :: Get the list of all forwarded ports and PIDs 148 for /f "tokens=2,5 delims=:_" %%a in ('hdc fport ls') do ( 149 if %%a gtr !PORT! ( 150 set PORT=%%a 151 ) 152 for /f "tokens=1 delims= " %%c in ("%%b") do ( 153 set PID_LIST=!PID_LIST! %%c 154 ) 155 ) 156 157 :: Increment port number for next application 158 set temp_PORT=!PORT! 159 set /a temp_PORT+=1 160 set PORT=!temp_PORT! 161 162 :: Get the domain socket name of devtools 163 for /f "tokens=*" %%a in ('hdc shell "cat /proc/net/unix | grep devtools"') do ( 164 set SOCKET_NAME=%%a 165 166 :: Extract process ID 167 for /f "delims=_ tokens=4" %%b in ("!SOCKET_NAME!") do set PID=%%b 168 169 :: Check if PID already has a mapping 170 echo !PID_LIST! | findstr /C:" !PID! " >nul 171 if errorlevel 1 ( 172 :: Add mapping 173 hdc fport tcp:!PORT! localabstract:webview_devtools_remote_!PID! 174 if errorlevel 1 ( 175 echo Error: Failed to add mapping. 176 pause 177 exit /b 178 ) 179 180 :: Add PID to list and increment port number for next application 181 set PID_LIST=!PID_LIST! !PID! 182 set temp_PORT=!PORT! 183 set /a temp_PORT+=1 184 set PORT=!temp_PORT! 185 ) 186 ) 187 188 :: If no process ID was found, prompt the user to open debugging in their application code and provide the documentation link 189 if "!SOCKET_NAME!"=="" ( 190 echo No process ID was found. Please open debugging in your application code using the corresponding interface. You can find the relevant documentation at this link: [https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/web/web-debugging-with-devtools.md] 191 pause 192 exit /b 193 ) 194 195 :: Check mapping 196 hdc fport ls 197 198 echo. 199 echo Script executed successfully. Press any key to exit... 200 pause >nul 201 202 :: Try to open the page in Edge 203 start msedge chrome://inspect/#devices.com 204 205 :: If Edge is not available, then open the page in Chrome 206 if errorlevel 1 ( 207 start chrome chrome://inspect/#devices.com 208 ) 209 210 endlocal 211 ``` 212### Linux或Mac平台 213请复制以下信息建立sh文件,注意chmod以及格式转换,开启调试应用后执行。 214本脚本会先删除所有的端口转发,如果有其他的工具(如:DevEco Studio)也在使用端口转发功能,会受到影响。 215 ``` 216 #!/bin/bash 217 218 # Get current fport rule list 219 CURRENT_FPORT_LIST=$(hdc fport ls) 220 221 # Delete the existing fport rule one by one 222 while IFS= read -r line; do 223 # Extract the taskline 224 IFS=' ' read -ra parts <<< "$line" 225 taskline="${parts[1]} ${parts[2]}" 226 227 # Delete the corresponding fport rule 228 echo "Removing forward rule for $taskline" 229 hdc fport rm $taskline 230 result=$? 231 232 if [ $result -eq 0 ]; then 233 echo "Remove forward rule success, taskline:$taskline" 234 else 235 echo "Failed to remove forward rule, taskline:$taskline" 236 fi 237 238 done <<< "$CURRENT_FPORT_LIST" 239 240 # Initial port number 241 INITIAL_PORT=9222 242 243 # Get the current port number, use initial port number if not set previously 244 CURRENT_PORT=${PORT:-$INITIAL_PORT} 245 246 # Get the list of all PIDs that match the condition 247 PID_LIST=$(hdc shell cat /proc/net/unix | grep webview_devtools_remote_ | awk -F '_' '{print $NF}') 248 249 if [ -z "$PID_LIST" ]; then 250 echo "Failed to retrieve PID from the device" 251 exit 1 252 fi 253 254 # Increment the port number 255 PORT=$CURRENT_PORT 256 257 # Forward ports for each application one by one 258 for PID in $PID_LIST; do 259 # Increment the port number 260 PORT=$((PORT + 1)) 261 262 # Execute the hdc fport command 263 hdc fport tcp:$PORT localabstract:webview_devtools_remote_$PID 264 265 # Check if the command executed successfully 266 if [ $? -ne 0 ]; then 267 echo "Failed to execute hdc fport command" 268 exit 1 269 fi 270 done 271 272 # List all forwarded ports 273 hdc fport ls 274 ``` 275 276## 常见问题与解决方法 277 278### hdc无法发现设备 279**问题现象** 280 281 在命令行里执行如下命令后,没有列出设备ID。 282 ```shell 283 hdc list targets 284 ``` 285 286**解决方法** 287 288 * 请确保设备上的USB调试开关已开启。 289 * 请确保设备与电脑相连。 290 291### hdc的命令显示设备"未授权"或"unauthorized" 292**问题现象** 293 294 执行hdc命令时,提示设备"未授权"或"unauthorized"。 295 296**问题原因** 297 298 设备没有授权该台电脑进行调试。 299 300**解决方法** 301 302 开启USB调试开关的设备连接没有授权的电脑后,会弹框提示"是否允许USB调试?",请选择允许。 303 304### 找不到DevTools的domain socket 305**问题现象** 306 307 在hdc shell里执行如下命令后,没有结果。 308 ```shell 309 cat /proc/net/unix | grep devtools 310 ``` 311 312**解决方法** 313 314 * 请确保应用[开启了Web调试开关](#应用代码开启web调试开关)。 315 * 请确保应用使用Web组件加载了网页。 316 317### 端口转发不成功 318**问题现象** 319 320 在命令行里执行如下命令后,没有列出之前设置过转发任务。 321 ```shell 322 hdc fport ls 323 ``` 324 325**解决方法** 326 327 * 请确保设备里的domain socket存在。 328 * 请确保电脑端的tcp:9222没有被占用。 329 如果tcp:9222被占用,可以将domain socket转发到其他未被占用的TCP端口, 比如9223等。 330 如果转发到了新的TCP端口, 需要同步修改电脑端Chrome浏览器"Target discovery settings"中的端口号。 331 332### 端口转发成功后,电脑端Chrome无法发现被调试网页 333**问题现象** 334 335 电脑端Chrome浏览器无法发现被调试网页。 336 337**问题原因** 338 339端口转发失效可能是以下原因: 340 * 设备与电脑断连,会导致hdc里的所有转发任务被清空。 341 * hdc服务重启,也会导致hdc里的所有转发任务被清空。 342 * 设备里应用的进程号发生了变更(应用重新启动等),会导致hdc里旧的转发任务失效。 343 * 多个转发任务转发到了同一个端口等异常配置,会导致转发异常。 344 345**解决方法** 346 347 * 请确保电脑端的本地tcp:9222(其他TCP端口同理)没有被占用。 348 * 请确保设备端的domain socket还存在。 349 * 请确保domain socket名称里的进程号与被调试的应用的进程号相同。 350 * 请删除hdc里其他不必要的转发任务。 351 * 转发成功后,请用电脑端的Chrome浏览器打开网址 http://localhost:9222/json ,URL里的9222需要改为自己实际配置的TCP端口。 352 353 - 如果网页有内容, 说明端口转发成功,请在Chrome的调试页面[等待被调试页面的出现](#等待发现被调试页面)。 354  355 356 - 如果展示的是错误网页, 说明端口转发失败, 解决方法见上面的[端口转发不成功](#端口转发不成功)。 357  358 359 * 电脑端Chrome浏览器打开 http://localhost:9222/json 页面有内容,但是Chrome的调试工具界面还是无法发现调试目标。 360 - 请确保Chrome调试工具界面的 "Configure" 中配置的端口号,与端口转发指定的TCP端口号一致。 361 - 在本文档中,默认使用的TCP端口号为9222。 362 如果开发者使用了其他的TCP端口号(比如9223),请同时修改[端口转发](#端口转发)中的TCP端口号和[Chrome调试工具界面"Configure"配置](#在chrome浏览器上打开调试工具页面)中的端口号。 363