Search code examples
javascriptbittorrent

How to launch a program/application from javascript just like some sites open torrent program


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?


Solution

  • 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/...)
    enter image description here