Reputation: 499
Hello I am new in electron and I am facing a problem, I want to execute a command on click button for that I am using a child_process of node. I have written a code for app.on('ready') and its working fine. But I need it on click below is my code please help..
function runExec() {
child_process.exec('pwd', function(error, stdout, stderr){
if(!error){
console.log(stdout);
setTimeout(function(){
child_process.exec('ls -la', function(error, stdout, stderr){
console.log(stdout);
});
}, 3000);
}
});
}
I want to run this function on click with jquery/index.html HTML code is here:
<div class="col-sm-12 controls">
<a id="btn-login" href="#" class="btn btn-success btnLogin">Login </a>
</div>
As I am hitting an API on click of login button and get response & I want to call that function too on same click or true response..
Any Idea..
thanks in advance :)
Upvotes: 3
Views: 7836
Reputation: 776
So, it works in your main process but you want to trigger it when someone clicks on a button in your renderer process?
What you'll need to do is export the functionality in your main process and then use Electron's remote
module to pull it into your renderer process.
In your main process, add the following code:
exports.runExec = runExec;
This will export your runExec
function as runExec
on the remote
object when you import it.
Now, in your renderer code (I am assuming your main file is called main.js
, but it might be named something else, which is totally okay, just replace ./main
with whatever the name of your file is on the second line):
var electron = require('electron');
var runExec = electron.remote.require('./main').runExec;
var loginButton = document.querySelector('. btnLogin');
loginButton.addEventListener('click', function () {
runExec();
});
Electron's remote
module acts as a proxy to the main process. So when someone clicks on the "Login" button, your renderer process will trigger the runExec
function from the main process.
Upvotes: 5