teaser

Selecting the browser when opening links in Linux

This article describes how to setup a dialog for selecting what browser to use when opening a link in typical Linux desktop environments. The dialog is created with Zenity from a bash script. It shows up every time a URL is clicked from a non-browser application.

Having multiple browsers installed is an effective way to encapsulate certain online tasks in separate environments. One browser might be used for banking, one for social media and one for general browsing. Separating the tasks like this limit the reach of tracking cookies from social media and may improve the security of critical operations.

Making the select-browser script

The script presents a dialog like this:

tram in city picture

Install Zenity:

sudo apt install zenity

Make the script:

sudo nano /usr/bin/select-browser and paste the following:

#!/bin/bash

BROWSERS='TRUE Firefox FALSE Chromium FALSE Vivaldi FALSE Opera'
HEIGHT=210

LINK=$*
BROWSER=$(zenity --list --radiolist --text "${LINK:0:100}" --column='' --column='Browsers' --height=$HEIGHT --title='Select browser to open link' $BROWSERS)

if [ ! -z "$BROWSER" ];
then
  # bash magic to make BROWSER lower case:
  ${BROWSER,,} "$LINK" &
fi

The BROWSERS variable defines the range of browsers to choose from and sets Firefox as default in the above example. Modify as desired.

The script assumes that the browsers can be invoked by running their name in lower case as a general command. Long links will be cropped to first 100 characters when displayed in the dialog.

Make the script executable:

sudo chmod +x /usr/bin/select-browser

and test it:

select-browser www.somesite.com

Associating the script as the default "browser"

One or more of the following techniques may be required for associating the script with opening URLs in the desktop environment.

Make a .desktop file:

nano ~/.local/share/applications/select-browser.desktop and paste the following:

[Desktop Entry]
Version=1.1
Type=Application
Name=Select browser
Exec=/usr/bin/select-browser %U
Actions=new-window;new-private-window;NewShortcut;NewShortcut1;
Categories=Network;WebBrowser;
StartupNotify=true
StartupWMClass=select-browser

MimeType=x-scheme-handler/unknown;x-scheme-handler/about;text/html;text/xml;application/xhtml_xml;image/webp;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp;

and associate it with xdg-settings: (this works for Gnome)

xdg-settings set default-web-browser select-browser.desktop

In KDE, the default browser must be set through the System Settings. Choose:

  • Default Applications
    • Web Browser, and pick the select-browser script for "Open http and https URLs"

Finally, configuring with update-alternatives may be useful in Ubuntu related environments, try one or more of these commands:

sudo update-alternatives --install /usr/bin/x-www-browser x-www-browser /usr/bin/select-browser 300`
sudo update-alternatives --config x-www-browser
sudo update-alternatives --display x-www-browser
sudo update-alternatives --install /usr/bin/gnome-www-browser gnome-www-browser /usr/bin/select-browser 300
sudo update-alternatives --display gnome-www-browser
sudo update-alternatives --config gnome-www-browser

I hope you enjoyed this content!

ko-fi donate

Comments

Comments powered by Talkyard