Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
190 lines (167 sloc) 6.12 KB
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Proxy File for YUI CrossFrame Utility</title>
<script type="text/javascript">
/*global location, window, top */
/*jslint evil:true */
/*
* This file is for the browsers which does not implement HTML 5 web messaging.
* Currently (2010-10) that would be Internet Explorer 6 and 7.
*
* You need to place this file under same domain that message posts to.
* For example, you want to send message from a.com to iframe of b.com, you need to place this in b.com.
* I recommend that you should put this file at both domains because communication is usually
* not just one way.
*
* @author Joseph Chiang <josephj6802@gmail.com>
* @created 2010-10-07
*/
(function () {
var _data,
targetWindow,
//========================
// Constants
//========================
MODULE_ID = "CrossFrame",
PATTERN = /^(?:(?:(top|parent|opener|frames\[(?:(?:['"][a-zA-Z\d-_]*['"])|\d+)\]))(?:\.|$))+/,
//========================
// Private Methods
//========================
_parseQueryString;
//========================
// Private Methods
//========================
/*
* Parsing provided location.hash string to object literal
*
* @method parseQueryString
* @param hash {String} URL fragment identifier
* @return {Object} hash information in object literal format
*/
_parseQueryString = function (hash) {
var data,
i,
j,
items,
item;
data = {};
items = hash.split("&");
for (i = 0, j = items.length; i < j; i = i + 1) {
item = items[i].split("=");
if (item.length === 2 && item[0].length > 0) {
data[item[0]] = decodeURIComponent(item[1]);
}
}
return data;
};
//========================
// Execution
//========================
// Parsing current URL fragment identifier to object literal
_data = _parseQueryString(location.hash.substr(1));
// Only execution when required parameter exist.
if (
!_data.hasOwnProperty("tid") ||
!_data.hasOwnProperty("message") ||
!_data.hasOwnProperty("target")
) {
return;
}
// Check if target is a valid window name.
if (!PATTERN.test(_data.target) && !eval("parent." + _data.target)) {
throw new Error("Invalid target: " + _data.target);
}
// Get window object.
targetWindow = eval("parent." + _data.target);
// Check if target window has YUI.
if (!targetWindow.YUI) {
throw new Error("YUI doesn't exist in target page.");
}
// FIXME: IE8 using opener stops execution here with unknown reason.
targetWindow.YUI().use("event-custom", "crossframe", function (Y) {
// Add window.opener hack support.
if (!_data.useProxy) {
if (typeof targetWindow.opener !== "undefined" && typeof targetWindow.opener.messageCallbackAdapter !== "undefined") {
targetWindow.opener.messageReceiveAdapter = Y.CrossFrame.messageReceiveAdapter;
}
}
var evt = {},
callback = null,
publisher = null,
tid = _data.tid,
message = _data.message,
eventType = _data.eventType,
target = _data.target,
url = _data.url,
origin = _data.origin,
source = _data.source,
ports = _data.ports,
reverseProxy = _data.reverseProxy
useProxy = _data.useProxy;
// Check if target window has CrossFrame utility.
if (!Y.CrossFrame) {
throw new Error("Y.CrossFrame doesn't exist.");
}
// Receive confirmation message, executing prepared onSuccess function
// when receive succssful callback message.
if (decodeURIComponent(message) === Y.CrossFrame.SUCCESS_MESSAGE) {
try {
targetWindow[tid](_data);
} catch (e) {
Y.log(e.message, "error", MODULE_ID);
}
return;
}
// Reproduce event object.
// Make it very similar with onmessage event object.
evt = {
"type" : eventType,
"data" : _data,
"origin" : null, // TODO
"lastEventId" : null,
"source" : source,
"ports" : ports
};
callback = function (o) {
if (!reverseProxy) {
callback = null;
return;
}
var i,
query;
// Change attribute object to query string.
query = [];
for (i in o) {
if (o.hasOwnProperty(i)) {
// Avoid overwrite.
if (i === "url" || i === "target" || i === "tid" || i === "message") {
continue;
}
o[i] = o[i].toString();
query.push(i + "=" + encodeURIComponent(o[i]));
}
}
// Compose information to query string.
target = source || "parent";
url = [
"url=" + encodeURIComponent(url),
"target=" + encodeURIComponent(target),
"tid=" + tid,
"message=" + encodeURIComponent(Y.CrossFrame.SUCCESS_MESSAGE)
].join("&");
Y.CrossFrame.appendFrame(reverseProxy + "#" + url + "&" + query.join("&"));
};
// Handle specific message by using Y.on("crossframe:<label>").
if (eventType) {
publisher = Y.CrossFrame.addPublisher(eventType);
publisher.fire(eventType, evt, _data, callback);
}
// Use Y.Global.on("crossframe:message") to handle all messages.
Y.CrossFrame.messageReceiveEvent.fire("crossframe:message", evt, _data, callback);
});
}());
</script>
</head>
</html>