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

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

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

myprotocol://testfile

我需要这个来向 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):

在此路径下编辑“本地状态”此文件:

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

或者直接访问:

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

然后,搜索以下字符串:protocol_handler

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

注意:请在编辑文件之前关闭 Google Chrome。否则,您所做的更改将被 Chrome 覆盖。

引用:

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

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

相关文章:

Haskell 二进制解析

c# - 注册协议(protocol)处理程序 Windows 7 - 错误的工作目录

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

javascript - 在 Chrome 扩展程序中使用 PAC 文件重定向 HTTPS 请求似乎失败

javascript - Chrome 扩展保持弹出窗口加载

google-chrome-extension - 使用 Chrome 扩展程序向现有网站添加额外页面

javascript - 如何在 Google Chrome 扩展程序中获取网络请求的结果?

firefox-addon - Firefox WebExtensions 中的自定义协议(protocol)