@echo off
:: Ensure admin privileges
NET SESSION >nul 2>&1
if %errorLevel% neq 0 (
    echo [ERROR] Please run this script as ADMINISTRATOR.
    pause
    exit /b
)

:: Set your Server IP here
set "targetSite=2.32.213.1"
set "xmlPath=C:\msedge_ie_list.xml"

echo --- Starting Fix for PageOffice Compatibility ---

:: 1. Setup IE Mode XML
echo [1/3] Configuring Edge IE Mode...
(
echo ^<site-list version="1"^>
echo   ^<site url="%targetSite%"^>
echo     ^<open-in^>IE11^</open-in^>
echo     ^<mode^>IE11^</mode^>
echo   ^</site^>
echo ^</site-list^>
) > "%xmlPath%"

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge" /v "InternetExplorerIntegrationLevel" /t REG_DWORD /d 1 /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge" /v "InternetExplorerIntegrationSiteList" /t REG_SZ /d "%xmlPath%" /f

:: 2. Protocol Whitelist
echo [2/3] Adding PageOffice protocol to whitelist...
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\URLAllowlist" /v "100" /t REG_SZ /d "pageoffice://*" /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge\URLAllowlist" /v "100" /t REG_SZ /d "pageoffice://*" /f

:: 3. Disable Private Network Restrictions (Replaces Chrome Flags)
echo [3/3] Disabling Private Network Access restrictions...
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome" /v "InsecurePrivateNetworkRequestsAllowed" /t REG_DWORD /d 1 /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\InsecurePrivateNetworkRequestsAllowedForUrls" /v "1" /t REG_SZ /d "http://%targetSite%*" /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge" /v "InsecurePrivateNetworkRequestsAllowed" /t REG_DWORD /d 1 /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Edge\InsecurePrivateNetworkRequestsAllowedForUrls" /v "1" /t REG_SZ /d "http://%targetSite%*" /f

echo.
echo ---------------------------------------------------------
echo SUCCESS! 
echo 1. Close ALL browsers.
echo 2. Open Edge and go to: edge://policy
echo 3. Click "Reload Policies".
echo 4. Visit your website: http://%targetSite%:9088
echo ---------------------------------------------------------
pause