Google chrome extension chrome中的自定义协议处理程序

Google chrome extension chrome中的自定义协议处理程序,google-chrome-extension,custom-protocol,Google Chrome Extension,Custom Protocol,如何在chrome中设置自定义协议处理程序?比如: myprotocol://testfile 我需要它向发送请求,然后将httpresponse发送到我的分机。Chrome 13现在支持navigator.registerProtocolHandlerAPI。比如说, navigator.registerProtocolHandler( 'web+custom', 'http://example.com/rph?q=%s', 'My App'); 请注意,您的协议名称必须以web+开

如何在chrome中设置自定义协议处理程序?比如:

myprotocol://testfile


我需要它向发送请求,然后将httpresponse发送到我的分机。

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

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

请注意,您的协议名称必须以
web+
开头,常见的协议名称除外(如
mailto
等)。有关更多详细信息,请参阅:

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

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

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


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


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

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

我主要使用这种方法在不弹出打印对话框的情况下以静默方式打印文档。结果非常好,是集成外部应用程序和浏览器的无缝解决方案

HTML代码(简单):

<a href="mycustproto:Hello World">Click Me</a>
<input id="DealerName" />
<button id="PrintBtn"></button>

$('#PrintBtn').on('click', function(event){
  event.preventDefault();
  window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});
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\""
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"
mycustproto.reg示例:

<a href="mycustproto:Hello World">Click Me</a>
<input id="DealerName" />
<button id="PrintBtn"></button>

$('#PrintBtn').on('click', function(event){
  event.preventDefault();
  window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});
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\""
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"
C#控制台应用程序-myprogram.exe:

<a href="mycustproto:Hello World">Click Me</a>
<input id="DealerName" />
<button id="PrintBtn"></button>

$('#PrintBtn').on('click', function(event){
  event.preventDefault();
  window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});
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\""
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"
首先尝试运行程序,以确保程序已放置在正确的路径中:

<a href="mycustproto:Hello World">Click Me</a>
<input id="DealerName" />
<button id="PrintBtn"></button>

$('#PrintBtn').on('click', function(event){
  event.preventDefault();
  window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});
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\""
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页面上的链接:

<a href="mycustproto:Hello World">Click Me</a>
<input id="DealerName" />
<button id="PrintBtn"></button>

$('#PrintBtn').on('click', function(event){
  event.preventDefault();
  window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});
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\""
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"
您将第一次看到一个警告窗口弹出

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

<a href="mycustproto:Hello World">Click Me</a>
<input id="DealerName" />
<button id="PrintBtn"></button>

$('#PrintBtn').on('click', function(event){
  event.preventDefault();
  window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});
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\""
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"
如果您曾经在Chrome中接受过自定义协议,并希望重置设置,请执行此操作(目前,Chrome中没有可更改设置的UI):

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

C:\Users\Username\AppData\Local\Google\Chrome\User Data\
或者直接转到:

%USERPROFILE%\AppData\Local\Google\Chrome\User Data\
然后,搜索此字符串:协议\u处理程序

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

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

参考资料:

<a href="mycustproto:Hello World">Click Me</a>
<input id="DealerName" />
<button id="PrintBtn"></button>

$('#PrintBtn').on('click', function(event){
  event.preventDefault();
  window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});
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\""
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"
打开

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

打开
首选项
然后搜索
排除的\u方案
您将在“协议处理程序”中找到它,删除此排除的方案以使用默认应用程序将chrome重置为打开url

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

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

解决方案: 使用控制台中的xdg设置

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

解决方案是在这里找到的,并根据我的情况进行了调整。

我发现谢军和马芬曼的解决方案通常在点击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"
为了进行比较,将http或https URL与这些命令中的任何一个一起提供给Chrome会导致打开网页

这一点很明显,因为我们的一位客户报告说,当Chrome是默认浏览器时,从Adobe Reader中显示的PDF中单击我们产品的链接无法调用我们的产品。(默认情况下MSIE和Firefox可以正常工作,但当Chrome或Edge都是默认值时就不行了。)

我猜Adobe产品不是告诉Windows调用URL并让Windows解决问题,而是找到默认浏览器,在本例中是Chrome,然后在命令行上传递URL

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


我已经在Chrome 88.0.4324.182上对此进行了测试。

我认为Chrome目前不支持这一点,它有一套相当有限的API可用于扩展。+1因为协议必须从web+开始,以避免“安全性错误:DOM异常18”
torrent:
steam:
链接如何工作?Chrome打包的应用程序也有这样的限制吗?@StevenRoose从我可以看出,Chrome会将这些限制传递给操作系统。即使使用
chrome.webNavigation
API,
tel:
链接也不会被报告。这在chrome应用程序中似乎不起作用,并且似乎无法重定向应用程序中的协议。更好的答案是下面的Gordon Williams答案。这在chrome中不起作用,这是一个特定的问题。@KjetilWatnedal:它在chrome中(至少在v40+)和此解决方案中效果很好。关于最初的问题,OP想要t