Created
April 30, 2010 11:32
-
-
Save tav/385083 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var centralLocation = "http://central.com/~tav/central.html", | |
centralChannel, | |
messageId = 0, | |
messageQueue = [], | |
callbackRegistry = {}, | |
firstContact = false; | |
function createCentralChannel() { | |
if (centralChannel) { | |
return; | |
} | |
centralChannel = document.createElement('iframe'); | |
document.body.appendChild(centralChannel); | |
centralChannel.src = centralLocation; | |
} | |
function createChannel(dest) { | |
var channel = document.createElement('iframe'); | |
document.body.appendChild(channel); | |
channel.src = dest; | |
} | |
var identity = { | |
getProvider: function(callback) { | |
sendMessage({"do": "getProvider"}, callback); | |
}, | |
setProvider: function(callback) { | |
sendMessage({"do": "setProvider"}, callback); | |
} | |
}; | |
if (window.location.host === 'central.com') { | |
var onMessage = function (event) { | |
var msg = JSON.parse(event.data); | |
var cmd = msg['do']; | |
if (!cmd) { | |
return; | |
} else if (cmd === "setProvider") { | |
var currentProvider = localStorage['provider']; | |
if (!currentProvider) { | |
alert("Setting provider to " + event.origin); | |
localStorage['provider'] = event.origin; | |
} else if (currentProvider !== event.origin) { | |
alert('Boo!!'); | |
} | |
} else if (cmd === "getProvider") { | |
sendMessage(msg.id, localStorage['provider'], event.origin); | |
} | |
}; | |
var sendMessage = function (id, msg, scope) { | |
if (window.parent) { | |
msg = JSON.stringify({'id': id, 'data': msg}); | |
window.parent.postMessage(msg, scope); | |
} | |
}; | |
$(function () { | |
sendMessage('y0!', "we're alive!", "*"); | |
}); | |
} else { | |
var sendMessage = function (msg, callback) { | |
createCentralChannel(); | |
var id = msg['id'] = messageId++; | |
msg = JSON.stringify(msg); | |
if (callback) { | |
callbackRegistry[id] = callback; | |
} | |
if (!firstContact) { | |
messageQueue.push(msg); | |
} else { | |
centralChannel.contentWindow.postMessage(msg, centralLocation); | |
} | |
}; | |
var onMessage = function (event) { | |
var msg = JSON.parse(event.data); | |
var id = msg.id; | |
var data = msg.data; | |
// alert("incoming " + data); | |
if (id === "confirm") { | |
} | |
firstContact = true; | |
var callback = callbackRegistry[id]; | |
if (callback) { | |
callback(data); | |
} | |
while (messageQueue.length) { | |
centralChannel.contentWindow.postMessage(messageQueue.shift(), centralLocation); | |
} | |
}; | |
} | |
if (typeof window.addEventListener !== 'undefined') { | |
window.addEventListener('message', onMessage, false); | |
} else if (typeof window.attachEvent !== 'undefined') { | |
window.attachEvent('onmessage', onMessage); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment