Reputation: 2663
I know that javascript cannot launch/open/run programs/applications on a client machine. But still I want to know how to open a program. I know that the browser's sandbox will not allow javascript to do such things and even javascript specification does not specifies in its spec to have such kind of features supported by javascript.
Now I would like to ask how some sites are able to launch the torrent program installed in my machine? Ususally when I click on magnet torrent, the browser will show me a dialog box whether or not to launch the application. How are these sites such as piratebay etc able to launch torrent program?
Upvotes: 1
Views: 2311
Reputation: 7342
You have to understand that a web page cannot (in theory) launch a program on the user machine without user interactions (for obvious security reasons).
When you click on a web link (for example starting with http:// or https://), your browser knows how to deal with it and directly opens the concerned page.
When you click on a link which starts with a specific protocol (like callto:// or magnet://), the browser doesn't know how to deal with it (at least the first time) and so asks the user what to do. It generally displays a list of compatible programs.
The Pirate Bay is simply using the magnet protocol to inform your browser that the link contains information which can be exploited by a program supporting this protocol.
You can simulate a user interaction on custom protocols with JavaScript. For example to start a Skype call: window.location.href = 'callto://helloworld'
. The same principle can be applied to launch a torrent download (through a magnet link).
Upvotes: 4
Reputation: 1971
You are probably talking about the magnet: protocol.
To manually define a new protocol and bind a program to it all you need is create a simple registry key.
Check out this C# snippet I use to install a company internal protocol handler.
RegistryKey rk;
rk = Registry.ClassesRoot.CreateSubKey("NAMEOFPROTOCOL");
rk.SetValue("", "URL:NAMEOFPROTOCOL protocol"); // "" = (Standard)
rk.SetValue("URL Protocol", "");
rk = rk.CreateSubKey("shell");
rk = rk.CreateSubKey("open");
rk = rk.CreateSubKey("command");
rk.SetValue("", "FILEPATH" + @"""%1"""); // gives the XXX of protocol:XXX to the called program as first argument
Registry classes are in the namespace "Microsoft.Win32".
This code needs administrator privilege to run.
Check out the following registry entries, my default Torrent client is Deluge and this is how it looks in the registry: (HKEY_CLASSES_ROOT/magnet/...)
Upvotes: 3