Search code examples
uwpxtermjs

Xterm.js in UWP WebView - paste programmatically


I'm working on a terminal app based on Xterm.js which runs in UWP WebView. Everything works OK, and I'm able to paste any text to the terminal by using Ctrl+V - it looks that it is handled automatically by Xterm.js.

But I cannot implement "Paste" menu option. I've tried to do something like:

var value = await _webView.InvokeScriptAsync("eval", new []{"document.execCommand('paste')"});

(according to https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) but no luck. Nothing get pasted, and the call returns empty string.

Any ideas how to implement paste from UWP app menu? Thanks!


Solution

  • This is not a problem with WebView because I found it from the github issue at xterm.js.

    You can consider publishing a Terminal object, such as attaching it to the window object so that it can be publicly accessed from outside, then use _webView.InvokeScriptAsync("eval",new string[] { "window.term.paste("some string") "});to paste.

    If you don't want to expose the terminal object, you can wrap the paste method as a public function and then call it through InvokeScriptAsync.

    Best regards.