Chrome 中的自定义协议处理程序

发布于 2024-11-30 05:41:51 字数 211 浏览 5 评论 0原文

如何在 Chrome 中设置自定义协议处理程序?类似于:

myprotocol://testfile

我需要它来发送请求到 http://example.com?query= testfile,然后将 httpresponse 发送到我的扩展。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

十年九夏 2024-12-07 05:41:52

以下方法将应用程序注册到 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();
    }
  }
}

首先尝试运行该程序,以确保该程序已放置在正确的路径中:< /strong>

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

点击 HTML 页面上的链接:

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

在此处输入图像描述

要重置 Chrome 中的外部协议处理程序设置:

如果您曾经接受 Chrome 中的自定义协议并且想要重置该设置,请执行此操作(目前 Chrome 中没有可更改的 UI)设置):

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

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

或只需转到:

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

然后,搜索此字符串:protocol_handler

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

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

参考:

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

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

温暖的光 2024-12-07 05:41:52

Chrome 13 现在支持 navigator.registerProtocolHandler API。例如,

navigator.registerProtocolHandler(
    'web+custom', 'http://example.com/rph?q=%s', 'My App');

请注意,您的协议名称必须以 web+ 开头,但常见协议有一些例外(例如 mailto 等)。有关更多详细信息,请参阅:http://updates.html5rocks.com/2011/06/注册自定义协议处理程序

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

衣神在巴黎 2024-12-07 05:41:52

这个问题现在已经很老了,但是 Chrome 最近有更新(至少在打包应用程序方面)...

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

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

它允许您注册 URL 的处理程序(如只要你拥有它)。遗憾的是没有 myprotocol:// 但至少您可以 http://myprotocol.mysite.com 并可以在那里创建一个网页,将人们引导到应用程序中的应用程序店铺。

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.

心清如水 2024-12-07 05:41:52

我就是这样做的。您的应用程序需要在安装时安装一些注册表项,然后在任何浏览器中您只需链接到 foo:\anythingHere.txt ,它将打开您的应用程序并传递该值。

这不是我的代码,只是我在搜索同一问题时在网上找到的代码。只需将下面文本中的所有“foo”更改为您想要的协议名称,并更改 exe 的路径即可。

(将其放入文本文件中,在桌面上另存为 foo.reg,然后双击它来安装密钥)
-----此行下方进入.reg 文件(不包括此行)-----

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\"" 

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\"" 
淡莣 2024-12-07 05:41:52

不确定这是否是我的答案的正确位置,但由于我发现很少有有用的线程,而这就是其中之一,所以我在这里发布我的解决方案。

问题:我希望 Linux Mint 19.2 Cinnamon 在单击 Chromium 中的 mailto 链接时打开 Evolution。 Gmail 已在 chrome://settings/handlers 中注册为默认处理程序,我无法选择任何其他处理程序。

解决方案
在控制台中使用 xdg-settings

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

解决方案在这里找到 https://alt.os.linux.ubuntu.narkive.com/U3Gy7inF/kubuntu-mailto-links-in-chrome-doesn-t-open-evolution 并适合我的情况。

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.

带刺的爱情 2024-12-07 05:41:52

我发现 Jun Hsieh 和 MuffinMan 提出的解决方案通常在单击 Chrome 页面上的链接或粘贴到 URL 栏中时有效,但它似乎在特定情况下不起作用在命令行上传递字符串。

例如,以下两个命令都会打开一个空白的 Chrome 窗口,然后该窗口不会执行任何操作。

"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"

为了进行比较,使用这些命令中的任何一个向 Chrome 提供 http 或 https URL 都会导致打开网页。

这变得很明显,因为我们的一位客户报告说,当 Chrome 是默认浏览器时,单击 Adob​​e Reader 中显示的 PDF 中的产品链接无法调用我们的产品。 (默认情况下,它在 MSIE 和 Firefox 中工作正常,但在 Chrome 或 Edge 为默认情况下则不然。)

我猜测,Adobe 产品不是只是告诉 Windows 调用 URL 并让 Windows 自己解决问题,而是寻找默认值浏览器(本例中为 Chrome),然后在命令行上传递 URL。

我很感兴趣是否有人知道 Chrome 安全性或其他可能与此处相关的设置,以便 Chrome 能够完全处理协议处理程序,即使它是通过命令行提供的。我一直在寻找,但到目前为止还没有找到任何东西。

我一直在针对 Chrome 88.0.4324.182 进行测试。

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.

厌倦 2024-12-07 05:41:52

打开

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

Preferences 然后搜索 excluded_schemes 您将在“protocol_handler”中找到它删除此排除的方案以重置 chrome 以使用默认应用程序打开网址

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文