question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Executing a command by javascript when clicking on a link

See original GitHub issue

Is there a way to make the following work without having to manually press enter? I need this to make a small automation tool.

<div id="terminal-container"></div>
<div id="commands">
    <a href="javascript:exec('ls -la');">ls -la</a>
    <a href="javascript:exec('sudo su');">sudo su</a>
</div>
<script>
function exec(cmd) {
    term.send(cmd); // needs manual Enter
    term.write(cmd); // also needs manual Enter
    term.writeln(cmd); // although the cursor advances to the next line, the command does not get executed
}
</script>

I have searched the issues and the closest thing I found was this comment offering this method:

process.send({
  event: 'input',
  data: text
});

But where is process exposed? It does not seem to be accessible thorough term.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Anirudh16commented, Aug 30, 2021

You need to write it to node-pty, not xterm.js. You’re trying to simulate an xterm.js onData event which would be forwarded to node-pty, so call whatever you’re using in your onData event handler.

Key presses: xterm.js -> node-pty Process data: node-pty -> xterm.js

@Tyriar Used pty.write(“\r”) and was able to execute the commands

0reactions
Tyriarcommented, Aug 25, 2021

You need to write it to node-pty, not xterm.js. You’re trying to simulate an xterm.js onData event which would be forwarded to node-pty, so call whatever you’re using in your onData event handler.

Key presses: xterm.js -> node-pty Process data: node-pty -> xterm.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

call javascript function on hyperlink click - Stack Overflow
The onclick event handler captures a click event from the users' mouse button on the element to which the onclick attribute is applied....
Read more >
HREF JavaScript: Call JavaScript Functions - Udemy Blog
HREF JavaScript is a method to easily call a JavaScript function when a user clicks on a link on a website. If you've...
Read more >
Execute JavaScript on link click | Example code
There are many ways to Execute a JavaScript function in an HTML document. Using the onclick event attribute simple way to Execute JavaScript...
Read more >
Running onclick JavaScript command using 'Inject JS Script'
The link I'm trying to click on has a piece of code written in javascript; the onlick looks like onlick=“javascript:code” whilst the href...
Read more >
Links used to activate JavaScript functions - Flavio Copes
When you are creating an app using plain JavaScript, sometimes you'll have the necessity of triggering a function when the user clicks a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found