Added "ready" event for iframe message passing

* When shellinabox is ready it sends "ready" message to parent window.
* Example file was updated with new use case.
This commit is contained in:
Luka Krajger 2016-09-26 13:14:56 +02:00
parent 8e28bb4c2a
commit d4bd77ca45
2 changed files with 19 additions and 5 deletions

View file

@ -14,7 +14,7 @@
For communication with Shell In A Box we need to set '-m' (messages-origin)
command line option with appropriate messages origin. Origin should be set to
URL of parent (this) window. If origin is set to '*' Shell In A Box won't checki
URL of parent (this) window. If origin is set to '*' Shell In A Box won't check
origin on received messages. This is usually unsafe option.
Command line example:
@ -59,6 +59,9 @@
Following types of messages can be received from shellinabox:
* ready
signals that shellinabox is ready to send and receive messages
* output
data field contains terminal output
@ -140,10 +143,6 @@
var output = document.getElementById("output");
var session = document.getElementById("session");
// Add url to our iframe. We do this, only that variable 'url' can be used
// throughout the whole code where needed.
iframe.src = url;
document.getElementById("execute").addEventListener("click", function() {
// Send input to shellinabox
var message = JSON.stringify({
@ -209,6 +208,15 @@
// Handle response according to response type
var decoded = JSON.parse(message.data);
switch (decoded.type) {
case "ready":
// Shellinabox is ready to communicate and we will enable console output
// by default.
var message = JSON.stringify({
type : 'output',
data : 'enable'
});
iframe.contentWindow.postMessage(message, url);
break;
case "output" :
// Append new output
output.innerHTML = output.innerHTML + decoded.data;
@ -220,6 +228,9 @@
}
}, false);
// Add url to our iframe after the event listener is installed.
iframe.src = url;
</script>
</body>

View file

@ -406,6 +406,9 @@ ShellInABox.prototype.messageInit = function() {
}
}
// After message mechanisms are in place "ready" message is sent to parent
// window.
parent.postMessage(JSON.stringify({type : 'ready', data : ''}), '*');
};
ShellInABox.prototype.messageReceive = function (message) {