Skip to content

Commit

Permalink
Added "ready" event for iframe message passing
Browse files Browse the repository at this point in the history
* When shellinabox is ready it sends "ready" message to parent window.
* Example file was updated with new use case.
  • Loading branch information
KLuka committed Sep 26, 2016
1 parent 8e28bb4 commit d4bd77c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
21 changes: 16 additions & 5 deletions misc/embedded.html
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -140,10 +143,6 @@ <h3>
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({
Expand Down Expand Up @@ -209,6 +208,15 @@ <h3>
// 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;
Expand All @@ -220,6 +228,9 @@ <h3>
}
}, false);

// Add url to our iframe after the event listener is installed.
iframe.src = url;

</script>

</body>
Expand Down
3 changes: 3 additions & 0 deletions shellinabox/shell_in_a_box.jspp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d4bd77c

Please sign in to comment.