1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| 主进程 win.webContents.send('call-page-function', 'showMessage', 'Hello from Main Process!');
渲染进程在页面发起调用的时候进行监听 contextBridge.exposeInMainWorld('electronAPI', { onFunctionCall: (callback) => ipcRenderer.on('call-page-function', (event, functionName, ...args) => { callback(functionName, ...args); }), });
页面
const pageFunctions = { showMessage: (message) => { console.log('Executing showMessage:', message); document.getElementById('output').innerHTML += `<p>${message}</p>`; }, logData: (data) => { console.log('Logging data:', data); }, };
window.electronAPI.onFunctionCall((functionName, ...args) => { if (pageFunctions[functionName]) { pageFunctions[functionName](...args); } else { console.error(`Function "${functionName}" not found.`); } });
|