open magnet URLs with xdg-open

Browsers on Linux should use xdg-open to open URLs. From xdg-open’s description:

xdg-open opens a file or URL in the user’s preferred application. If a URL is
provided the URL will be opened in the user’s preferred web browser. If a file
is provided the file will be opened in the preferred application for files of
that type. xdg-open supports file, ftp, http and https URLs.

Magnet is a new type of URLs that’s currently not supported by xdg-open. It’s very easy to add a quick hack to support it though. Just copy xdg-open to a local binary path (e.g /usr/local/bin or preferably $HOME/bin) and apply the following patch. Change deluge with your preferred torrent application.

--- /usr/bin/xdg-open  2010-09-15 14:08:29.000000000 +0300
+++ bin/xdg-open  2012-01-24 22:05:03.935338593 +0200
@@ -437,6 +437,11 @@
                 exit_success
             fi
         fi
+    elif (echo "$1" | grep -q '^magnet:'); then
+        deluge "$1" 
+        if [ $? -eq 0 ]; then
+            exit_success
+        fi
     fi
 
     sensible-browser "$1"

For those who don’t know how to patch a file, here it is in full: patched xdg-open to open magnet URLs

Works fine on my Debian using Chrome which properly uses xdg-open to handle URLs. Firefox after version 3.5 became a piece of bloatware and it’s not possible to open magnet links using about:config settings or xdg-open. For Opera just go to Settings->Preferences->Programs and add new protocol with name magnet. At Open with other application, use the path that you saved the patched xdg-open.

Now go download some creative commons licensed files.

*UPDATE*
Patch for latest xdg-utils (since some people complained that previous patch does not apply):



--- xdg-open  2012-02-18 14:22:48.058497027 +0200
+++ xdg-open  2012-02-18 14:24:04.326875223 +0200
@@ -440,6 +440,11 @@
                 exit_success
             fi
         fi
+    elif (echo "$1" | grep -q '^magnet:'); then
+        deluge "$1" 
+        if [ $? -eq 0 ]; then
+            exit_success
+        fi
     fi
 
     IFS=":"

*UPDATE 2*
For Firefox/Iceweasel one can do the following:
go to about:config and right-click. Then click on New->Boolean->network.protocol-handler.expose.magnet -> Value -> false
Upon the next click Firefox/Iceweasel will ask you to choose a program to open magnet links, choose the patched xdg-open posted above.

