2

I would like to register a URL scheme (or protocol) handler for my own custom URL protocol, so that clicking on a link with this custom protocol will execute a command on that URL. Which steps do I need to take to add this handler?

Example: I want to open URLs like ddg://query%20terms in a new DuckDuckGo browser search. If this protocol already exists, I assume that the steps to override a handler don't differ much from the steps to create a new one. Yes, technically, this is just a URL scheme, not a protocol.

4

To register a new URL scheme handler with XDG, first create a Desktop Entry which specifies the x-scheme-handler/... MIME type:

[Desktop Entry]
Type=Application
Name=DDG Scheme Handler
Exec=open-ddg.sh %u
StartupNotify=false
MimeType=x-scheme-handler/ddg;

Note that %u passes the URL (e.g. ddg://query%20terms) as a single parameter, according to the Desktop Entry Specification.

Once you have created this Desktop Entry and installed it (i.e. put it in the local or system applications directory for XDG, like ~/.local/share/applications/ or /usr/share/applications/), then you must register the application with the MIME type (assuming you had named your Desktop Entry ddg-opener.desktop):

xdg-mime default ddg-opener.desktop x-scheme-handler/ddg

A reference implementation of the ddg-open.sh handler:

#!/bin/bash

# bash and not just sh because we are using some bash-specific syntax

if [[ "$1" == "ddg:"* ]]; then
    ref=${1#ddg://}
    #ref=$(python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])" "$ref") # If you want decoding
    xdg-open "https://duckduckgo.com/?q=$ref"
else
    xdg-open "$1" # Just open with the default handler
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.