1# Debugging Frontend Pages by Using DevTools 2 3 4The **Web** component supports debugging of web frontend pages by using DevTools, a web frontend development and debugging tool that allows you to debug an application's frontend pages on a PC. Before you do this, use [setWebDebuggingAccess()](../reference/apis-arkweb/js-apis-webview.md#setwebdebuggingaccess) to enable frontend page debugging for the **Web** component and make sure the test device connected to the PC runs 4.1.0 or a later version. 5 6 7To use DevTools for frontend page debugging, perform the following steps: 8 9 101. Enable web frontend page debugging in the application code. 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 // Enable web frontend page debugging. 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. Declare the required permission in the **module.json5** file of the HAP module in the application project in DevEco Studio. For details, see [Declaring Permissions in the Configuration File](../security/AccessToken/declare-permissions.md). 34 35 ``` 36 "requestPermissions":[ 37 { 38 "name" : "ohos.permission.INTERNET" 39 } 40 ] 41 ``` 42 433. Connect your device to a PC, and configure port mapping on the PC as follows: 44 45 ``` 46 // Search for the domain socket name required for DevTools. The name is related to the process ID. After the application being debugged is restarted, repeat this step to complete port forwarding. 47 cat /proc/net/unix | grep devtools 48 // Configure port mapping. Replace [pid] with the actual process ID. 49 hdc fport tcp:9222 localabstract:webview_devtools_remote_[pid] 50 // View port mapping. 51 hdc fport ls 52 Example: 53 hdc shell 54 cat /proc/net/unix | grep devtools 55 // Display webview_devtools_remote_3458. 56 exit 57 hdc fport tcp:9222 localabstract:webview_devtools_remote_3458 58 hdc fport ls 59 ``` 60 614. Enter **chrome://inspect/\#devices** in the address box of the Chrome browser on the PC. Once the device is identified, you can get started with page debugging. The debugging effect is as follows: 62 63 **Figure 1** Successful device identification 64 65  66 67 **Figure 2** Page debugging effect 68 69  70 71 725. To debug multiple applications, click **Configure** under **Devices** and add multiple port numbers. 73 74 **Figure 3** Adding port numbers 75 76  77 786. You can use a script to accelerate debugging on Windows as follows: Create a .bat file that contains the following information, start the application to debug, and run the script: 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. You can use a script to accelerate debugging on macOS or Linux: Create an .sh file that contains the following information (pay attention to chmod and format conversion), start the application to debug, and run the script: 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