如何在 Windows 中的自定义协议处理程序中转义 & 符号

How to escape & ampersand in Custom protocol handler in Windows

我按照此 link. The case is I need to open a link which can only be opened in IE and might contains several query parameters and should be opened in IE from our web app which is running on Chrome (this is really annoying). After many tries and fails, I managed to find the 创建了一个自定义协议处理程序,以将条目添加到 windows 注册表配置单元并创建 .reg 文件和 运行:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\ie]
"URL Protocol"="\"\""
@="\"URL:IE Protocol\""
[HKEY_CURRENT_USER\Software\Classes\ie\DefaultIcon]
@="\"explorer.exe,1\""
[HKEY_CURRENT_USER\Software\Classes\ie\shell]
[HKEY_CURRENT_USER\Software\Classes\ie\shell\open]
[HKEY_CURRENT_USER\Software\Classes\ie\shell\open\command]
@="cmd /k set myvar=%1 & call set myvar=%%myvar:ie:=%% & call \"C:\Program Files (x86)\Internet Explorer\iexplore.exe\" %%myvar%% & exit /B"

它有效,但问题是如果 link 包含超过 1 个查询参数,除了第一个之外的所有参数都被省略,我确信这是因为 windows 命令行中的字符编码:

e.g. some_example_url?query1=value1&query2=value2&query3=value3 is becoming some_example_url?query1=value1

我找到这个 solution 但它也不起作用。如何正确转义 & 字符,以便可以使用所有查询参数在 IE 上打开它。 (如前所述,link 由网络应用 运行ning 在 Chrome 上触发)

编辑:触发的代码:

fileClicked(url) {
  // url should be escaped with ^
  const escapedUrl = url.replace(/&/gi, '^&');
  const ie = document.createElement('a');
  // ie: scheme => custom protocol handler
  // host computer (windows) should add custom protocol to windows registry
  ie.href = `ie:${escapedUrl}`;
  ie.click();
}

编辑 2 @muzafako 修复了脚本,最后一行应该像下面这样替换:

@="cmd /c set url=\"%1\" & call set url=%%url:ie:=%% & call start iexplore -nosessionmerging -noframemerging %%url%%"

您根本不需要解码查询参数。我试图找到这个问题的解决方案并看到了这个 answer。它对我有用。只需将命令行更改为:

@="cmd /c set url=\"%1\" & call set url=%%url:ie:=%% & call start iexplore -nosessionmerging -noframemerging %%url%%"