我如何在Chrome中设置自定义协议处理程序?例如:
myprotocol://testfile
我需要将此发送到http://example.com?query=testfile,然后将HTTP响应发送到我的扩展程序。
我如何在Chrome中设置自定义协议处理程序?例如:
myprotocol://testfile
我需要将此发送到http://example.com?query=testfile,然后将HTTP响应发送到我的扩展程序。
<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页面上的链接:
您将会看到一个警告窗口第一次弹出。
在Chrome中重置外部协议处理程序设置:
如果您曾经在Chrome中接受过自定义协议并希望重置设置,可以执行以下操作(目前,Chrome中没有UI来更改设置):
编辑此路径下的文件“Local State”:
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
%localappdata%\Google\Chrome\User Data\Default
的 Preferences
文件中找到了 protocol_handler 配置选项。 - BabbloURL Protocol
的值应该是空字符串而不是两个双引号时,你已经转义了引号。 - NetMageChrome 13 现在支持 navigator.registerProtocolHandler
API。例如:
navigator.registerProtocolHandler(
'web+custom', 'http://example.com/rph?q=%s', 'My App');
请注意,您的协议名称必须以web+
开头,但对于一些常见协议(如mailto
等),有少数例外。有关更多详细信息,请参见:http://updates.html5rocks.com/2011/06/Registering-a-custom-protocol-handler
torrent:
和 steam:
链接?Chrome 打包应用程序是否也有此限制? - Steven Roose这个问题现在有些陈旧了,但是最近Chrome进行了更新(至少涉及打包应用程序)...
http://developer.chrome.com/apps/manifest/url_handlers
并且
它允许您为URL注册处理程序(只要您拥有该URL)。不幸的是没有myprotocol://
,但至少您可以使用http://myprotocol.mysite.com
并在那里创建一个网页,指向应用商店中的应用程序。这是我完成它的方法。你的应用程序需要在安装时安装几个reg键,然后在任何浏览器中,您只需链接到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\""
我不确定这是否是发布我的答案的正确地方,但由于我找到了很少有用的线索,而这是其中之一,所以我在此处发布我的解决方案。
问题:我希望在Chromium中单击mailto
链接时,Linux Mint 19.2 Cinnamon可以打开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,并针对我的情况进行了调整。
"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"
打开
C:\Users\<Username>\AppData\Local\Google\Chrome\User Data\Default
打开 偏好设置
,然后搜索 excluded_schemes
,你会在“协议处理程序”中找到它。删除这些被排除的方案,以将 Chrome 重置为使用默认应用程序打开 URL。