Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to paste+execute (simulate pressing Enter on Xterm.js) #979

Closed
arekdudys opened this issue Sep 5, 2022 · 4 comments
Closed

How to paste+execute (simulate pressing Enter on Xterm.js) #979

arekdudys opened this issue Sep 5, 2022 · 4 comments

Comments

@arekdudys
Copy link

I found it worth mentioning as I couldn't find an answer anywhere and it took me hours to figure it out.

This may be of interest to those who embed ttyd into their website (iframe etc.) and want to execute their command immediately, i.e. on button click.

So, if you want to pass the entire command and execute it immediately (without clicking on xterm and manually pressing enter), it's simple as :

term.paste("whoami");
document.querySelector("textarea.xterm-helper-textarea").dispatchEvent(new KeyboardEvent('keypress', {charCode: 13}))

If you are using an iframe, you must first access its contentWindow and contentDocument just like :

document.getElementById('youriframeid').contentWindow.term.paste("whoami");
document.getElementById('youriframeid').contentDocument.querySelector("textarea.xterm-helper-textarea").dispatchEvent(new KeyboardEvent('keypress', {charCode: 13}))
@arekdudys arekdudys changed the title How to paste+execute (simulate pressing Enter) How to paste+execute (simulate pressing Enter on Xterm.js) Sep 5, 2022
@tsl0922
Copy link
Owner

tsl0922 commented Nov 3, 2022

@tsl0922 tsl0922 closed this as completed Nov 3, 2022
@natarajan0007
Copy link

I am getting the following VM4357:1 Uncaught DOMException: Blocked a frame with origin "http://localhost:8501" from accessing a cross-origin frame.
i tried this i am getting this error message.
image

@arekdudys
Copy link
Author

arekdudys commented Sep 26, 2023

Caused by browser security. See https://stackoverflow.com/a/25098153

@natarajan0007
Copy link

I was able to make it work using post message. Thanks for sharing the link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants