@echo off
setlocal
REM ============================================================
REM  Dotb - anti-freeze browser launcher
REM  https://dotb.io/guides/stop-browser-freezing-automation-tab
REM
REM  Starts Chrome or Brave in a mode where the browser does not
REM  freeze your background tabs, so your Dotb automations keep
REM  running while you work in another account.
REM
REM  This file never closes your browser by itself.
REM ============================================================

title Dotb - anti-freeze browser launcher

REM --- Optional: only if you use browser profiles for multiple
REM --- Vinted accounts. Put the profile name below, e.g.
REM ---    set "PROFILE=Profile 1"
REM --- Leave it empty to use your normal profile.
set "PROFILE="

set "FLAGS=--disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-renderer-backgrounding --disable-features=CalculateNativeWinOcclusion,IntensiveWakeUpThrottling"
set "MARKER=disable-renderer-backgrounding"

echo.
echo   Dotb - anti-freeze browser launcher
echo   ===================================
echo.
echo   Looking for Chrome and Brave on this computer...

REM ---------------------- find Chrome ----------------------
set "CHROME="
if exist "%ProgramFiles%\Google\Chrome\Application\chrome.exe" set "CHROME=%ProgramFiles%\Google\Chrome\Application\chrome.exe"
if not defined CHROME if exist "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe" set "CHROME=%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"
if not defined CHROME if exist "%LocalAppData%\Google\Chrome\Application\chrome.exe" set "CHROME=%LocalAppData%\Google\Chrome\Application\chrome.exe"
if not defined CHROME for /f "tokens=2,*" %%A in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" /ve 2^>nul ^| find "REG_SZ"') do set "CHROME=%%B"
if not defined CHROME for /f "tokens=2,*" %%A in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" /ve 2^>nul ^| find "REG_SZ"') do set "CHROME=%%B"

REM ----------------------- find Brave -----------------------
set "BRAVE="
if exist "%ProgramFiles%\BraveSoftware\Brave-Browser\Application\brave.exe" set "BRAVE=%ProgramFiles%\BraveSoftware\Brave-Browser\Application\brave.exe"
if not defined BRAVE if exist "%ProgramFiles(x86)%\BraveSoftware\Brave-Browser\Application\brave.exe" set "BRAVE=%ProgramFiles(x86)%\BraveSoftware\Brave-Browser\Application\brave.exe"
if not defined BRAVE if exist "%LocalAppData%\BraveSoftware\Brave-Browser\Application\brave.exe" set "BRAVE=%LocalAppData%\BraveSoftware\Brave-Browser\Application\brave.exe"
if not defined BRAVE for /f "tokens=2,*" %%A in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\brave.exe" /ve 2^>nul ^| find "REG_SZ"') do set "BRAVE=%%B"
if not defined BRAVE for /f "tokens=2,*" %%A in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\brave.exe" /ve 2^>nul ^| find "REG_SZ"') do set "BRAVE=%%B"

if not defined CHROME if not defined BRAVE goto notfound
if not defined BRAVE goto usechrome
if not defined CHROME goto usebrave

REM --------------------- both installed ---------------------
echo.
echo   Both browsers are installed. Which one do you use for Vinted?
echo.
echo     [1] Google Chrome
echo     [2] Brave
echo.
choice /C 12 /N /M "  Type 1 or 2 then press Enter: "
if errorlevel 2 goto usebrave
goto usechrome

:usechrome
set "TARGET=%CHROME%"
set "DOTB_EXE=chrome.exe"
set "LABEL=Google Chrome"
goto check

:usebrave
set "TARGET=%BRAVE%"
set "DOTB_EXE=brave.exe"
set "LABEL=Brave"
goto check

REM ---- is it already running, and is it already protected? ----
:check
echo.
echo   Found %LABEL%:
echo   %TARGET%

tasklist /FI "IMAGENAME eq %DOTB_EXE%" 2>nul | find /I "%DOTB_EXE%" >nul
if errorlevel 1 goto launch

echo.
echo   %LABEL% is already open. Checking it...

set "PROTECTED="
for /f "usebackq delims=" %%L in (`powershell -NoProfile -Command "@(Get-CimInstance Win32_Process ^| Where-Object { $_.Name -eq $env:DOTB_EXE -and $_.CommandLine -like ('*' + $env:MARKER + '*') }).Count" 2^>nul`) do set "PROTECTED=%%L"

if "%PROTECTED%"=="" goto needsrestart
if "%PROTECTED%"=="0" goto needsrestart

echo.
echo   Good news - %LABEL% is already running in anti-freeze mode.
echo   There is nothing to do. Your automations will not be paused.
echo.
pause
exit /b 0

:needsrestart
echo.
echo   %LABEL% is open, but not in anti-freeze mode.
echo.
echo   This file will not close it for you - you may have work in
echo   progress. Please close %LABEL% yourself:
echo.
echo     1. Close every %LABEL% window
echo     2. Look at the bottom-right of your screen, next to the
echo        clock, and if you see a %LABEL% icon there, right-click
echo        it and choose Exit
echo     3. Run this file again
echo.
pause
exit /b 1

:launch
echo.
echo   Starting %LABEL% in anti-freeze mode...
if defined PROFILE (
  start "" "%TARGET%" --profile-directory="%PROFILE%" %FLAGS%
) else (
  start "" "%TARGET%" %FLAGS%
)

echo.
echo   All set. Open Vinted and start your Dotb automations as usual.
echo.
echo   From now on, open your browser with this file every time you
echo   use Dotb - your normal shortcut will not do the same thing.
echo.
pause
exit /b 0

:notfound
echo.
echo   Could not find Chrome or Brave in the usual folders.
echo.
echo   Either they are not installed, or they are installed in a
echo   custom location. Come and tell us on Discord and we will
echo   help you out:
echo   https://dotb.io/guides/stop-browser-freezing-automation-tab
echo.
pause
exit /b 1
