Search code examples
xtermjs

Xtermjs get text


Using xterm.js, how do you get the text in the console (the text entered with term.write(...) or other methods).

Getting the textContent of the HTML does not work, because html handles whitespace differently and because xtermjs handles colors differently.


Solution

  • What you are referring to is the contents of the terminal's buffer.

    As of now (xterm.js 3.2) there is no public API to retrieve the contents of the buffer, so you will have to work your way around this.

    Attempting to retrieve the contents via the DOM will not work. Xterm.js uses a canvas element since version 3.0, instead of DOM div elements and text nodes, for performance.

    The best way to do this with a public API is by listening to the data event and work with that. The data event is emitted when a chunk of data is being dispatched to the terminal for handling.