Skip to content

Instantly share code, notes, and snippets.

@towo
Created November 8, 2012 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save towo/4038998 to your computer and use it in GitHub Desktop.
Save towo/4038998 to your computer and use it in GitHub Desktop.
Patches xdg-open to decode scheme:// URLs by querying xdg-mime
--- /usr/bin/xdg-open 2011-10-04 06:49:38.000000000 +0200
+++ /home/towo/bin/xdg-open 2012-11-08 15:05:36.632522149 +0100
@@ -446,8 +446,35 @@
fi
fi
+ if (echo "$1" | egrep -q '^[a-z]+:'); then
+ scheme=`echo "$1" | cut -d: -f1`;
+ handler=`xdg-mime query default x-scheme-handler/$scheme`;
+ if [ -n "$handler" ] ; then
+ xdg_user_dir="$XDG_DATA_HOME"
+ [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
+
+ xdg_system_dirs="$XDG_DATA_DIRS"
+ [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
+
+ for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
+ local file="$x/applications/$handler"
+ if [ -r "$file" ] ; then
+ command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
+ command_exec=`which $command 2>/dev/null`
+ if [ -x "$command_exec" ] ; then
+ $command_exec "$1"
+ if [ $? -eq 0 ]; then
+ exit_success
+ fi
+ fi
+ fi
+ done
+ fi
+ fi
+
OLDIFS="$IFS"
IFS=":"
+
for browser in $BROWSER; do
IFS="$OLDIFS"
if [ x"$browser" != x"" ]; then
@towo
Copy link
Author

towo commented Nov 8, 2012

If xdg-open doesn't detect a desktop environment, it defaults to using the open_generic tree. open_generic tries to see if it's a path/file URL, in which case a wrapper for xdg-mime query is called (the wrapper is reused here). If it doesn't find anything, it just gives up and hands it over to the browser.

But in the case of magnet: urls and such, handing it to the browser is rather counter-productive. Thus this little patch that queries for a relevant x-scheme-handler for the given URL. Be sure to use e.g. xdg-mime default transmission-gtk.desktop x-scheme-handler/magnet to define a local handler for magnet URLs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment