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
14 Comments | 70,478 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 :o)
Thanks so much! Works like a charm.
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.
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.
@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
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…
Worked fine for Chrome and Vuze (on Xubuntu), thanks!