如何解决webview调试用每次都需要寻找进程号

在研发过程中,经常需要调试应用中的Web页面,目前HarmonyOS提供的Web调试工具Devtools,每次启动都需要重新映射端口,具体步骤如下:hdc shell 进入shell模式,输入cat /proc/net/unix | grep devtools,记录webview信息,然后再操作端口映射hdc fport tcp:9222 localabstract:webview_devtools_remote_xxx。

HarmonyOS
2024-05-20 20:22:12
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
mzshj

可以参考文件代码,改成bat后缀,每次开启调试之后,运行bat文件即可自行获取进程号。

单应用:

@echo off 
setlocal 
 
   Set devtools parameter 
hdc shell param set web.debug.devtools true 
if errorlevel 1 ( 
  echo Error: Failed to set devtools parameter. 
pause 
exit /b 
) 
 
:: Get the domain socket name of devtools 
for /f "tokens=*" %%a in ('hdc shell "cat /proc/net/unix | grep devtools"') do set SOCKET_NAME=%%a 
if not defined SOCKET_NAME ( 
  echo Error: Failed to get the domain socket name of devtools. 
pause 
exit /b 
) 
 
:: Extract process ID 
for /f "delims=_ tokens=4" %%a in ("%SOCKET_NAME%") do set PID=%%a 
if not defined PID ( 
  echo Error: Failed to extract process ID. 
pause 
exit /b 
) 
 
:: Add mapping 
hdc fport tcp:9222 localabstract:webview_devtools_remote_%PID% 
  if errorlevel 1 ( 
  echo Error: Failed to add mapping. 
pause 
exit /b 
) 
 
:: Check mapping 
hdc fport ls 
 
echo. 
echo Script executed successfully. Press any key to exit... 
pause >nul 
 
:: 尝试在 Edge 中打开页面 
start msedge chrome://inspect/#devices.com 
 
:: 如果 Edge 不可用,那么在 Chrome 中打开页面 
if errorlevel 1 ( 
  start chrome chrome://inspect/#devices.com 
) 
 
endlocal
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.

多应用:

@echo off 
setlocal enabledelayedexpansion 
 
  :: Initialize port number and PID list 
set PORT=9222 
set PID_LIST= 
 
:: Set devtools parameter 
hdc shell param set web.debug.devtools true 
if errorlevel 1 ( 
  echo Error: Failed to set devtools parameter. 
pause 
exit /b 
) 
 
:: Get the list of all forwarded ports and PIDs 
for /f "tokens=2,5 delims=:_" %%a in ('hdc fport ls') do ( 
if %%a gtr !PORT! ( 
  set PORT=%%a 
) 
for /f "tokens=1 delims= " %%c in ("%%b") do ( 
set PID_LIST=!PID_LIST! %%c 
) 
) 
 
:: Increment port number for next application 
set temp_PORT=!PORT! 
set /a temp_PORT+=1   
set PORT=!temp_PORT! 
 
:: Get the domain socket name of devtools 
for /f "tokens=*" %%a in ('hdc shell "cat /proc/net/unix | grep devtools"') do ( 
  set SOCKET_NAME=%%a 
 
:: Extract process ID 
for /f "delims=_ tokens=4" %%b in ("!SOCKET_NAME!") do set PID=%%b 
 
:: Check if PID already has a mapping 
echo !PID_LIST! | findstr /C:" !PID! " >nul 
if errorlevel 1 ( 
:: Add mapping 
hdc fport tcp:!PORT! localabstract:webview_devtools_remote_!PID! 
if errorlevel 1 ( 
  echo Error: Failed to add mapping. 
pause 
exit /b 
) 
 
:: Add PID to list and increment port number for next application 
set PID_LIST=!PID_LIST! !PID! 
set temp_PORT=!PORT! 
set /a temp_PORT+=1   
set PORT=!temp_PORT! 
) 
) 
 
:: Check mapping 
hdc fport ls 
 
echo. 
echo Script executed successfully. Press any key to exit... 
pause >nul 
 
:: 尝试在 Edge 中打开页面 
start msedge chrome://inspect/#devices.com 
 
:: 如果 Edge 不可用,那么在 Chrome 中打开页面 
if errorlevel 1 ( 
  start chrome chrome://inspect/#devices.com 
) 
 
endlocal
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
分享
微博
QQ
微信
回复
2024-05-21 15:42:50
相关问题
HarmonyOS 每次编译需要10分钟
1094浏览 • 1回复 待解决
如何解决webview loaddata白屏问题
2064浏览 • 1回复 待解决
如何解决webview离线加载白屏问题
2297浏览 • 1回复 待解决
如何解决底层库无法调试的问题?
1822浏览 • 1回复 待解决
HarmonyOS webview如何调试
1922浏览 • 1回复 待解决
HarmonyOS flutter webview如何调试
836浏览 • 1回复 待解决
HarmonyOS 2300061报错如何解决
1075浏览 • 1回复 待解决
this传递问题,该如何解决
3112浏览 • 1回复 待解决
HarmonyOS toast问题如何解决
1980浏览 • 1回复 待解决
HarmonyOS webview远程调试
1427浏览 • 1回复 待解决
native层多进程场景的调试
983浏览 • 1回复 待解决
http请求报错2300006如何解决
3976浏览 • 1回复 待解决
Gauge组件问题,该如何解决
1233浏览 • 1回复 待解决
获取UIContext报错1300002如何解决
3391浏览 • 1回复 待解决
使用Devtools调试webview
1006浏览 • 1回复 待解决