Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

ajax - Is it possible to open custom URL scheme with Google Chrome?

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.

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

Safari launches some registered App. And it is right thing.

Firefox and Opera asks what to do... and I can launch App also.

But Chrome... Doesn't ask.

I even tried to write some HTML page with JavaScript inside to send 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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

[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.

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.

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.

So for right now I am using one function for Chrome/FF and another function for IE.

These links helped me develop this solution:

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)

==================================================

Jake posted a great solution for chrome: https://superuser.com/questions/655405/custom-protocol-handler-not-working-in-chrome-on-ssl-page

This works in chrome only:

 window.location.assign("customprotocol://");

It will fail in an iframe so this is working:

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.

Turns out the solution is to change "://" to ":"

so do this:

src="x-myproto:query"  .....

and the SSL warnings will go away.

==================================================

Follow: (after months of production use)

==================================================

This has been working well for chorme. Detect the browser and if chrome do this:

var w = (window.parent)?window.parent:window
w.location.assign('myproto://xyzabcdefetc')

For IE and other browsers I do something slightly different.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...