@@ -111,11 +111,27 @@ The next table demonstrates available API. Left column are `terminal` object pro
111
111
the user after execution (prints "NAMESPACE > " as well).
112
112
</td>
113
113
</tr>
114
+ <tr>
115
+ <td>onOutput([ <b>options</b> ], <b>callback</b>)</td>
116
+ <td>
117
+ By default, <code>callback</code>(<u>strings</u>) will be called before the user is
118
+ prompted for input, and <code>strings</code> array will always contain an array of
119
+ chunks of all the text printed between the prompts. For example, if user writes
120
+ <code>write 123</code> and presses "Enter", the <code>strings</code> will contain
121
+ this array: <code>["\r\n", "1", "\r\n"]</code>. However, when user enters
122
+ <code>write 1, 2, 3</code>, <code>strings</code> will result with
123
+ <code>["\r\n", "1", "2", "3", "\r\n"]</code>. You can join this array with
124
+ <code>join</code> array method to get the full output.<br/>
125
+ Optional <code>options</code> object may include <code>stream</code> property, which
126
+ is <code>false</code> by default. When set to <code>true</code>, callback will be
127
+ fired any time any chunk is print to the terminal simultaneously.
128
+ </td>
129
+ </tr>
114
130
<tr>
115
- <td>onUserInput(<b>cb </b>)</td>
131
+ <td>onUserInput(<b>callback </b>)</td>
116
132
<td>
117
- <b>cb </b>(<u>text</u>, <u>mode</u>) is fired right after user presses enter. Argument
118
- <code>text</code> is a <code>String</code> of user input, and
133
+ <b>callback </b>(<u>text</u>, <u>mode</u>) is fired right after user presses enter.
134
+ Argument <code>text</code> is a <code>String</code> of user input, and
119
135
<code>mode</code> is a <code>Number</code>, which can be compared
120
136
with one of the terminal mode constants, such as <code>MODE_PROMPT</code>.
121
137
</td>
@@ -153,11 +169,18 @@ function myInitHandler (terminal) {
153
169
terminal .execute (" set hiddenVariable = 7" , {
154
170
echo: false // the default is false, this is just a demo
155
171
});
156
- terminal .onUserInput (function (text , mode ) {
172
+ terminal .onUserInput ((text , mode ) => {
157
173
if (mode !== terminal .MODE_PROMPT )
158
174
return ;
159
175
terminal .print (" \r\n You've just entered the next command: " + text);
160
176
});
177
+ terminal .onOutput ((chunks ) => {
178
+ // If you "write 12", chunks are ["\r\n", "12", "\r\n"].
179
+ // If you "write 1, 2", chunks are ["\r\n", "1", "2", "\r\n"].
180
+ if (chunks[1 ] === " duck" ) {
181
+ alert (` You've found a secret phrase!` );
182
+ }
183
+ });
161
184
}
162
185
163
186
// At first, handle iFrame load event. Note that the load handler won't work
0 commit comments