Motivation

One eventually stumbles into the problem of routing a url to it's intended program. The operating system configures some defaults, but they are often inadequate. The typical solution is to copy the url into the system clipboard and then paste it into the desired program. This is better but still limiting. A more sophisticated solution is to use a clipboard manager to route the copied url. Two such tools are parcellite and xclip, and they work well. My preferred solution is dmenu and a shell script, which is what this post is about. This solution is leaner and offers more control, at the expense of doing some shell scripting.

dmenu

dmenu is a menu tool from suckless tools. It gives the user a menu and echoes the choice. A shell script can grab and process the choice echoed by dmenu, in effect routing a url to the program chosen by the user. One can do this by creating a script browser-launcher.sh and making it the default browser.

#!/bin/sh
# browser-launcher.sh

# if no url provided, use the one in the clipboard

if ! [ "$1" ]; then
    set -- "$(xclip -o -selection clipboard)"

fi

browser=$(
    dmenu -l 5 -p "Open with:" <<-EOF
        w3m
        feh
        firefox
        mkebook
        yt-dlp
        chromium
    EOF
)

# $1 contains the url to route
# $browser contains the menu item chosen by the user, i.e. the program to send the url to

# send $1 to $browser program the background
{
    case "$browser" in
      w3m)
           # ${1%%\?*} is necessary to strip parameter options, or w3m exits prematurely
           urxvt -e sh -c "w3m ${1%%\?*}" ;;
      feh) feh "$1" ;;
      firefox) firefox "$1" ;;
      mkebook) mkebook "$1" ;;
      yt-dlp) yt-dlp "$1" ;;
      chromium) chromium "$1" ;;
      *) # default option
         exit 0
      ;;
    esac
} &

To make browser-launcher.sh the default browser copy the file /usr/share/applications/firefox-esr.desktop to $HOME/.local/share/applications/browser-launcher.desktop. Edit the new file changing the line starting with Exec to Exec=browser-launcher.sh %u. This assumes that browser-launcher.sh is in the $PATH Then we make it the default browser by

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

One can also setup a hotkey to launch browser-launcher.sh.

The file browser-launcher.sh in the example above is simple and is meant as a template to demonstrate proof of concept. Since it's a shell script one can do some sophisticated processing. This is where the approach proves superior to a clipboard manager. Below is a (slightly) more complicated case. We process an x.com url to extract the user name and launch firefox with the new url pointing to the users threadreader.com page.

firefox-threadreader)
   url=$1
   url=${url#http://}
   url=${url#https://}
   url=${url#*/}
   url=${url%%/*}
   firefox "https://threadreaderapp.com/user/$url"
   ;;

fzf

fzf offers a similar capability to dmenu with the benefit of fuzzy searching through menu items. The limitation is the menu can only be deployed in the terminal which is limiting.