User Tools

Site Tools


wiki:create_custom_uri_url_handlers

Create custom URI/URL handlers

If you want to be able to open URIs like ssh://user@host from Firefox in Terminal application.

Creating the Handlers

Make a .desktop file in ~/.local/share/applications. Example ssh-handler.desktop:

[Desktop Entry]
Type=Application
Name=SSH Handler
Exec=ssh-handler.sh %u
Icon=utilities-terminal
StartupNotify=false
MimeType=x-scheme-handler/ssh;

Create handler script itself

ssh-handler.sh
#!/bin/bash
 
# Debug option
set -o xtrace
 
(
echo "Parsing URI..."
# If there is an @ sign in uri parse it, uri is in the form of ssh://user@host or ssh://user@host:port
if [[ "${1}" =~ "@" ]];then
    echo "In if statement"
    user=$(echo ${1} | awk -F// '{ print $2 }' - | awk -F@ '{ print $1 }' -)
    host=$(echo ${1} | awk -F// '{ print $2 }' - | awk -F@ '{ print $2 }' -| awk -F: '{ print $1 }' -)
    host=${host%/} # Remove trailing slash (seems to be passed in URI)
    port=$(echo ${1} | awk -F: '{ print $3 }' -)
    [ -z "$port" ] && port="22"
 
    gnome-terminal -- ssh $user@$host -p $port
#    terminator -e "ssh $user@$host -p $port"
 
# else $1 is in the form of ssh://host which uses our .ssh/config aliases
else
    echo "In else"
    host=$(echo ${1} | awk -F// '{ print $2 }')
    host=${host%/} # Remove trailing slash (seems to be passed in URI)
 
    gnome-terminal -- ssh $user@$host -p $port
#    terminator -e "ssh $host"
 
fi
) >> /tmp/ssh-handler 2>&1

Save it in ~/.local/bin, make it executable. Add this to you PATH bash variable.

Registering Custom URI Handler

Once this file is created, the last step is to tell the system that the SSH links should be handled by default by this desktop entry. Before we do that, it can be a good idea to check that nothing is currently handling the SSH protocol, or if something is handling it make sure that we can replace it safely. To check that, run the following command:

xdg-mime query default x-scheme-handler/ssh

If the output is blank, we’re good ! It not, use your best judgement to decide if you want to replace it or not.

Now we can run the following command to define our ssh-handler.desktop file as the default handler for ssh protocol:

xdg-mime default ssh-handler.desktop x-scheme-handler/ssh

And that’s it ! Clicking on ssh://... links in your browser should now directly open a terminal window and connect to the specified host.

GVFS Over-Rides

The gvfs system may take precident over what you have specified here, specifically the ssh: handler is aliased to sftp:. Looking in the directory: /usr/share/gvfs/mounts/ we can find the handlers. Edit sftp.desktop and remove the SchemeAlias, like the following example.

[Mount]
Type=sftp
Exec=/usr/libexec/gvfsd-sftp
AutoMount=false
Scheme=sftp
# SchemeAliases=ssh
DefaultPort=22
HostnameIsInetAddress=true

Tested on

  • Xubuntu 20.04.1

See also

References

wiki/create_custom_uri_url_handlers.txt · Last modified: 2021/06/04 09:21 by antisa

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki