-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
275 additions
and
563 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,198 +1,109 @@ | ||
define("arale/messenger/1.0.2/messenger-debug", [ "$-debug" ], function(require, exports, module) { | ||
var $ = require("$-debug"); | ||
function Messenger(config) { | ||
var win = config.target || parent; | ||
try { | ||
if (!$.isWindow(win)) { | ||
win = $(win).get(0); | ||
if (win && win.tagName === "IFRAME") { | ||
win = win.contentWindow; | ||
} | ||
} | ||
} catch (e) {} | ||
// save the pointer to the window which is interacting with | ||
this.win = win; | ||
this.onmessage = config.onmessage || function() {}; | ||
this.init(); | ||
} | ||
// postMessage API is supported | ||
Messenger.prototype.init = function() { | ||
var self = this; | ||
this._receiver = function(event) { | ||
// Some IE-component browsers fails if you compare | ||
// window objects with '===' or '!=='. | ||
if (event.source != self.win) return; | ||
(self.onmessage || function() {}).call(self, event.data); | ||
}; | ||
if (window.addEventListener) window.addEventListener("message", this._receiver, false); else if (window.attachEvent) window.attachEvent("onmessage", this._receiver); | ||
}; | ||
Messenger.prototype.send = function(data) { | ||
this.win.postMessage(data, "*"); | ||
}; | ||
// in IE, postMessage API is not supported | ||
if (!window.postMessage && window.attachEvent) { | ||
// redefine the init method | ||
Messenger.prototype.init = function() { | ||
var isSameOrigin = false; | ||
// test if the two document is same origin | ||
try { | ||
isSameOrigin = !!this.win.location.href; | ||
} catch (ex) {} | ||
if (isSameOrigin) { | ||
this.send = this.sendForSameOrigin; | ||
this.initForSameOrigin(); | ||
return; | ||
define("arale/messenger/2.0.0/messenger-debug", [], function(require, exports, module) { | ||
/** | ||
* __ ___ | ||
* / |/ /___ _____ _____ ___ ____ ____ _ ___ _____ | ||
* / /|_/ // _ \ / ___// ___// _ \ / __ \ / __ `// _ \ / ___/ | ||
* / / / // __/(__ )(__ )/ __// / / // /_/ // __// / | ||
* /_/ /_/ \___//____//____/ \___//_/ /_/ \__, / \___//_/ | ||
* /____/ | ||
* | ||
* @description MessengerJS, a common cross-document communicate solution. | ||
* @author biqing kwok | ||
* @version 2.0 | ||
* @license release under MIT license | ||
*/ | ||
module.exports = function() { | ||
// 消息前缀, 建议使用自己的项目名, 避免多项目之间的冲突 | ||
var prefix = "arale-messenger", supportPostMessage = "postMessage" in window; | ||
// Target 类, 消息对象 | ||
function Target(target, name) { | ||
var errMsg = ""; | ||
if (arguments.length < 2) { | ||
errMsg = "target error - target and name are both required"; | ||
} else if (typeof target != "object") { | ||
errMsg = "target error - target itself must be window object"; | ||
} else if (typeof name != "string") { | ||
errMsg = "target error - target name must be string type"; | ||
} | ||
// different origin case | ||
// init the message queue, which can guarantee the messages won't be lost | ||
this.queue = []; | ||
if (window.parent == this.win) { | ||
this.initForParent(); | ||
} else { | ||
this.initForFrame(); | ||
if (errMsg) { | ||
throw new Error(errMsg); | ||
} | ||
}; | ||
Messenger.prototype.initForSameOrigin = function() { | ||
var self = this; | ||
this._dataavailable = function(event) { | ||
if (!event.eventType || event.eventType !== "message" || event.eventSource != self.win) return; | ||
(self.onmessage || function() {}).call(self, event.eventData); | ||
this.target = target; | ||
this.name = name; | ||
} | ||
// 往 target 发送消息, 出于安全考虑, 发送消息会带上前缀 | ||
if (supportPostMessage) { | ||
// IE8+ 以及现代浏览器支持 | ||
Target.prototype.send = function(msg) { | ||
this.target.postMessage(prefix + msg, "*"); | ||
}; | ||
document.attachEvent("ondataavailable", this._dataavailable); | ||
}; | ||
Messenger.prototype.sendForSameOrigin = function(data) { | ||
var self = this; | ||
setTimeout(function() { | ||
var event = self.win.document.createEventObject(); | ||
event.eventType = "message"; | ||
event.eventSource = window; | ||
event.eventData = data; | ||
self.win.document.fireEvent("ondataavailable", event); | ||
}); | ||
}; | ||
// create two iframe in iframe page | ||
Messenger.prototype.initForParent = function() { | ||
var fragment = document.createDocumentFragment(); | ||
var style = "width: 1px; height: 1px; position: absolute; left: -999px; top: -999px;"; | ||
var senderFrame = document.createElement("iframe"); | ||
senderFrame.src = 'javascript:""'; | ||
senderFrame.style.cssText = style; | ||
fragment.appendChild(senderFrame); | ||
var receiverFrame = document.createElement("iframe"); | ||
receiverFrame.src = 'javascript:""'; | ||
receiverFrame.style.cssText = style; | ||
fragment.appendChild(receiverFrame); | ||
document.body.insertBefore(fragment, document.body.firstChild); | ||
this.senderWin = senderFrame.contentWindow; | ||
this.receiverWin = receiverFrame.contentWindow; | ||
this.startReceive(); | ||
// for destroy | ||
this._fragment = fragment; | ||
}; | ||
// parent page wait the messenger iframe is ready | ||
Messenger.prototype.initForFrame = function() { | ||
this.senderWin = null; | ||
this.receiverWin = null; | ||
var self = this; | ||
this.timerId = setInterval(function() { | ||
self.waitForFrame(); | ||
}, 50); | ||
}; | ||
// parent page polling the messenger iframe | ||
// when all is ready, start trying to receive message | ||
Messenger.prototype.waitForFrame = function() { | ||
var senderWin; | ||
var receiverWin; | ||
try { | ||
senderWin = this.win[1]; | ||
receiverWin = this.win[0]; | ||
} catch (ex) {} | ||
if (!senderWin || !receiverWin) return; | ||
clearInterval(this.timerId); | ||
this.senderWin = senderWin; | ||
this.receiverWin = receiverWin; | ||
if (this.queue.length) this.flush(); | ||
this.startReceive(); | ||
}; | ||
// polling the messenger iframe's window.name | ||
Messenger.prototype.startReceive = function() { | ||
var self = this; | ||
this.timerId = setInterval(function() { | ||
self.tryReceive(); | ||
}, 50); | ||
} else { | ||
// 兼容IE 6/7 | ||
Target.prototype.send = function(msg) { | ||
var targetFunc = window.navigator[prefix + this.name]; | ||
if (typeof targetFunc == "function") { | ||
targetFunc(prefix + msg, window); | ||
} else { | ||
throw new Error("target callback function is not defined"); | ||
} | ||
}; | ||
} | ||
// 信使类 | ||
// 创建Messenger实例时指定, 必须指定Messenger的名字, (可选)指定项目名, 以避免Mashup类应用中的冲突 | ||
// !注意: 父子页面中projectName必须保持一致, 否则无法匹配 | ||
function Messenger(messengerName, projectName) { | ||
this.targets = {}; | ||
this.name = messengerName; | ||
this.listenFunc = []; | ||
prefix = projectName || prefix; | ||
this.initListen(); | ||
} | ||
// 添加一个消息对象 | ||
Messenger.prototype.addTarget = function(target, name) { | ||
var targetObj = new Target(target, name); | ||
this.targets[name] = targetObj; | ||
}; | ||
Messenger.prototype.tryReceive = function() { | ||
try { | ||
// If we can access name, we have already got the data. | ||
this.receiverWin.name; | ||
return; | ||
} catch (ex) {} | ||
// if the name property can not be accessed, try to change the messenger iframe's location to 'about blank' | ||
this.receiverWin.location.replace('javascript:"";'); | ||
// We have to delay receiving to avoid access-denied error. | ||
// 初始化消息监听 | ||
Messenger.prototype.initListen = function() { | ||
var self = this; | ||
setTimeout(function() { | ||
self.receive(); | ||
}, 0); | ||
var generalCallback = function(msg) { | ||
if (typeof msg == "object" && msg.data) { | ||
msg = msg.data; | ||
} | ||
// 剥离消息前缀 | ||
msg = msg.slice(prefix.length); | ||
for (var i = 0; i < self.listenFunc.length; i++) { | ||
self.listenFunc[i](msg); | ||
} | ||
}; | ||
if (supportPostMessage) { | ||
if ("addEventListener" in document) { | ||
window.addEventListener("message", generalCallback, false); | ||
} else if ("attachEvent" in document) { | ||
window.attachEvent("onmessage", generalCallback); | ||
} | ||
} else { | ||
// 兼容IE 6/7 | ||
window.navigator[prefix + this.name] = generalCallback; | ||
} | ||
}; | ||
// recieve and parse the data, call the listener function | ||
Messenger.prototype.receive = function() { | ||
var rawData = null; | ||
try { | ||
rawData = this.receiverWin.name; | ||
} catch (ex) {} | ||
if (!rawData) return; | ||
this.receiverWin.name = ""; | ||
var self = this; | ||
var dataList = rawData.substring(1).split("|"); | ||
for (var i = 0; i < dataList.length; i++) (function() { | ||
var data = decodeURIComponent(dataList[i]); | ||
setTimeout(function() { | ||
(self.onmessage || function() {}).call(self, data); | ||
}, 0); | ||
})(); | ||
// 监听消息 | ||
Messenger.prototype.listen = function(callback) { | ||
this.listenFunc.push(callback); | ||
}; | ||
// send data via push the data into the message queue | ||
Messenger.prototype.send = function(data) { | ||
this.queue.push(data); | ||
if (!this.senderWin) return; | ||
this.flush(); | ||
// 注销监听 | ||
Messenger.prototype.clear = function() { | ||
this.listenFunc = []; | ||
}; | ||
Messenger.prototype.flush = function() { | ||
var dataList = []; | ||
for (var i = 0; i < this.queue.length; i++) dataList[i] = encodeURIComponent(this.queue[i]); | ||
var encodedData = "|" + dataList.join("|"); | ||
try { | ||
this.senderWin.name += encodedData; | ||
this.queue.length = 0; | ||
} catch (ex) { | ||
this.senderWin.location.replace("about:blank"); | ||
var self = this; | ||
setTimeout(function() { | ||
self.flush(); | ||
}, 0); | ||
// 广播消息 | ||
Messenger.prototype.send = function(msg) { | ||
var targets = this.targets, target; | ||
for (target in targets) { | ||
if (targets.hasOwnProperty(target)) { | ||
targets[target].send(msg); | ||
} | ||
} | ||
}; | ||
} | ||
Messenger.prototype.destroy = function() { | ||
// 删除定时器 | ||
clearInterval(this.timerId); | ||
// 解除绑定事件 | ||
if (this._receiver) { | ||
if (window.removeEventListener) window.removeEventListener("message", this._receiver, false); else if (window.detachEvent) window.detachEvent("onmessage", this._receiver); | ||
} | ||
// 解除绑定事件 ie | ||
if (document.detachEvent && this._dataavailable) { | ||
document.detachEvent("ondataavailable", this._dataavailable); | ||
} | ||
// 删除fragment | ||
this._fragment && document.body.removeChild(this._fragment); | ||
// 删除实例属性 | ||
for (var p in this) { | ||
if (this.hasOwnProperty(p)) { | ||
delete this[p]; | ||
} | ||
} | ||
}; | ||
module.exports = Messenger; | ||
}); | ||
return Messenger; | ||
}(); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.