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.
The Protocol Handler Icon Your first step is to start Chrome on your computer, open Gmail, then look up at your top address bar. Look for the Protocol Handler icon at the top right. This looks like a gray-colored diamond that's to the left of a star, with the latter being used for bookmarking.
Your Default Mail Handler is the email program that is launched when you click on an email link on a Web page or email message. You can make Google Chrome your default mail handler by doing one of the following steps: 1. Open the Google Chrome Browser on your computer. 2.
A protocol handler is an application that knows how to handle particular types of links: for example, a mail client is a protocol handler for "mailto:" links.
To use it, you first need to enable it: open the DevTools by right-click any page and choosing “Inspect Element". Go to the network tab, right-click the columns in the and enable the “Protocol” column. Once enabled, refresh the page and it'll show you what protocols each resource are using.
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.
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
Chrome 13 now supports the navigator.registerProtocolHandler
API. For example,
navigator.registerProtocolHandler( 'web+custom', 'http://example.com/rph?q=%s', 'My App');
Note that your protocol name has to start with web+
, with a few exceptions for common ones (like mailto
, etc). For more details, see: http://updates.html5rocks.com/2011/06/Registering-a-custom-protocol-handler
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With