メインプロセスからレンダラープロセスに通信したいと思っています。
一応できているのですが、できるときとできないときがあり安定しません。
理由がわからず困っております。
どうぞご教授お願い致します。
具体的には下記コードmain.jsの
win.webContents.on('did-finish-load', () => {
win.webContents.send('ping', app.getPath('userData'))
console.log('test');
})
でファイルルのパスを取得してindex.htmlの
require('electron').ipcRenderer.on('ping', (event, message) => {
db_path = require('path').join(message, 'diary.db') console.log(db_path);
})
で上記パスを受け取りたいです。
htmlのconsole.log(db_path);で表示されるときと表示されないときがあります。
また、起動時は表示されてもリロードするとかなりの頻度で表示されません。
javascript
1main.js 2 3 4const {app, BrowserWindow, Menu} = require('electron'); 5const path = require('path'); 6const url = require('url'); 7 8 9let win; 10var settings_path = require('path').join(app.getPath('userData'), 'setWin.json'); 11function createWindow () { 12 13 try{ 14 var setWin = require(settings_path); 15 win = new BrowserWindow(setWin); 16 }catch(e){ 17 win = new BrowserWindow({width: 800, height: 600, x: 0, y: 0, show: false}); 18 } 19 20 win.once('ready-to-show', () => { win.show(); }); 21 win.loadURL(url.format({ 22 pathname: path.join(__dirname, 'index.html'), 23 protocol: 'file:', 24 slashes: true 25 })); 26 27 28 win.webContents.on('did-finish-load', () => { 29 win.webContents.send('ping', app.getPath('userData')) 30 console.log('test'); 31 }) 32 33 34 win.webContents.openDevTools(); 35 36 37 38 win.on('close', function() { 39 var fs = require('fs'); 40 var item = win.getBounds(); 41 item = '{' + '"x":' + item['x'] + ',' + '"y":' + item['y'] + ',' + '"width":' + item['width'] + ',' + '"height":' + item['height'] + ',' + '"show":false}' 42 fs.writeFileSync(settings_path, item); 43 }); 44 45 46 win.on('closed', () => { 47 48 win = null; 49 }) 50 51 52} 53 54app.on('ready', createWindow); 55 56 57const template = [ 58 { 59 label: 'Edit', submenu: [{role: 'undo'}, {role: 'redo'},{type: 'separator'},{role: 'cut'},{role: 'copy'},{role: 'paste'},{role: 'pasteandmatchstyle'}, 60 {role: 'delete'},{role: 'selectall'}] 61 }, 62 { 63 label: 'View',submenu: [{role: 'reload'},{role: 'toggledevtools'},{type: 'separator'},{role: 'zoomin'},{role: 'zoomout'},{type: 'separator'},{role: 'togglefullscreen'}] 64 }, 65 { 66 role: 'window',submenu: [{role: 'minimize'},{role: 'close'}] 67 } 68] 69const menu = Menu.buildFromTemplate(template) 70Menu.setApplicationMenu(menu) 71 72app.on('window-all-closed', () => { 73 74 if (process.platform !== 'darwin') { 75 app.quit(); 76 } 77}); 78 79app.on('activate', () => { 80 81 if (win === null) { 82 createWindow(); 83 } 84}); 85
html
1index.html 2 3<!DOCTYPE html> 4<html lang="ja"> 5<head> 6 <meta charset="UTF-8"> 7 <title>mydiary</title> 8 <link href="css/mystyle.css" rel="stylesheet" type="text/css"> 9 <link href="./css/font-awesome.min.css" rel="stylesheet"> 10 <link href="./jquery-ui-1.12.1.custom/jquery-ui.css" rel="stylesheet"> 11 12</head> 13<body> 14 15<script> 16window.$ = window.jQuery = require('jquery'); 17 18$(function(){ 19 20 require('electron').ipcRenderer.on('ping', (event, message) => { 21 22 db_path = require('path').join(message, 'diary.db') 23 console.log(db_path); 24 }) 25 26}); 27 28</script> 29<script type="text/javascript" src="./jquery-ui-1.12.1.custom/jquery-ui.js"></script> 30<script src="./datepicker-ja.js"></script> 31</body> 32</html>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。