Custom protocol handler in chrome

custom-protocolgoogle-chrome-extension

How do i set up a custom protocol handler in chrome? Something like:

myprotocol://testfile

I would need this to send a request to http://example.com?query=testfile, then send the httpresponse to my extension.

Best Answer

The following method registers an application to a URI Scheme. So, you can use mycustproto: in your HTML code to trigger a local application. It works on a Google Chrome Version 51.0.2704.79 m (64-bit).

I mainly used this method for printing document silently without the print dialog popping up. The result is pretty good and is a seamless solution to integrate the external application with the browser.

HTML code (simple):

<a href="mycustproto:Hello World">Click Me</a>

HTML code (alternative):

<input id="DealerName" />
<button id="PrintBtn"></button>

$('#PrintBtn').on('click', function(event){
  event.preventDefault();
  window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});

URI Scheme will look like this:

You can create the URI Scheme manually in registry, or run the "mycustproto.reg" file (see below).

HKEY_CURRENT_USER\Software\Classes
   mycustproto
      (Default) = "URL:MyCustProto Protocol"
      URL Protocol = ""
      DefaultIcon
         (Default) = "myprogram.exe,1"
      shell
         open
            command
               (Default) = "C:\Program Files\MyProgram\myprogram.exe" "%1"

mycustproto.reg example:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\mycustproto]
"URL Protocol"="\"\""
@="\"URL:MyCustProto Protocol\""

[HKEY_CURRENT_USER\Software\Classes\mycustproto\DefaultIcon]
@="\"mycustproto.exe,1\""

[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell]

[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell\open]

[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell\open\command]
@="\"C:\\Program Files\\MyProgram\\myprogram.exe\" \"%1\""

C# console application - myprogram.exe:

using System;
using System.Collections.Generic;
using System.Text;

namespace myprogram
{
  class Program
  {
    static string ProcessInput(string s)
    {
       // TODO Verify and validate the input 
       // string as appropriate for your application.
       return s;
    }

    static void Main(string[] args)
    {
      Console.WriteLine("Raw command-line: \n\t" + Environment.CommandLine);
      Console.WriteLine("\n\nArguments:\n");

      foreach (string s in args)
      {
        Console.WriteLine("\t" + ProcessInput(s));
      }

      Console.WriteLine("\nPress any key to continue...");
      Console.ReadKey();
    }
  }
}

Try to run the program first to make sure the program has been placed in the correct path:

cmd> "C:\Program Files\MyProgram\myprogram.exe" "mycustproto:Hello World"

Click the link on your HTML page:

You will see a warning window popup for the first time.

enter image description here

To reset the external protocol handler setting in Chrome:

If you have ever accepted the custom protocol in Chrome and would like to reset the setting, do this (currently, there is no UI in Chrome to change the setting):

Edit "Local State" this file under this path:

C:\Users\Username\AppData\Local\Google\Chrome\User Data\

or Simply go to:

%USERPROFILE%\AppData\Local\Google\Chrome\User Data\

Then, search for this string: protocol_handler

You will see the custom protocol from there.

Note: Please close your Google Chrome before editing the file. Otherwise, the change you have made will be overwritten by Chrome.

Reference:

https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx

Related Topic