24/01/2012
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.
Filed by kargig at 23:39 under Internet,Linux
Tags: bash, chrome, debian, deluge, firefox, iceweasel, Linux, magnet, opera, torrent, xdg
9 Comments | 10,454 views

Thaaaaaaaanks!
Πολύ χρήσιμο
As it stands, using your patch is impossible. Your patching a mult-year outdated version of that script.
Strange, in Firefox when I click a magnet url, it asks me which application to use
New patch for latest xdg-utils added. It should now apply cleanly.
Also could not use the patch, but i manually added the lines to xdg-open script, and it works like a charm! Thanx!
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?
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
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
)
Thanks so much! Works like a charm.