• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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使用DevTools工具,可以执行以下步骤:
8
9
101. 在应用代码中开启Web调试开关,具体如下:
11
12   ```ts
13   // xxx.ets
14   import { webview } from '@kit.ArkWeb';
15
16   @Entry
17   @Component
18   struct WebComponent {
19     controller: webview.WebviewController = new webview.WebviewController();
20
21     aboutToAppear() {
22       // 配置Web开启调试模式
23       webview.WebviewController.setWebDebuggingAccess(true);
24     }
25
26     build() {
27       Column() {
28         Web({ src: 'www.example.com', controller: this.controller })
29       }
30     }
31   }
32   ```
332. 开启调试功能需要在DevEco Studio应用工程hap模块的module.json5文件中增加如下权限,添加方法请参考[在配置文件中声明权限](../security/AccessToken/declare-permissions.md)。
34
35   ```
36   "requestPermissions":[
37      {
38        "name" : "ohos.permission.INTERNET"
39      }
40    ]
41   ```
42
433. 将设备连接上电脑,在电脑端配置端口映射,配置方法如下:
44
45   ```
46   //查找 devtools 远程调试所需的 domain socket 名称,该名称与进程号有关,重启调试应用后,需要重复此步骤,以完成端口转发
47   cat /proc/net/unix | grep devtools
48   // 添加映射 [pid] 替换成实际的进程id
49   hdc fport tcp:9222 localabstract:webview_devtools_remote_[pid]
50   // 查看映射
51   hdc fport ls
52   示例:
53   hdc shell
54   cat /proc/net/unix | grep devtools
55   //显示 webview_devtools_remote_3458
56   exit
57   hdc fport tcp:9222 localabstract:webview_devtools_remote_3458
58   hdc fport ls
59   ```
60
614. 在电脑端Chrome浏览器地址栏中输入chrome://inspect/\#devices,页面识别到设备后,就可以开始页面调试。调试效果如下:
62
63     **图1** 识别设备成功效果图
64
65     ![debug-succeed](figures/debug-succeed.png)
66
67     **图2** 页面调试效果图
68
69     ![debug-effect](figures/debug-effect.png)
70
71
725. 多应用调试请在调试地址内Devices中的configure添加多个端口号以同时调试多个应用:
73
74     **图3** 添加端口号效果图
75
76     ![debug-effect](figures/debug-domains.png)
77
786. windows便捷脚本,请复制以下信息建立bat文件,开启调试应用后执行:
79
80   ```
81   @echo off
82   setlocal enabledelayedexpansion
83
84   :: Initialize port number and PID list
85   set PORT=9222
86   set PID_LIST=
87
88   :: Get the list of all forwarded ports and PIDs
89   for /f "tokens=2,5 delims=:_" %%a in ('hdc fport ls') do (
90       if %%a gtr !PORT! (
91           set PORT=%%a
92       )
93       for /f "tokens=1 delims= " %%c in ("%%b") do (
94           set PID_LIST=!PID_LIST! %%c
95       )
96   )
97
98   :: Increment port number for next application
99   set temp_PORT=!PORT!
100   set /a temp_PORT+=1
101   set PORT=!temp_PORT!
102
103   :: Get the domain socket name of devtools
104   for /f "tokens=*" %%a in ('hdc shell "cat /proc/net/unix | grep devtools"') do (
105       set SOCKET_NAME=%%a
106
107       :: Extract process ID
108       for /f "delims=_ tokens=4" %%b in ("!SOCKET_NAME!") do set PID=%%b
109
110       :: Check if PID already has a mapping
111       echo !PID_LIST! | findstr /C:" !PID! " >nul
112       if errorlevel 1 (
113           :: Add mapping
114           hdc fport tcp:!PORT! localabstract:webview_devtools_remote_!PID!
115           if errorlevel 1 (
116               echo Error: Failed to add mapping.
117               pause
118               exit /b
119           )
120
121           :: Add PID to list and increment port number for next application
122           set PID_LIST=!PID_LIST! !PID!
123           set temp_PORT=!PORT!
124           set /a temp_PORT+=1
125           set PORT=!temp_PORT!
126       )
127   )
128
129   :: If no process ID was found, prompt the user to open debugging in their application code and provide the documentation link
130   if "!SOCKET_NAME!"=="" (
131       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]
132       pause
133       exit /b
134   )
135
136   :: Check mapping
137   hdc fport ls
138
139   echo.
140   echo Script executed successfully. Press any key to exit...
141   pause >nul
142
143   :: Try to open the page in Edge
144   start msedge chrome://inspect/#devices.com
145
146   :: If Edge is not available, then open the page in Chrome
147   if errorlevel 1 (
148       start chrome chrome://inspect/#devices.com
149   )
150
151   endlocal
152   ```
153
1547. mac/linux便捷脚本,请复制以下信息建立sh文件,请自行注意chmod以及格式转换,开启调试应用后执行:
155   ```
156   #!/bin/bash
157
158   # Get current fport rule list
159   CURRENT_FPORT_LIST=$(hdc fport ls)
160
161   # Delete the existing fport rule one by one
162   while IFS= read -r line; do
163       # Extract the taskline
164       IFS=' ' read -ra parts <<< "$line"
165       taskline="${parts[1]} ${parts[2]}"
166
167       # Delete the corresponding fport rule
168       echo "Removing forward rule for $taskline"
169       hdc fport rm $taskline
170       result=$?
171
172       if [ $result -eq 0 ]; then
173           echo "Remove forward rule success, taskline:$taskline"
174       else
175           echo "Failed to remove forward rule, taskline:$taskline"
176       fi
177
178   done <<< "$CURRENT_FPORT_LIST"
179
180   # Initial port number
181   INITIAL_PORT=9222
182
183   # Get the current port number, use initial port number if not set previously
184   CURRENT_PORT=${PORT:-$INITIAL_PORT}
185
186   # Get the list of all PIDs that match the condition
187   PID_LIST=$(hdc shell cat /proc/net/unix | grep webview_devtools_remote_ | awk -F '_' '{print $NF}')
188
189   if [ -z "$PID_LIST" ]; then
190       echo "Failed to retrieve PID from the device"
191       exit 1
192   fi
193
194   # Increment the port number
195   PORT=$CURRENT_PORT
196
197   # Forward ports for each application one by one
198   for PID in $PID_LIST; do
199       # Increment the port number
200       PORT=$((PORT + 1))
201
202       # Execute the hdc fport command
203       hdc fport tcp:$PORT localabstract:webview_devtools_remote_$PID
204
205       # Check if the command executed successfully
206       if [ $? -ne 0 ]; then
207           echo "Failed to execute hdc fport command"
208           exit 1
209       fi
210   done
211
212   # List all forwarded ports
213   hdc fport ls
214   ```
215