ajax 是否可以使用 Google Chrome 打开自定义 URL 方案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2330545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to open custom URL scheme with Google Chrome?
提问by UncleMiF
I have protocol (like http) with scheme managed with 3rd party App registered in Mac OS X. I.e, x-someapp://someaction or something like that.
我有协议(如 http),其方案由在 Mac OS XIe、x-someapp://someaction 或类似内容中注册的 3rd 方应用程序管理。
How can I open this URL with Google Chrome? By default, Chrome starts searching in Google engine instead launching App and passing URL handling to it...
如何使用 Google Chrome 打开此 URL?默认情况下,Chrome 开始在 Google 引擎中搜索,而不是启动 App 并将 URL 处理传递给它...
Safari launches some registered App. And it is right thing.
Safari 启动一些已注册的应用程序。这是正确的事情。
Firefox and Opera asks what to do... and I can launch App also.
Firefox 和 Opera 询问该怎么做……我也可以启动应用程序。
But Chrome... Doesn't ask.
但是 Chrome... 不问。
I even tried to write some HTML page with JavaScript inside to send XHttpRequest:
我什至尝试用 JavaScript 编写一些 HTML 页面来发送 XHttpRequest:
function _httpExecuteCallback()
{
if (httpRequestCallbackFunction != null) {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
httpRequestCallbackFunction();
httpRequestCallbackFunction = null;
}
}
}
}
function _httpGet(url, callbackFunction)
{
httpRequest = false;
httpRequestCallbackFunction = callbackFunction;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = _httpExecuteCallback;
httpRequest.open('GET', url, true);
httpRequest.send(null);
}
_httpGet('x-someapp://test',function(){})
No results also...
也没有结果...
采纳答案by UncleMiF
I found the solution that works with Chrome. I use the IFRAME-way.
我找到了适用于 Chrome 的解决方案。我使用 IFRAME 方式。
Example (with JQuery):
示例(使用 JQuery):
$("body").append('<span id="__protoProxy"></span>');
function queryWord(aWord)
{
var protoProxy = document.getElementById('__protoProxy');
if (protoProxy)
{
var word = aWord.replace('"','\"');
protoProxy.innerHTML = '<div style="display:none;"><iframe src="x-myproto://query?' + word + '"></iframe></div>';
}
}
queryWord('hello');
回答by Brian McGinity
The current accepted solution has a problem with Chrome for SSL https. Watching the console log, Chrome blocks the request because it thinks the custom url protocol is not secure:
当前接受的解决方案在 Chrome for SSL https 中存在问题。查看控制台日志,Chrome 阻止了请求,因为它认为自定义 url 协议不安全:
[blocked] The page at reports blah blah ran insecure content from customproto//blah blah
Here is a solution (this took me a few days to research):
这是一个解决方案(我花了几天时间研究):
<input type='button' value='Test Custom Url' onclick='exec()'>
<script>
function submitRequest(buttonId) {
var d = (window.parent)?window.parent.document:window.document
if (d.getElementById(buttonId) == null || d.getElementById(buttonId) == undefined) return;
if (d.getElementById(buttonId).dispatchEvent) {
var e = d.createEvent("MouseEvents");
e.initEvent("click", true, true);
d.getElementById(buttonId).dispatchEvent(e);
}
else {
d.getElementById(buttonId).click();
}
}
function exec(){
var d = (window.parent)?window.parent.document:window.document
var f = d.getElementById('customUrlLink')
if (f ) {f.parentNode.removeChild(f);}
var a = d.createElement('a');
a.href = 'mycustomproto://arg1';
a.innerHTML = "Link"
a.setAttribute('id', 'customUrlLink');
a.setAttribute("style", "display:none; ");
d.body.appendChild(a);
submitRequest("customUrlLink");
}
</script>
This code will not work for IE. I've found using this technique IE limits the argument of the custom protocol to less than 1000 where as using the iFrame technique IE will allow 2083 chars.
此代码不适用于 IE。我发现使用这种技术 IE 将自定义协议的参数限制为小于 1000,而使用 iFrame 技术 IE 将允许 2083 个字符。
The only way to overcome the url limit in javascript is chuck the data and call multiple times. If anyone wants to take a stab at that, please let me know how it goes. I would like to use it.
克服 javascript 中 url 限制的唯一方法是删除数据并多次调用。如果有人想尝试一下,请告诉我进展如何。我想使用它。
To handle long urls in the executing app, pass a token into the app and have it go get the data from a url GET.
要处理正在执行的应用程序中的长 url,请将令牌传递到应用程序并让它从 url GET 获取数据。
So for right now I am using one function for Chrome/FF and another function for IE.
所以现在我使用 Chrome/FF 的一个功能和 IE 的另一个功能。
These links helped me develop this solution:
这些链接帮助我开发了这个解决方案:
https://superuser.com/questions/655405/custom-protocol-handler-not-working-in-chrome-on-ssl-page
https://superuser.com/questions/655405/custom-protocol-handler-not-working-in-chrome-on-ssl-page
Simulating a click in jQuery/JavaScript on a link
(wish I had known this a few days ago....hope this helps someone)
(希望我几天前就知道了……希望这对某人有帮助)
==================================================
==================================================
Update: (8hr later)
更新:(8 小时后)
==================================================
==================================================
Jake posted a great solution for chrome: https://superuser.com/questions/655405/custom-protocol-handler-not-working-in-chrome-on-ssl-page
Jake 为 chrome 发布了一个很好的解决方案:https: //superuser.com/questions/655405/custom-protocol-handler-not-working-in-chrome-on-ssl-page
This works in chrome only:
这仅适用于 chrome:
window.location.assign("customprotocol://");
It will fail in an iframe so this is working:
它会在 iframe 中失败,所以这是有效的:
var w = (window.parent)?window.parent:window
w.location.assign(service + '://' + data)
==================================================
==================================================
Update: (weeks later)
更新:(几周后)
==================================================
==================================================
All of the examples of opening the custom protocol, including my own, have a "://" in the url. And this is what is causing the SSL warnings.
所有打开自定义协议的示例,包括我自己的,在 url 中都有一个“://”。这就是导致 SSL 警告的原因。
Turns out the solution is to change "://" to ":"
原来的解决方案是将“://”更改为“:”
so do this:
所以这样做:
src="x-myproto:query" .....
and the SSL warnings will go away.
并且 SSL 警告将消失。
==================================================
==================================================
Follow: (after months of production use)
遵循:(经过几个月的生产使用)
==================================================
==================================================
This has been working well for chorme. Detect the browser and if chrome do this:
这对 chorme 来说效果很好。检测浏览器,如果 chrome 这样做:
var w = (window.parent)?window.parent:window
w.location.assign('myproto://xyzabcdefetc')
For IE and other browsers I do something slightly different.
对于 IE 和其他浏览器,我做了一些稍微不同的事情。
Note that browsers do impose a limit on how much data you can put in custom url protocol. As long as your string is under 800 chars this seems to be the magic number for which works in all browsers.
请注意,浏览器确实对可以放入自定义 url 协议的数据量施加了限制。只要您的字符串少于 800 个字符,这似乎是适用于所有浏览器的神奇数字。
回答by Mark Judd
It looks like it's Google's locationbar parsing which is getting in the way.
看起来是谷歌的位置栏解析造成了障碍。
The browser, however, does seem to handle custom URL schemes properly. Try this in your locationbar:
但是,浏览器似乎可以正确处理自定义 URL 方案。在您的位置栏中试试这个:
javascript:document.location = 'myscheme://whatever'
Any link on your page that uses the custom scheme should also do the right thing.
您页面上使用自定义方案的任何链接也应该做正确的事情。
回答by st.derrick
Here's a solution that also includes a redirect to the App Store / Play Store if the user doesn't have the app. It uses a setTimeout for this. It also makes use of an iframe to support more browsers. So this works on Chrome, and any other mobile browser. We use this as my company, Branch. Just modify the two links below to correspond to your URI and App Store link.
这是一个解决方案,如果用户没有该应用程序,该解决方案还包括重定向到 App Store / Play Store。它为此使用了 setTimeout。它还利用 iframe 来支持更多浏览器。所以这适用于 Chrome 和任何其他移动浏览器。我们将其用作我的公司Branch。只需修改下面的两个链接以对应您的 URI 和 App Store 链接。
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
window.onload = function() {
// Deep link to your app goes here
document.getElementById("l").src = "my_app://somepath";
setTimeout(function() {
// Link to the App Store should go here -- only fires if deep link fails
window.location = "https://itunes.apple.com/us/app/myapp/id123456789?ls=1&mt=8";
}, 500);
};
</script>
<iframe id="l" width="1" height="1" style="visibility:hidden"></iframe>
</body>
</html>
Again, this should work on any browser, thanks to the iframe.
同样,由于 iframe,这应该适用于任何浏览器。
回答by Pierre-Antoine LaFayette
If Chrome does not recognize the URL scheme, it defaults to a search.
如果 Chrome 无法识别 URL 方案,则默认为搜索。
This is what I see in Safari: alt text http://img62.imageshack.us/img62/6792/clipboard02oh.jpg
这是我在 Safari 中看到的: 替代文本 http://img62.imageshack.us/img62/6792/clipboard02oh.jpg
and in Firefox:
在 Firefox 中:
alt text http://img138.imageshack.us/img138/9986/clipboard04xk.jpg
替代文字 http://img138.imageshack.us/img138/9986/clipboard04xk.jpg
I believe the reason why Chrome defaults to search is that there are special google searches that use the colon.
我相信 Chrome 默认搜索的原因是有使用冒号的特殊谷歌搜索。
E.g:
例如:
- define: dictionary
- filetype:pdf google chromium
- 定义:字典
- 文件类型:pdf 谷歌铬
This is one of the annoyances I have with Firefox, I have to jump to the "search box" rather than the address bar to execute these types of searches. Since Chrome does not have a separate search box like Firefox, IE and Safari have, this functionality is required.
这是我对 Firefox 的烦恼之一,我必须跳转到“搜索框”而不是地址栏来执行这些类型的搜索。由于 Chrome 没有像 Firefox、IE 和 Safari 那样的单独搜索框,因此需要此功能。
Ajax requests won't get you around this.
Ajax 请求不会让您解决这个问题。

