Skip to content

Instantly share code, notes, and snippets.

@noamnotyou
Last active November 20, 2022 06:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noamnotyou/64e1e2639eb17ad58877985b0b7a379c to your computer and use it in GitHub Desktop.
Save noamnotyou/64e1e2639eb17ad58877985b0b7a379c to your computer and use it in GitHub Desktop.
Force Vivaldi browser to open URL links in new windows
@echo off
title New windows for external links, for Vivaldi browser - by noamnotyou
set workdir=%temp%\noamnotyou
set errmsg="Unknown"
set args=--
set nw=--new-window
set im=-incognito
echo.
echo Script to force Vivaldi to open (external) URLs in new normal/private window, by noamnotyou.
echo.
echo.
:sel
echo What to do when you click external links?:
echo F = force opening links in a new window each time
echo D = restore default behaviour
echo E = exit this script without change
echo J = Forget about changing stuff, just rainbow spam this cmd window! :D
echo.
choice /c fdej /n
if %errorlevel%==1 (
set selection=%nw% %args%
)
if %errorlevel%==2 (
set selection= %args%
goto commit
)
if %errorlevel%==3 exit
if %errorlevel%==4 goto spam
echo.
echo.
echo What kind of window should external links open in:
echo P = private / incognito window
echo N = normal window (default)
echo.
choice /c pn /n
if %errorlevel%==1 (
set selection= %im% %selection%
)
:commit
echo.
echo.
echo Continue with your choice(s)? [Y/N]
echo.
choice /c yn /n
if %errorlevel%==2 goto sel
if not exist %workdir%\ md %workdir%
echo.
echo.
echo Finding Vivaldi's keys in registry...
set stage=reg-q-k
reg query hkcr /s /k /f "VivaldiHTM.*">%workdir%\vivaldi-re-keys
if %errorlevel% neq 0 (
set errmsg="Couldn't query registry."
goto encerror
)
echo Done.
echo.
echo Making the changes... (repeats if there are multiple Vivaldi keys)
set stage=fstr-a
for /f "tokens=* USEBACKQ" %%G in (`findstr /r /c:"HKEY_CLASSES_ROOT\\VivaldiHTM\.[0-9a-zA-Z]*" %workdir%\vivaldi-re-keys`) do (
set stage=reg-e
reg export "%%G\shell\open\command" "%workdir%\exportedkey.reg" /y
if %errorlevel% neq 0 (
set errmsg="Couldn't export key."
goto encerror
)
set stage=psr-a
powershell -Command "(gc '%workdir%\exportedkey.reg') -replace '(%im%)|(%nw%)|( %arg% )', '' | Out-File -encoding UTF8 '%workdir%\clearedkey.reg'"
if %errorlevel% neq 0 (
set errmsg="Couldn't clear exported key."
goto encerror
)
set stage=psr-b
powershell -Command "(gc '%workdir%\clearedkey.reg') -replace '^(.*?)( -.*)', '$1 %selection% $2' | Out-File -encoding UTF8 '%workdir%\modifiedkey.reg'"
if %errorlevel% neq 0 (
set errmsg="Couldn't modify cleared key."
goto encerror
)
set stage=psr-c
powershell -Command "(gc '%workdir%\modifiedkey.reg') -replace ' (?= +)| (!?= +)|( *-+(?= ) )', '' | Out-File -encoding UTF8 '%workdir%\formattedkey.reg'"
if %errorlevel% neq 0 (
set errmsg="Couldn't format cleared key."
goto encerror
)
set stage=reg-i
echo Please provide RegEdit with admin access to apply changes.
regedit /s %workdir%\formattedkey.reg
if %errorlevel% neq 0 (
set errmsg="Couldn't import formatted key."
goto encerror
)
set stage=fstr-b
)
if %errorlevel% neq 0 (
set errmsg="Unknown error."
goto encerror
)
if %stage% neq fstr-b (
set errorlevel=1
set errmsg="Couldn't find Vivaldi's registry key(s)."
goto encerror
)
echo Done.
echo.
echo.
goto cleanup
:spam
title Rainbow spam! :D
:sspam
set /a count=31
:ssspam
echo [%count%mSPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM SPAM
set /a count=%count% + 1
if %count%==36 goto sspam
goto ssspam
:encerror
set errmsg=%errmsg:"=%
echo.
echo.
echo Oops! :( Ran into an error: %errmsg% Error code: %errorlevel% Stage: %stage%.
echo Please try relaunching the script or tell @noamnotyou if it keeps happening.
echo.
echo Remove working directory? [Y/N]
echo (select N if needed for debugging errors)
echo.
choice /c yn /n
if %errorlevel%==2 goto finished
:cleanup
echo.
echo.
echo Removing generated files...
rmdir %workdir% /s /q
echo Done.
:finished
echo.
echo.
echo Press any key to close this window.
pause>nul
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment