google-chrome-extension - chrome 中的自定义协议(protocol)处理程序

标签 google-chrome-extension custom-protocol protocol-handler

如何在 Chrome 中设置自定义协议(protocol)处理程序?像这样的东西:

我的协议(protocol)://测试文件

我需要这个来向 http://example.com?query=testfile 发送请求,然后将 httpresponse 发送到我的分机。

最佳答案

以下方法将应用程序注册到 URI 方案。因此,您可以在 HTML 代码中使用 mycustproto: 来触发本地应用程序。它适用于 Google Chrome 版本 51.0.2704.79 m(64 位)。

我主要使用这种方法来静默打印文档,而不会弹出打印对话框。结果非常好,是将外部应用程序与浏览器集成的无缝解决方案。

HTML 代码(简单):

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

HTML 代码(替代):

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

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

URI 方案如下所示:

您可以在注册表中手动创建 URI 方案,或运行“mycustproto.reg”文件(见下文)。

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 示例:

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# 控制台应用程序 - 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();
    }
  }
}

首先尝试运行程序以确保程序已放置在正确的路径中:

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

单击 HTML 页面上的链接:

您将第一次看到一个警告窗口弹出。

enter image description here

要在 Chrome 中重置外部协议(protocol)处理程序设置:

如果您曾经接受过 Chrome 中的自定义协议(protocol)并希望重置设置,请执行此操作(目前,Chrome 中没有用于更改设置的 UI):

在这个路径下编辑“Local State”这个文件:

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

或只需转到:

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

然后,搜索这个字符串:protocol_handler

您将从那里看到自定义协议(protocol)。

注意:编辑文件前请关闭您的谷歌浏览器。否则,您所做的更改将被 Chrome 覆盖。

引用:

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

关于google-chrome-extension - chrome 中的自定义协议(protocol)处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28750167/

相关文章:

google-chrome-extension - 如何使用 Chrome 的历史 API 来搜索与给定 URL 匹配的访问?

google-chrome - 在安装之前,是否可以检查Google Chrome扩展程序的代码?

standards-compliance - URI方案的有效字符?

Haskell 二进制解析

c# - Visual Studio 协议(protocol)处理程序 - 打开文件

iphone - 以编程方式检索支持给定文件类型的已安装应用程序列表

javascript - Chrome 扩展程序在一段时间后停止工作。响应未定义

protocols - 将 CSP(内容安全策略)与自定义协议(protocol)结合使用

windows - 如何创建自己的 URL 协议(protocol)? (例如 ://. ..)

javascript - DOM功能可在控制台中使用,但不能在扩展中使用,尽管它正在等待加载