Skip to content

Instantly share code, notes, and snippets.

@david-risney
Created March 25, 2016 03:56
Show Gist options
  • Save david-risney/7d6b2da6e5f2fd57eca6 to your computer and use it in GitHub Desktop.
Save david-risney/7d6b2da6e5f2fd57eca6 to your computer and use it in GitHub Desktop.
This code won't run - just trying to get the idea across of how we might replace registerProtocolHandler, but only if register and registee both agree to use the replacement.
Page that wants to register:
<iframe class="hidden" src="http://you.github.io/registerProtocolHandler/" id="registerProtocolHandler"></iframe>
<script>
document.getElementById("registerProtocolHandler").contentWindow.postMessage({
operation: "setRegistration",
scheme: "web+action",
handler: "http://me.com/?url=%s"
}, "http://you.github.io/");
</script>
Page that wants to read a registration:
<iframe class="hidden" src="http://you.github.io/registerProtocolHandler/" id="registerProtocolHandler"></iframe>
<script>
addEventListener("message", function (eventArg) {
// received registration info for 'web+action'
});
document.getElementById("registerProtocolHandler").contentWindow.postMessage({
operation: "getRegistration"
scheme: "web+action"
}, "http://you.github.io/");
</script>
The http://you.github.io/registerProtocolHandler/index.html page:
<script>
addEventListener("message", function (eventArg) {
switch (eventArg.data.operation) {
case "setRegistration":
if (confirm("Let " + eventArg.data.handler + " handle " + eventArg.data.scheme + "?")) {
localStorage["registration"] = localStorage["registration"] || {};
localStorage["registration"][eventArg.data.scheme] = eventArg.data.handler;
}
break;
case "getRegistration":
eventArg.source.postMessage({
scheme: eventArg.data.scheme,
handler: (localStorage["registration"] || {})[eventArg.data.scheme]
}, eventArg.origin);
break;
}
});
</script>
@david-risney
Copy link
Author

Just for my example I have a confirm call to ask the user if they want to allow registration. For the real thing you'd probably want to open a new window and present better, richer UX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment