Custom protocol handler in chrome
Asked Answered
M

7

100

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.

Mongrel answered 17/8, 2011 at 3:48 Comment(1)
I don't think that Chrome currently supports this, it has a rather limited set of APIs available to extensions.Literally
C
94

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

Cassella answered 2/6, 2016 at 20:46 Comment(7)
this looks promising, can i use this approach to print from a tablet. if so how to deal with registry etc in that caseChordate
@Chordate I think it's better to specify the tablet name and the operating system first (ex: Surface, iPad, etc) and see if others have done that before. The solution I've posted was for Windows. I have seen the similar settings for OS X and Linux. However, I did not have these device/environment handy to experiment. You can try it on Windows first to get the basic idea of how it works. I hope this will help.Cassella
For the record, I founded the protocol_handler config option in Preferences file, located at %localappdata%\Google\Chrome\User Data\DefaultEpicritic
Unable to invoke multiple protocol one after the other in a single javascript call within chrome.Leucite
What if my key is not found in registry? From browser I am asking to call the application based on the key? here, how can I notify to user that the key is not found in registry?Jackinthepulpit
You mentioned that you use this to print files, but I'm not seeing any print functionality in the example you provided. Do you have more documentation?Clockmaker
I don't believe your sample .reg file is correct: you have escaped quotes for e.g. the value of URL Protocol when it should be an empty string, not two double quotes.Lehrer
I
55

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

Inman answered 18/8, 2011 at 0:54 Comment(5)
+1 for the fact that protocols must start with web+ to avoid "SECURITY_ERR: DOM Exception 18"Glialentn
How to torrent: and steam: links work then? Do Chrome Packaged Apps also have this restriction?Automaton
@StevenRoose From what I can tell, Chrome passes those on to the OS. Even using the chrome.webNavigation API, tel: links are never reported.Gingery
This doesn't seem to work inside a chrome app, and doesn't seem to be able to redirect the protocol inside the app. Much better answer is Gordon Williams answer below.Attentive
developer.mozilla.org/en-US/docs/Web/API/Navigator/…Festination
M
33

This question is old now, but there's been a recent update to Chrome (at least where packaged apps are concerned)...

http://developer.chrome.com/apps/manifest/url_handlers

and

https://github.com/GoogleChrome/chrome-extensions-samples/blob/e716678b67fd30a5876a552b9665e9f847d6d84b/apps/samples/url-handler/README.md

It allows you to register a handler for a URL (as long as you own it). Sadly no myprotocol:// but at least you can do http://myprotocol.mysite.com and can create a webpage there that points people to the app in the app store.

Mccay answered 25/3, 2014 at 16:40 Comment(1)
For those reading this many years later, this might be relevant: web.dev/pwa-url-handler -- You'll notice support for this is being phased out. This might be a better option: github.com/WICG/pwa-url-handler/blob/main/handle_links/…Sheepcote
S
12

This is how I did it. Your app would need to install a few reg keys on installation, then in any browser you can just link to foo:\anythingHere.txt and it will open your app and pass it that value.

This is not my code, just something I found on the web when searching the same question. Just change all "foo" in the text below to the protocol name you want and change the path to your exe as well.

(put this in to a text file as save as foo.reg on your desktop, then double click it to install the keys) -----Below this line goes into the .reg file (NOT including this line)------

REGEDIT4

[HKEY_CLASSES_ROOT\foo]
@="URL:foo Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\foo\shell]

[HKEY_CLASSES_ROOT\foo\shell\open]

[HKEY_CLASSES_ROOT\foo\shell\open\command]
@="\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\" \"%1\"" 
Stortz answered 21/2, 2014 at 17:12 Comment(7)
This does not work in chrome, which was the specific question.Zacharie
@KjetilWatnedal: It works fine in Chrome (at least in v40+) and with this solution. Related to the initial question, OP wanted the browser to submit something to a site automatically, apparently without a local handler application, which indeed may not be possible.Bigname
I want to let everyone know his solution does, in fact, work in Chrome (tested in 49.0.2623.87 today). However, I did find that if you type this protocol directly in the browser (foo://C:/test.txt) you might have to use ctrl+enter because the browser doesn't immediately recognized the protocol and the search protocol intercepts the action before the OS can. Putting it in a link worked correctly, though.Shontashoo
@Shontashoo - thank-you so much! I've been tearing my hair out trying to get protocol handlers to work in chrome - turns out all I needed to do was use Ctrl+Enter! Thanks again!Neighbors
@Shontashoo thank you so much, I already thought my users would have to stick to Internet ExplorerGoldsworthy
I had no success at all in Chrome until I called the custom protocol from an https page. Then all the above worked perfectly.Towel
can anyone show an example of the reg file with actual chrome.exe opening a specific website and specific page on that site that will deal with the opened protocol page?Calan
L
2

Not sure whether this is the right place for my answer, but as I found very few helpful threads and this was one of them, I am posting my solution here.

Problem: I wanted Linux Mint 19.2 Cinnamon to open Evolution when clicking on mailto links in Chromium. Gmail was registered as default handler in chrome://settings/handlers and I could not choose any other handler.

Solution: Use the xdg-settings in the console

xdg-settings set default-url-scheme-handler mailto org.gnome.Evolution.desktop

Solution was found here https://alt.os.linux.ubuntu.narkive.com/U3Gy7inF/kubuntu-mailto-links-in-chrome-doesn-t-open-evolution and adapted for my case.

Lolland answered 18/12, 2019 at 10:25 Comment(0)
D
1

I've found the solution by Jun Hsieh and MuffinMan generally works when it comes to clicking links on pages in Chrome or pasting into the URL bar, but it doesn't seem to work in a specific case of passing the string on the command line.

For example, both of the following commands open a blank Chrome window which then does nothing.

"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "foo://C:/test.txt"
"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --new-window "foo://C:/test.txt"

For comparison, feeding Chrome an http or https URL with either of these commands causes the web page to be opened.

This became apparent because one of our customers reported that clicking links for our product from a PDF being displayed within Adobe Reader fails to invoke our product when Chrome is the default browser. (It works fine with MSIE and Firefox as default, but not when either Chrome or Edge are default.)

I'm guessing that instead of just telling Windows to invoke the URL and letting Windows figure things out, the Adobe product is finding the default browser, which is Chrome in this case, and then passing the URL on the command line.

I'd be interested if anyone knows of Chrome security or other settings which might be relevant here so that Chrome will fully handle a protocol handler, even if it's provided via the command line. I've been looking but so far haven't found anything.

I've been testing this against Chrome 88.0.4324.182.

Demonstrator answered 17/2, 2021 at 4:17 Comment(0)
H
-1

open

C:\Users\<Username>\AppData\Local\Google\Chrome\User Data\Default

open Preferences then search for excluded_schemes you will find it in 'protocol_handler' delete this excluded scheme(s) to reset chrome to open url with default application

Hewitt answered 25/1, 2019 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.