14 Responses to “open magnet URLs with xdg-open”

  1. January 25th, 2012 | 20:44
    Using Google Chrome Google Chrome 17.0.963.38 on Linux Linux

    Thaaaaaaaanks!

    Πολύ χρήσιμο 🙂

  2. February 8th, 2012 | 09:59
    Using Google Chrome Google Chrome 17.0.963.26 on Ubuntu Linux Ubuntu Linux

    As it stands, using your patch is impossible. Your patching a mult-year outdated version of that script.

  3. Alec
    February 11th, 2012 | 04:43
    Using Mozilla Firefox Mozilla Firefox 10.0 on Linux Linux

    Strange, in Firefox when I click a magnet url, it asks me which application to use 😉

  4. February 18th, 2012 | 14:29
    Using Google Chrome Google Chrome 17.0.963.46 on Linux Linux

    New patch for latest xdg-utils added. It should now apply cleanly.

  5. Vincent
    March 8th, 2012 | 07:37
    Using Google Chrome Google Chrome 17.0.963.56 on Linux Linux

    Also could not use the patch, but i manually added the lines to xdg-open script, and it works like a charm! Thanx!

  6. Barba
    May 28th, 2012 | 00:32
    Using Mozilla Firefox Mozilla Firefox 12.0 on Linux Linux

    It’s funny, I copied your lines and changed deluge for ktorrent, and I got chrome to try to open the magnet link… with Okular!!.
    I’m completely lost in this, as okular is not mentioned at all in the file.
    Any hints?

  7. Barba
    May 30th, 2012 | 21:39
    Using Mozilla Firefox Mozilla Firefox 12.0 on Linux Linux

    Don’t know what caused it, but ir fixed itself.
    For anyone coming after me, I managed to make it work by editing ~/.kde/share/kde4/services/magnet.protocol like this:

    [Protocol]
    exec=ktorrent “%u”
    protocol=magnet
    input=none
    output=none
    helper=true
    listing=false
    reading=false
    writing=false
    makedir=false
    deleting=false

  8. TuX11
    June 6th, 2012 | 08:23
    UsingUnknown browser

    I have enabled magnet urls in iceweasel 10, which is no different to Firefox.
    You just add the the following values to your about:config :
    network.protocol-handler.app.magnet (string; add the path to your torrent client e.g. /usr/bin/vuze)
    network.protocol-handler.handler.external.magnet (boolean; set to true)
    network.protocol-handler.expose.magnet (boolean; set to false)

    Now, without(and that’s the important part) restarting the browser, click on a magnet link. A dialogue will show up and ask for the path to your executable. Set it and you’re done :o)

  9. Armando
    April 7th, 2013 | 04:11
    Using Google Chrome Google Chrome 26.0.1410.43 on Linux Linux

    Thanks so much! Works like a charm.

  10. andy
    July 8th, 2013 | 22:39
    Using Debian IceWeasel Debian IceWeasel 10.0.4 on Linux Linux

    TA’ MUCH!
    Got it working on my side ((near-)stock Debian Sid on this machine), however Transmission (v2.03 here) refused to show the torrent.

    (For the less-savvy users out there: ‘deluge’ is a CLIENT name!! You have to replace it by the client that you’re using, not quote the script/patch word by word!)

    In my case – glad I’m a programmer – the lines to add oin /usr/bin/xdg-open started at about line 624! (search for /OLDIFS/)

    It’s definitely the right place.

    If TM2.03 does not show the links, it’s a bug in the program and I have to update it first.
    Thanks in any case, as the basic problem is now solved.

  11. Anon
    December 13th, 2013 | 04:59
    Using Mozilla Firefox Mozilla Firefox 25.0 on Linux Linux

    People are really following this guide? :/

    You should be using xdg-mime to configure xdg, NOT patching the actual source code! You can find a lot of information about this on the web and on the arch wiki.

  12. December 15th, 2013 | 12:42
    Using Mozilla Firefox Mozilla Firefox 25.0 on Windows Windows 7

    @Anon: There was a time where you could not use xdg-mime because it wasn’t supported by xdg-open, at least on Debian’s Squeeze version of xgd-utils (1.0.2), and that’s when this blog post was written. It’s been almost 2 years since then…

    The needed patches for xgd-open to use xdg-mime were added after version 1.0.2:
    http://cgit.freedesktop.org/xdg/xdg-utils/commit/scripts/xdg-open?id=89254b7c97d7dead6a4d74d095e41143d5bba0c0
    http://cgit.freedesktop.org/xdg/xdg-utils/commit/scripts/xdg-open?id=298184c1cb4e33f6a1abd8d23b9ca2624e1a0991
    http://cgit.freedesktop.org/xdg/xdg-utils/commit/scripts/xdg-open?id=1f56efecaf203d53c77633da1b1078d44ee73ae5

    so version 1.1 should be ok with xdg-mime

  13. Blitz
    February 21st, 2014 | 17:31
    Using Google Chrome Google Chrome 32.0.1700.107 on Linux Linux

    Thank you! This worked like a charm. I added magnet and steam links. Now if only there was an easy tool to add new ones…

  14. Mar
    December 9th, 2014 | 07:52
    Using Google Chrome Google Chrome 39.0.2171.71 on Linux Linux

    Worked fine for Chrome and Vuze (on Xubuntu), thanks!

Leave a reply