2008-01-29 17:31:29 -08:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>postMessage message receiver</title>
|
|
|
|
<script type="application/javascript" src="browserFu.js"></script>
|
|
|
|
<script type="application/javascript">
|
|
|
|
function $(id) { return document.getElementById(id); }
|
|
|
|
|
|
|
|
function setup()
|
|
|
|
{
|
2008-05-02 12:26:47 -07:00
|
|
|
var target = $("domain");
|
|
|
|
target.textContent = location.hostname + ":" + (location.port || 80);
|
2008-01-29 17:31:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function receiveMessage(evt)
|
|
|
|
{
|
|
|
|
var response = evt.data + "-response";
|
|
|
|
|
2008-05-02 12:26:47 -07:00
|
|
|
if (evt.lastEventId !== "")
|
|
|
|
response += " wrong-lastEventId(" + evt.lastEventId + ")";
|
|
|
|
|
2008-01-29 17:31:29 -08:00
|
|
|
if (evt.source !== window.parent)
|
|
|
|
{
|
|
|
|
response += " unexpected-source(" + evt.source + ")";
|
|
|
|
response += " window-parent-is(" + window.parent + ")";
|
|
|
|
response += " location(" + window.location.href + ")";
|
|
|
|
}
|
|
|
|
|
2008-05-02 12:26:47 -07:00
|
|
|
if (isMozilla)
|
|
|
|
{
|
|
|
|
if (evt.isTrusted !== false)
|
|
|
|
response += " unexpected-trusted";
|
|
|
|
}
|
2008-01-29 17:31:29 -08:00
|
|
|
|
|
|
|
if (evt.type != "message")
|
|
|
|
response += " wrong-type(" + evt.type + ")";
|
|
|
|
|
|
|
|
var data = evt.data;
|
|
|
|
if (data == "post-to-other-same-domain")
|
|
|
|
{
|
|
|
|
receiveSame(evt, response);
|
|
|
|
}
|
|
|
|
else if (data == "post-to-other-cross-domain")
|
|
|
|
{
|
|
|
|
receiveCross(evt, response);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response += " unexpected-message-to(" + window.location.href + ")";
|
2010-03-12 13:53:36 -08:00
|
|
|
window.parent.postMessage(response, "http://mochi.test:8888");
|
2008-01-29 17:31:29 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function receiveSame(evt, response)
|
|
|
|
{
|
|
|
|
var source = evt.source;
|
|
|
|
try
|
|
|
|
{
|
2010-03-12 13:53:36 -08:00
|
|
|
if (evt.origin != "http://mochi.test:8888")
|
2008-02-26 19:48:54 -08:00
|
|
|
response += " unexpected-origin(" + evt.origin + ")";
|
2008-01-29 17:31:29 -08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var threw = false;
|
|
|
|
var privateVariable = source.privateVariable;
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
threw = true;
|
|
|
|
}
|
|
|
|
if (threw || privateVariable !== window.parent.privateVariable)
|
|
|
|
response += " accessed-source!!!";
|
|
|
|
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2008-05-02 12:26:47 -07:00
|
|
|
source.postMessage(response, evt.origin);
|
2008-01-29 17:31:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function receiveCross(evt, response)
|
|
|
|
{
|
|
|
|
var source = evt.source;
|
2010-03-12 13:53:36 -08:00
|
|
|
if (evt.origin != "http://mochi.test:8888")
|
2008-02-26 19:48:54 -08:00
|
|
|
response += " unexpected-origin(" + evt.origin + ")";
|
2008-01-29 17:31:29 -08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var threw = false;
|
|
|
|
var privateVariable = source.privateVariable;
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
threw = true;
|
|
|
|
}
|
|
|
|
if (!threw || privateVariable !== undefined)
|
|
|
|
response += " accessed-source!!!";
|
|
|
|
|
2008-05-02 12:26:47 -07:00
|
|
|
source.postMessage(response, evt.origin);
|
2008-01-29 17:31:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("load", setup, false);
|
2008-05-02 12:26:47 -07:00
|
|
|
window.addEventListener("message", receiveMessage, false);
|
2008-01-29 17:31:29 -08:00
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1 id="domain"></h1>
|
|
|
|
</body>
|
|
|
|
</html>
|