chronosjs
Version:
JS Channels Mechanism
102 lines (95 loc) • 2.78 kB
HTML
<html>
<head>
<meta charset="utf-8">
<title>Courier Tests</title>
<script type="text/javascript" src="../dist/PostMessageCourier.js"></script>
<script type="text/javascript">
var channel = new Chronos.Channels();
var courier = new Chronos.PostMessageCourier({
eventChannel: channel
});
channel.bind("host", "Ma Shlomha?", function(data) {
if (data && data.text) {
alert(data.text);
}
alert("HAKOL TOV!");
});
courier.bind({
appName: "host",
eventName: "Ma Shlomha?",
aSync: true,
triggerOnce: true,
func: function(data) {
alert("TODA!");
}
});
courier.comply({
appName: "host",
cmdName: "console",
func: function(data) {
if (data && data.text) {
console.log(data.text);
}
}
});
courier.reply({
appName: "host",
reqName: "Your Name?",
func: function(data) {
if (data && data.text) {
alert(data.text);
}
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve("My Name is IFrame!");
}, 3000);
});
}
});
</script>
</head>
<body>
<button id="SendEvent">Event</button>
<button id="SendCommand">Command</button>
<button id="SendRequest">Request</button>
<script type="text/javascript">
var btnEvent = document.getElementById("SendEvent");
var btnCommand = document.getElementById("SendCommand");
var btnRequest = document.getElementById("SendRequest");
btnEvent.onclick = function() {
courier.trigger({
appName: "iframe",
eventName: "Ma Hmaztav?",
data: {
text: "Just Asking (Ma Hmaztav?)"
}
});
};
btnCommand.onclick = function() {
courier.command({
appName: "iframe",
cmdName: "console",
data: {
text: "Log this from iframe"
}
});
};
btnRequest.onclick = function() {
courier.request({
appName: "iframe",
reqName: "Your Name?",
data: {
text: "Just Asking (What is Your Name?)"
}
}, function(err, data) {
if (err) {
console.error("OOOOOOOOOOOOOOOOPS! Something happened");
return;
}
alert(data);
});
};
</script>
</body>
</html>