Patching Vivaldi with batch scripts
-
This topic contains a collection of batch scripts suitable for automating the patching of your Vivaldi application after updates, as described in Modding Vivaldi. The example scripts presented in this post omit the handling of CSS files, because the experiment โAllow for using CSS modificationsโ already takes care of this neatly.
Windows
- Place this script in the folder which contains the *.js files to install.
- Edit the
installPath
variable to point to your Vivaldi application. - Doubleโclick the file to install modifications.
:: Author: debiedowner @echo off REM make current directory work when run as administrator cd "%~dp0" set installPath="C:\Program Files\Vivaldi\Application\" echo Searching at: %installPath% for /f "tokens=*" %%a in ('dir /a:-d /b /s %installPath%') do ( if "%%~nxa"=="window.html" set latestVersionFolder=%%~dpa ) if "%latestVersionFolder%"=="" ( pause & exit ) else ( echo Found latest version folder: "%latestVersionFolder%" ) if not exist "%latestVersionFolder%\window.bak.html" ( echo Creating a backup of your original window.html file. copy "%latestVersionFolder%\window.html" "%latestVersionFolder%\window.bak.html" ) echo copying js files to custom.js type *.js > "%latestVersionFolder%\custom.js" echo patching window.html file type "%latestVersionFolder%\window.bak.html" | findstr /v "</body>" | findstr /v "</html>" > "%latestVersionFolder%\window.html" echo ^<script src="custom.js"^>^</script^> >> "%latestVersionFolder%\window.html" echo ^</body^> >> "%latestVersionFolder%\window.html" echo ^</html^> >> "%latestVersionFolder%\window.html" pause
macOS
Copy the script, save it to your
$PATH
(e.g./usr/local/bin/
) and make it executable (chmod +x
). You will need to adaptPATH_TO_FILE
and the file names depending on your setup.#!/bin/bash # Original version by Isildur, adapted by luetage # Quit Vivaldi osascript -e 'quit app "Vivaldi.app"' # Find path to Framework folder of current version and save it as variable findPath="`find /Applications/Vivaldi.app -name Vivaldi\ Framework.framework`" # Copy custom js file to Vivaldi.app cp /PATH_TO_FILE/custom.js "$findPath"/Resources/vivaldi/ # Save path to window.html as variable browserHtml="$findPath"/Resources/vivaldi/window.html # Insert references, if not present, and save to temporary file sed 's| </body>| <script src="custom.js"></script></body>|' "$browserHtml" > "$browserHtml".temp # Backup original file cp "$browserHtml" "$browserHtml".bak # Overwrite mv "$browserHtml".temp "$browserHtml" # Pause script read -rsp $'Press [Enter] to restart Vivaldi...\n' # Open Vivaldi open /Applications/Vivaldi.app
Linux
Copy the script, save it to your
$PATH
(e.g./usr/local/bin/
) and make it executable (chmod +x
). You will need to adapt themod_dir
and the file names depending on your setup.#!/bin/bash ####################################################################### # Author: GwenDragon, <https://labs.gwendragon.de/blog/> # License: GPL ####################################################################### # 2021-06-25 removed custom.css support # commented custom.css shell commands # users can now use vivaldi://experiments to change CSS in UI mod_dir=$HOME/path/to/dir vivaldi_installs=$(dirname $(find /opt -name "vivaldi-bin" )) ; vivaldi_install_dirs=( $vivaldi_installs ) ; echo "---------------------" count=1 selected=0 echo "Installations found:" for dir in $vivaldi_installs ; do echo $dir": "$count ; ((count++)) ; done read -p " Select installation to patch. Input number and press [Enter] or [X] to cancel. Input selection: " selected ; if [ "$selected" = "X" ] ; then exit ; fi ((selected--)) ; if [ $selected -ge ${#vivaldi_install_dirs[@]} ] ; then echo "Selection too large!" fi dir=${vivaldi_install_dirs[$selected]} ; echo "--------------------- " echo "Patch originating from "${mod_dir}" targeting "${vivaldi_install_dirs[$selected]} ; sudo cp "$dir/resources/vivaldi/window.html" "$dir/resources/vivaldi/window.html-$(date +%Y-%m-%dT%H-%M-%S)" alreadypatched=$(grep '<script src="custom.js"></script>' $dir/resources/vivaldi/window.html); #alreadypatched=$(grep '<link rel="stylesheet" href="style/custom.css" />' $dir/resources/vivaldi/window.html); if [ "$alreadypatched" = "" ] ; then echo patching window.html #sudo sed -i -e 's/<\/head>/<link rel="stylesheet" href="style\/custom.css" \/> <\/head>/' "$dir/resources/vivaldi/window.html" sudo sed -i -e 's/<\/body>/<script src="custom.js"><\/script> <\/body>/' "$dir/resources/vivaldi/window.html" else echo "window.html has already been patched!" fi #if [ -f "$mod_dir/custom.css" ] ; then # echo copying custom.css # sudo cp -f "$mod_dir/custom.css" "$dir/resources/vivaldi/style/custom.css" #else # echo custom.css missing in $mod_dir #fi if [ -f "$mod_dir/custom.js" ] ; then echo copying custom.js sudo cp -f "$mod_dir/custom.js" "$dir/resources/vivaldi/custom.js" else echo custom.js missing in $mod_dir fi
Please feel free to share your own scripts, but make sure to include information on which platform they target. Should you have any questions or run into issues, let us know in a comment.
-
set "modFolder=C:\Users\You\Vivaldi Mod Files"
seems wrong to me, should be, if anything
set modFolder="C:\Users\You\Vivaldi Mod Files"
you can also use %LOCALAPPDATA% instead of C:\Users\You\AppData\Local
and %USERPROFILE% instead of C:\Users\You -
It seems odd, but this is the only correct way of assigning paths including whitespaces to a batch script variable.
Your version assigns a string including the quotation marks to the variable.And you know that these folders are example paths, right? They don't get used in production unless you forget to substitute them. :silly:
-
it's strange, it doesn't seem needed to me
set "modFolder=C:\Users\You\Vivaldi Mod Files"
echo x%modFolder%x
set modFolder=C:\Users\You\Vivaldi Mod Files
echo x%modFolder%xthis produces the same output, no need of quotes at all
-
Believe me, you do. Echoing (as text) is not the same as using it as a folder. This script got tested by us and is solid.
-
And you know that these folders are example paths, right? They don't get used in production unless you forget to substitute them. :silly:
of course, these system variables are already set so you don't have to edit the batch everytime you change user or pc
-
Mod folder and batch file should be put together to work correctly?
Custom.css copying worked, but other files are not always correctly copied (the browser.html was backed up, but not copied); it says something like "cyclic error".I've script in C:\Portable programs\VivaldiRC\script.bat
and mod folder in C:\Portable programs\VivaldiRC\Application\Mod
Paths in the batch points to my mod folder/vivaldi, obviously -
You seem to be doing something wrong.
You can place the mod where you want. It doesn't need to stay in your modding directory.
Just make sure to set
[ul]- modFolder to the root of your mods (i.e. "modFolder=C:\Portable programs\VivaldiRC\Application\Mod") and
- installPaths to your Vivaldi Application directory (i.e. installPaths="C:\Portable programs\VivaldiRC\Application" in your case)
[/ul]
and pay attention to not add additional whitespaces and keep the quotation marks where they are.
-
Windows
Searching for newest browser.html in C:\Users\Bende\AppData\Local\Vivaldi\Application\...'dir /a:-d /b /s "C:\Users\Bende\AppData\Local\Vivaldi\Application\"' is not recognized as an internal or external command, operable program or batch file. Couldn't find it. :( Is C:\Users\Bende\AppData\Local\Vivaldi\Application\ the correct Vivaldi Application folder? All done! :) Successfully patched 0 out of 1 installs. Press any key to continue . . .
It's kind of difficult to make out the code as Markdown has mangled it somewhat (whoever devised Markdown as a formatting system needs shooting before they reproduce any more), but as far as I can tell, the Command Prompt is parsing the dir command incorrectly, and it is not performing the search that the command ought to do. The only things in this Batch that I've modified are the two sets at the top.
To answer the Batch File's query: yes, that is the correct Application folder. it has the Application in it.
So what's going on? -
Hi, the new forum doesn't display correctly the first post
-
@Hobbes Yeah, the new forum unfortunately doesn't support the same markup as the old one did. I tried to fix that for you
-
@Christoph142
Great, thanks ! -
Does anyone have the complete version of this? I don't think the snippets above can possibly be all.
-
@rseiler It wasn't. It seems to have been corrupted at some point. I uploaded it again just now.
-
It would be really nice if Vivaldi could finally implement an official method. Is anything developing on that front?
-
@luetage not to my knowledge I'm afraid. But I'll keep bugging them.
-
Windows
First, thank you for your script. I have made a few modifications for my purposes, but I am having a tough time modifying your batch file to always replace the files in the install path with the files in the mod path regardless of whether Vivaldi was recently updated or not.
I removed the counter successfully and simplified the script to only update one installpath, but when I run it, it will only work after a recent vivaldi upgrade, otherwise I get a cyclical error message [Yes, my mod folder is in the same directory as the installpath] and I still have to delete the original browser.html in the install path and rename the copied browser.bak.html to browser.html
::::::::::::::::::::::::::::::::::::::::::: :: Script to copy over your Vivaldi mods :: :: by Christoph142 :: ::::::::::::::::::::::::::::::::::::::::::: @echo off setlocal enabledelayedexpansion :: To use this script, change the two set paths below and scroll all the way down and also change the webpage that launches in the Vivaldi window. :: This is the folder that contains your modded files (make sure it uses similar folder structure): set "modFolder=C:\Vivaldi\Application\DefaultPolicy" :: This is the folder that contains your Vivaldi Application installation folder: set installPaths="C:\Vivaldi\Application\" :: Don't alter anything below this point ;) for %%i in (%installPaths%) do ( <NUL set /p=Searching for newest browser.html in %%~dpi... set installPath=%%~dpi set latestVersionFolder= for /f "tokens=*" %%a in ('dir /a:-d /b /s "!installPath!"') do ( if "%%~nxa"=="browser.html" set latestVersionFolder=%%~dpa ) echo Copying files echo from %modFolder% echo into !latestVersionFolder!: xcopy /s /y "%modFolder%" "!latestVersionFolder!" echo. ) ) echo All done^^! :) echo Successfully patched Vivaldi echo. pause
-
macOS
I originally posted this in another thread. At @luetage's suggestion, I'm posting this here too. Be careful; this works for me, but you may have some setup I'm not thinking of, so you might want to create extra an backup of the Vivaldi app before first trying this. It might also be neater to do the "find" only once and save that to a variable, but you get the idea. like I said below, you'll need to edit the ALL-CAPS path placehoders to suit your locations. It runs a "find" because in OS X vivaldi, the path changes every version to a different subdirectory in the app package.
@TwisterMc said in Tab Loading Indicator:
Is there a way to add custom CSS to an extension so I don't have to keep editing the custom.css? Or a CSS file that will remain after I update snapshot?
What I've been doing is running up script file after every update. Of course, replace the paths to suit your file locations.
#!/bin/bash # Copy the css file to whatever the right directory is for this version of Vivaldi cp SOMEPATH/vivaldi-custom.css "`find WHATEVERAPPLICATIONFOLDERPATHYOU'REUSNG/Vivaldi.app -name Vivaldi\ Framework.framework`"/Resources/vivaldi/style/ # Find the path to browser.html and save that to a variable. browserHtml="`find WHATEVERAPPLICATIONFOLDERPATHYOU'REUSNG/Vivaldi.app -name Vivaldi\ Framework.framework`"/Resources/vivaldi/browser.html # insert in the stylesheet reference if it's not there and save that to browser.html.temp sed 's| </head>| <link rel="stylesheet" href="style/vivaldi-custom.css" /></head>|' "$browserHtml" > "$browserHtml".temp # Save a backup (why not). cp "$browserHtml" "$browserHtml".bak # Overwrite the browser.html file. mv "$browserHtml".temp "$browserHtml"
2017.09.01 Update: See also @luetage's version below , which in addition to neatening the above script a bit, also closes the browser, copies a custom JS file (and adds a script tag to the end of the HTML body to link to it), pauses for the user to press Enter, opens the new copy of the custom.css, and starts the browser again.
(As luetage described in the intervening posts, the pause is to deal with an issue where the script might not launch Vivaldi if the script issued the relaunch command too soon.) -
This is really instructive. Thanks!
I added the executable tousr/local/bin
to make it easier to execute. Afterwards I tried to add some functionality and ran into a (non-)issue.# runs as first action osascript -e 'quit app "Vivaldi.app"' # runs at the end open -a "TextWrangler" "`find /Applications/Vivaldi.app -name Vivaldi\ Framework.framework`"/Resources/vivaldi/style/custom.css open -a "Vivaldi"
When I'm using the script, it's in a situation when Vivaldi is open and without modifications (directly after an update). Therefore when running this script I want to first close Vivaldi, then run the script, and then open Vivaldi to have my customized version already started up. In between I also open the new
custom.css
file in my text editor. I just added this to above code to show what is different in my version. The issue is that Vivaldi successfully closes, but it doesn't open again at the end. Am I missing something? I also tried to open Vivaldi with another osascript command at the end, which doesn't work either. Both commands work from the shell though. -
Hmm, I think I found the problem, I just needed to insert a pause, or Vivaldi tries to restart when it is still open. I'm using zsh, so don't know if this code will work in bash.
echo "Customization successful. Press any key to restart Vivaldi."; read -k1 -s