質問編集履歴
1
追記
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
IPC通信を実装したらrequire is not definedとエラーが発生した
|
1
|
+
ElectronでIPC通信を実装したらrequire is not definedとエラーが発生した
|
test
CHANGED
@@ -221,3 +221,179 @@
|
|
221
221
|
}
|
222
222
|
|
223
223
|
```
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
##
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
# =============追記==============
|
232
|
+
|
233
|
+
ipc通信の書き方に関する書き方が古いというご意見をいただいたので、次の記事を参考にしてもやはりエラーが出てしまいます。
|
234
|
+
|
235
|
+
https://qiita.com/pochman/items/62de713a014dcacbad68
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
以下を確認しても、書き方に特に問題はないように見える
|
240
|
+
|
241
|
+
https://github.com/electron/electron/blob/main/docs/api/ipc-main.md
|
242
|
+
|
243
|
+
https://github.com/electron/electron/blob/main/docs/api/ipc-renderer.md
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
###発生している問題・エラーメッセージ
|
248
|
+
|
249
|
+
`Uncaught TypeError: Cannot read property 'send' of undefined
|
250
|
+
|
251
|
+
at HTMLInputElement.<anonymous> (settings.html:45)`
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
###該当のソースコード
|
256
|
+
|
257
|
+
```JS
|
258
|
+
|
259
|
+
index.js
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
const { app, BrowserWindow,Menu,dialog,ipcMain } = require('electron');
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
ipcMain.on('settings_changed', (event, ...args)=> {
|
268
|
+
|
269
|
+
mainWindow.webContents.send('set_bgcolor', color);
|
270
|
+
|
271
|
+
});
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
function createMainWindow() {
|
276
|
+
|
277
|
+
mainWindow = new BrowserWindow({
|
278
|
+
|
279
|
+
width: 600,
|
280
|
+
|
281
|
+
height: 400,
|
282
|
+
|
283
|
+
webPreferences: {
|
284
|
+
|
285
|
+
nodeIntegration: false,
|
286
|
+
|
287
|
+
contextIsolation: true,
|
288
|
+
|
289
|
+
preload: __dirname + '/preload.js'
|
290
|
+
|
291
|
+
},
|
292
|
+
|
293
|
+
});
|
294
|
+
|
295
|
+
```
|
296
|
+
|
297
|
+
```JS
|
298
|
+
|
299
|
+
preload.js
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
const { contextBridge, ipcRenderer} = require("electron");
|
304
|
+
|
305
|
+
contextBridge.exposeInMainWorld(
|
306
|
+
|
307
|
+
"api", {
|
308
|
+
|
309
|
+
send: (channel, data) => {//rendererからの送信用//
|
310
|
+
|
311
|
+
ipcRenderer.send(channel, data);
|
312
|
+
|
313
|
+
},
|
314
|
+
|
315
|
+
on: (channel, func) => { //rendererでの受信用, funcはコールバック関数//
|
316
|
+
|
317
|
+
ipcRenderer.on(channel, (event,...args) => func(...args));
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
);
|
324
|
+
|
325
|
+
```
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
```html
|
330
|
+
|
331
|
+
index.html
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
window.api.on('set_bgcolor', (color)=>{
|
336
|
+
|
337
|
+
document.body.style.backgroundColor = color;
|
338
|
+
|
339
|
+
});//receive //ここで何か表記ミスした
|
340
|
+
|
341
|
+
```
|
342
|
+
|
343
|
+
```html
|
344
|
+
|
345
|
+
settings.html
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
<script >
|
350
|
+
|
351
|
+
'use strict';
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
let colors = document.getElementsByName('colors');
|
356
|
+
|
357
|
+
for (let i = 0; i < colors.length; i++) {
|
358
|
+
|
359
|
+
colors[i].addEventListener('change', function() {
|
360
|
+
|
361
|
+
let color = this.value;
|
362
|
+
|
363
|
+
// console.log(color);
|
364
|
+
|
365
|
+
// settings.html -> index.js -> index.html
|
366
|
+
|
367
|
+
// ipc
|
368
|
+
|
369
|
+
window.api.send('settings_changed', color);
|
370
|
+
|
371
|
+
});
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
</script>
|
376
|
+
|
377
|
+
```
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
```JS
|
382
|
+
|
383
|
+
package.json
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
"devDependencies": {
|
388
|
+
|
389
|
+
"electron": "v6.14.13"
|
390
|
+
|
391
|
+
},
|
392
|
+
|
393
|
+
"dependencies": {
|
394
|
+
|
395
|
+
"require": "^6.14.13"
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
```
|