Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

Custom protocol handler in chrome

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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_USERSoftwareClasses
   mycustproto
      (Default) = "URL:MyCustProto Protocol"
      URL Protocol = ""
      DefaultIcon
         (Default) = "myprogram.exe,1"
      shell
         open
            command
               (Default) = "C:Program FilesMyProgrammyprogram.exe" "%1"

mycustproto.reg example:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USERSoftwareClassesmycustproto]
"URL Protocol"=""""
@=""URL:MyCustProto Protocol""

[HKEY_CURRENT_USERSoftwareClassesmycustprotoDefaultIcon]
@=""mycustproto.exe,1""

[HKEY_CURRENT_USERSoftwareClassesmycustprotoshell]

[HKEY_CURRENT_USERSoftwareClassesmycustprotoshellopen]

[HKEY_CURRENT_USERSoftwareClassesmycustprotoshellopencommand]
@=""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: 
" + Environment.CommandLine);
      Console.WriteLine("

Arguments:
");

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

      Console.WriteLine("
Press 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 FilesMyProgrammyprogram.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:UsersUsernameAppDataLocalGoogleChromeUser Data

or Simply go to:

%USERPROFILE%AppDataLocalGoogleChromeUser 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...