Last active
November 20, 2022 06:20
-
-
Save noamnotyou/64e1e2639eb17ad58877985b0b7a379c to your computer and use it in GitHub Desktop.
Force Vivaldi browser to open URL links in new windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 [36mScript to force Vivaldi to open (external) URLs in new normal/private window, by noamnotyou. | |
echo. | |
echo. | |
:sel | |
echo [36mWhat 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[31m | |
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 [36mWhat kind of window should external links open in: | |
echo P = private / incognito window | |
echo N = normal window (default)[31m | |
echo. | |
choice /c pn /n | |
if %errorlevel%==1 ( | |
set selection= %im% %selection% | |
) | |
:commit | |
echo. | |
echo. | |
echo [36mContinue with your choice(s)? [Y/N][31m | |
echo. | |
choice /c yn /n | |
if %errorlevel%==2 goto sel | |
if not exist %workdir%\ md %workdir% | |
echo. | |
echo. | |
echo [36mFinding 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 [92mDone. | |
echo. | |
echo [36mMaking 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 [33mPlease provide RegEdit with admin access to apply changes.[36m | |
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 [92mDone. | |
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 [91mOops! :( Ran into an error: [31m%errmsg% [91mError code: [31m%errorlevel% [91mStage: [31m%stage%. | |
echo [91mPlease try relaunching the script or tell [36m@noamnotyou [91mif it keeps happening. | |
echo. | |
echo [36mRemove working directory? [Y/N] | |
echo (select N if needed for debugging errors)[31m | |
echo. | |
choice /c yn /n | |
if %errorlevel%==2 goto finished | |
:cleanup | |
echo. | |
echo. | |
echo [36mRemoving generated files... | |
rmdir %workdir% /s /q | |
echo [92mDone. | |
:finished | |
echo. | |
echo. | |
echo [36mPress 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