Custom URLs in Chromium (Edited)

I’ve been tinkering with Second Life and Spotify on Linux, and discovered there’s a known bug in XDG whereby it complains when you try to access a custom protocol link (e.g. secondlife:// or spotify://) that is already registered within XDG. There’s a (rather cumbersome) way of getting around it, if you’re using Chromium (probably works with Chrome, but I haven’t tried it) First step, add a mime desktop entry – usually in ~/.local/share/application/. This entry must contain a MimeType entry, and a NoDisplay entry. Use the %u to pass the url. This desktop entry will NOT be visible on the menus, and is solely to “connect” to the application you want to invoke when the link is clicked. This example is using the Singularity viewer as an example.

[Desktop Entry]
 Name=Singularity
 Comment=Client for Online Virtual Worlds, such as Second Life
 Exec=~/.singularity-install/handle_secondlifeprotocol.sh %u
 Icon=~/.singularity-install/singularity_icon.png
 Terminal=false
 Type=Application
 Categories=Application;Network;
 StartupNotify=true
 X-Desktop-File-Install-Version=3.0
 MimeType=x-scheme-handler/x-secondlife-url
 NoDisplay=true

Next, inside the program you are calling, you may need to do some manipulation of the passed URL as it will come with escapes (e.g. %20 = space).  See later in the post for an example

Finally, make Chromium aware of this new handler, by editing the Chromium preferences, found in (for the Default user), in ~/.config/chromium/Default/Preferences. Chromium must be closed for this to work, or you will lose your changes. In the Preferences file, add this block of code:

"custom_handlers": {
 "enabled": true,
 "ignored_protocol_handlers": [ ],
 "registered_protocol_handlers": [ {
 "default": true,
 "protocol": "secondlife",
 "title": "Second Life",
 "url": "x-secondlife-url:%s"
 } ]
 },

Note the trailing comma, and the url must be prefixed with the mime type you declared in the desktop entry. Start up Chromium afterwards, then click the link. When you click on the link, Chromium gets the link, finds there is a custom handler (in Preferences file), passes it to the mime desktop link you created (which contains the mime type), finds the program to use from the desktop link, and passes the url to the program using the arguments you specified.

Here’s an example of what I mean by manipulating of the passed URL. This is the script from the Firestorm Viewer. Singularity uses a VERY similar script so the code should be nearly interchangeable. I have hacked this script (changes I have made are in bold) so that it works with SLURLs (Second Life URLs)  with both spaces, and prefixed with “app/region”

#!/bin/bash

URL="$1"
NEWURL=$(echo $URL| perl -pe 's/^x-secondlife-url://; tr/+/ /; s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;')
URL=echo $NEWURL | sed 's/secondlife:\/\///g' | sed 's/\/app\/region\///g'
if [ -z "$URL" ]; then
#echo Usage: $0 secondlife://...
echo "Usage: $0 [ secondlife:// | hop:// ] ..."
exit
fi
RUN_PATH=
dirname "$0" || echo .
#cd “${RUN_PATH}/..”
cd “${RUN_PATH}”
#exec ./firestorm -url \'”${URL}”\’
if [ pidof do-not-directly-run-firestorm-bin ]; then
exec dbus-send –type=method_call –dest=com.secondlife.ViewerAppAPIService /com/secondlife/ViewerAppAPI com.secondlife.ViewerAppAPI.GoSLURL string:$URL
else
exec ../firestorm -url $URL
fi

Snippet: Chromium Snapshot Download (Cygwin) (Updated 3rd April)

A snippet to download and run the latest build of the Chromium browser. Meant for Cygwin/Windows but can be adapted if needed.

If you need to use a proxy, set your http_proxy and https_proxy variables before using this.

Now updated to kill running Chrome/Chromium processes before running the installer (in case of locking issues). Also created a Bitbucket repo here for this script

if [ -f ./mini_installer.exe ]; then
  echo "WARNING: Previous script run did not clean up"
  rm ./mini_installer.exe
fi

if [ ! -f prev ]; then
  echo "No previous build logged"
  echo "-1" >prev
fi

if [[ `wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O-` == `cat prev` ]]; then
  echo "No build change (`wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O-` = `cat prev`)"
  exit 1
else
  echo "New build (previous: `cat prev`, new: `wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O-`)"
fi
echo Downloading http://commondatastorage.googleapis.com/chromium-browser-snapshots/`wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O-`/mini_installer.exe
wget http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/`wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O-`/mini_installer.exe

if [[ `ps -W | grep chrome | wc -l` != 0 ]]; then
  echo "`ps -W | grep chrome | wc -l` Chrome Processes Running. Attempting to Kill"
  for a in `ps -W | grep chrome | awk '{ print $1}'`
    do
      echo Killing PID $a
      /bin/kill -f $a #This is the cygwin kill, not the bash kill
    done
else
  echo "No Chrome Processes Running"
fiecho "Running installer"
chmod +x ./mini_installer.exe
./mini_installer.exe
echo Done
wget -q http://commondatastorage.googleapis.com/chromium-browser-snapshots/Win/LAST_CHANGE -O prev
echo "New build recorded (`cat prev`)"
rm ./mini_installer.exe
%d bloggers like this: