0

Hi i'm very new to electron please bear with me.

i have a situation like this:

  1. i will get filename from li

  2. once the filename got i want to write it with below code

    var fs = require('fs');
    fs.writeFile('gotfromjsvariable', 'Hello content!', function (err) 
    

    { if (err) throw err; console.log('Saved!'); });

suppose gotfromjsvariable is coming from below code

question is, How do i pass it to electron main.js or same file node.js

$(document).on('click','#files li',function(){
   var gotfromjsvariable = $(this).data('filename');
   // gotfromjsvariable has to be sent a filename to nodejs or electron
   
   /*
  
 var fs = require('fs');
fs.writeFile('gotfromjsvariable', 'Hello content!', function (err) {
            if (err) throw err;
              console.log('Saved!');
  });
   
   */
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="files">
  <li data-filename="abc.json">abc.json</li>
  <li data-filename="pqr.json">pqr.json</li>
</ul>
  • `gotfromjsvariable` is a variable name. So, you can use it without single quote on the **fs.writeFile()** method – Tamilvanan Sep 26 '18 at 10:50

1 Answers1

0

Basically you send messages (data) from Renderer process to Main process by ipcRenderer.

However, as the Renderer process also have access to node API your task can be done completely in html. Your fixed code can look like this:

<!DOCTYPE html>
  <html>
    <body>
      <ul id="files">
        <li data-filename="abc.json">abc.json</li>
        <li data-filename="pqr.json">pqr.json</li>
      </ul>
      <script>
        const fs = require('fs')
        window.$ = window.jQuery = require('jquery')
        $(document).on('click','#files', (e) => {
          var gotfromjsvariable = $(e.target).data('filename')
          fs.writeFile(gotfromjsvariable, 'Hello content!', (err) => {
            if (err) throw err
            console.log('saved')
          })
        })
      </script>
    </body>
 </html>

For jquery I've used this approach: https://stackoverflow.com/a/32621989/2550156

pergy
  • 5,285
  • 1
  • 22
  • 36