0

I'm learning electron, took electron-quick-start and made certain modifications to index.html, main.js and renderer.js following the tutorial at https://blog.logrocket.com/handling-interprocess-communications-in-electron-applications-like-a-pro/

index.html

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

  <title>Electron-Process-Comm</title>
  <link rel="stylesheet" href="style.css">
  <meta charset="UTF-8">
  <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
  <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
  <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
</head>
<body>
<h2 id="mainWindowTitle">I am the main window</h2>
<button id="sendSyncMsgBtn">Ping Main Process</button>
<p id="syncReply">Chilling for response</p>

<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>

main.js

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
const ipcMain = require('electron').ipcMain

console.log('Hello')

ipcMain.on('sync-message', (event, arg) => {
  console.log('IPCMain')
  event.returnValue = 'Message Recieved!'
})

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

renderer.js

const electron = require('electron')
const ipc = electron.ipcRenderer

const syncMsgBtn = document.querySelector('#sendSyncMsgBtn')

console.log('Hello renderer')

syncMsgBtn.addEventListener('click', () => {
  console.log('Renderer')
  const reply = ipc.sendSync('sync-message', 'Sent from main Window')
  const message = `Synchronous message reply: ${reply}`
  document.querySelector('#syncReply').innerHTML = message
})

const asyncMsgBtn = document.querySelector('#sendAsyncMsgBtn')

asyncMsgBtn.addEventListener('click', () => {
  ipc.send('async-message', 'Async message baby')

})

Now, though renderer.js is included in the index.html it is never invoked. The console log doesn't appear and the button click doesn;t respond. Can somebody tell me what i'm doing wrong ?

SBakki
  • 5
  • 4

1 Answers1

1

The code looks fine and should be executed when running. You might be looking at the wrong console? The log statements should appear in the console of the renderer window. To open the Developer Tools, you can press Cmd + Opt + I (macOS) and Ctrl + Shift + I (Windows).

You can find more information about debugging Electron applications in their documentation and the Developer Tools in Chrome's documentation.

Sebastian Richner
  • 782
  • 1
  • 13
  • 21
  • thanks, but the button press on window must show up the change right. it doesn't. I'm running it on visual studio code, windows 10. The console log for main.js shows up fine. – SBakki Apr 28 '21 at 19:25
  • Are you getting any errors in the renderer's console? – Sebastian Richner Apr 28 '21 at 19:29
  • sorry, where can i find renderer console in visual studio code ? Ctrl + Shift + I doesn't open up anything. – SBakki Apr 28 '21 at 19:32
  • I've updated my answer with two links to the documentation. You should be able to open them when you have the renderer's window open. You can also open the dev tools programmatically. See https://www.electronjs.org/docs/tutorial/application-debugging – Sebastian Richner Apr 28 '21 at 19:38
  • ahh, i could open the renderer console. here is the error: Uncaught ReferenceError: require is not defined at renderer.js:8 what and why is this ? – SBakki Apr 28 '21 at 19:44
  • You can get it to work by enabling node integration (see more here: https://www.electronjs.org/docs/faq#i-can-not-use-jqueryrequirejsmeteorangularjs-in-electron). Please be aware of the security implications documented here: https://www.electronjs.org/docs/tutorial/security#2-do-not-enable-nodejs-integration-for-remote-content. You may find more information e.g., in the following SO Answer: https://stackoverflow.com/questions/52236641/electron-ipc-and-nodeintegration – Sebastian Richner Apr 28 '21 at 19:51
  • i set it like this in main.js webPreferences: { nodeIntegration: true } Still couldn't get require: Failed to load resource: net::ERR_FILE_NOT_FOUND – SBakki Apr 28 '21 at 19:58
  • I just realized that you are including a file called "require.js", does that exist? – Sebastian Richner Apr 28 '21 at 20:01
  • Sorry about that, i removed that and tested. The require error still stays: Uncaught ReferenceError: require is not defined at renderer.js:8 – SBakki Apr 28 '21 at 20:07
  • You might also need to set contextIsolation to false. Please do consider using the preload script though for everything you need from Node/Electron in the renderer process. – Sebastian Richner Apr 28 '21 at 20:15
  • Excellent, setting contextIsolation solved it.Thanks! I start to get the structure of electron now. – SBakki Apr 28 '21 at 20:34