How do I run a process function via Native Menu


#1

Can I run webview function in native menu? eg.

index.html

function test() {
alert('Test');
}

main.js

{ label: "Test", accelerator: "Command+1", click: function() { mainWindow.test()); }},

Thanks


#2

You can use the ipc module to communicate between processes.

// main process
{
  label: "Test",
  accelerator: "Command+1",
  click: function (menuItem, currentWindow) {
    currentWindow.webContents.send('test')
  }
}

// renderer process
ipc.on('test', function (ev, data) {
  console.log(...)
})

#3

ipc module seams to be depreciated I’m trying to us ipcMain instead:

//main.js

const ipcMain = require('ipc-main');

{
  label: "Test",
  accelerator: "Command+1",
  click: function () {
    mainWindow.webContents.send('test');
  }
}
//index.html

<script>

var ipcMain = require('ipc-main');

ipcMain.on('test', function() {
    console.log('menu test');
});

</script>

No success thought…


#4

I think you need to require ipc-renderer in index.html.


#5
// index.html    
<script>
        require('electron-cookies');
        var ipcRenderer = require('electron').ipcRenderer;

        ipcRenderer.on('test', function() {
            console.log('test');
        });
</script>

I thin in the main we should be something else rather than:

mainWindow.webContents.send('test');

as we are declaring

const ipcMain = require('ipc-main');

and not using it.


#6

Hi. It’d be great to have a clue on how to do this. I’m trying to setup a shortcut to a save-file function. Any help appreciated. Anybody have a guide on local keyboard shortcuts?
EDIT: I figured this out: https://github.com/szwacz/electron-boilerplate/issues/71#issuecomment-215697886