目录

What should I do if the process ID needs to be found during each debugging of the WebView?

Symptom

During application development and web page debugging, each time Devtools is started, the port needs to be mapped again.

Solution

After the file content is written, change the file name extension to .bat. Each time the debugging is enabled, run the .bat file to obtain the process ID.

// xxx.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
// tokens=4 indicates extracting the field separated by the fourth underscore as the PID
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

:: Try opening the page in Edge
start msedge chrome://inspect/#devices.com

:: If Edge is unavailable, open the page in Chrome
if errorlevel 1 (
    start chrome chrome://inspect/#devices.com
)

endlocal