-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
197 lines (173 loc) · 5.45 KB
/
main.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const electron = require('electron');
const session = require('electron').session;
const app = electron.app;
const Menu = electron.Menu;
const BrowserWindow = electron.BrowserWindow;
const pie = require("puppeteer-in-electron");
const puppeteer = require("puppeteer-core");
const dateformat = require('dateformat');
const ipc = require('electron').ipcMain
const kinrouLib = require('./lib/kinrou.js');
const debug = /--debug/.test(process.argv[2])
require("update-electron-app");
const logger = require("electron-log");
const Store = require('electron-store');
let store = new Store({});
if (debug) {
store = new Store({
"cwd": require('path').resolve(__dirname, '..'),
});
}
let conf = store.get("kinrou", null);
global.sharedObject = {
store: store,
debug: debug
};
// puppeteerを使うときはこのタイミングでinitializeする
pie.initialize(app);
var mainWindow = null;
// Render側に渡すためのメッセージ文字列
var text = "";
// main.htmlからsendを受け取ったらrecieveとともにテキストを返すイベント
ipc.on('loaded', function(event, arg) {
console.log(`receive loaded message:${arg}, reply text:${text}`);
event.reply("reply-text", text);
});
ipc.on('reload', function(event, arg) {
console.log(`receive loaded message:${arg}`);
// 設定があれば打刻処理を実施
conf = store.get("kinrou", null);
kinrouLib.dakoku(app, puppeteer, mainWindow, conf).then(v => {
text = v.text;
mainWindow.loadURL(v.url);
});
});
// メニューに関する情報
const template = [
{
"label":"メニュー",
"submenu": [{
"label": "ログイン情報",
"click": function(m,b,e) {
// logger.log(m, b, e);
kinrouLib.showConfig().then(v => {
mainWindow.loadURL(v.url);
});
},
"accelerator": "CmdOrCtrl+L"
}, {
"label": "パスワード変更",
"click": function(m,b,e) {
// logger.log(m, b, e);
kinrouLib.showConfig().then(v => {
mainWindow.loadURL(v.url);
});
},
"accelerator": "CmdOrCtrl+P"
}, {
"label": "実行",
"click": function(m,b,e) {
// logger.log(m, b, e);
kinrouLib.dakoku(app, puppeteer, mainWindow, conf).then(v => {
text = v.text;
mainWindow.loadURL(v.url);
});
},
"accelerator": "CmdOrCtrl+R"
},
{ label: "コピー", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "ペースト", accelerator: "CmdOrCtrl+V", selector: "paste:" },
]
}
];
const initialize = async () => {
logger.log("initialize");
makeSingleInstance();
const createWindow = async () => {
const windowOptions = {
width: 1080,
height: 680,
title: "獅子舞",
webPreferences: {
nodeIntegration: true,
// TODO: ここはIslandに変えて対応する
enableRemoteModule: true,
}
};
mainWindow = new BrowserWindow(windowOptions);
if (debug) {
mainWindow.webContents.openDevTools()
}
console.log("mainWindow is initialized");
mainWindow.on("closed", () => {
mainWindow = null;
});
};
app.on('ready', () => {
logger.log("ready");
/*
* TODO セキュリティ対応
if (!debug) {
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
callback({
responseHeaders: {
'Content-Security-Policy': [`default-src \'self\' ${kinrouLib.kinrouUrl}`]
}
})
})
}
*/
createWindow();
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
if (conf === null) {
console.info("show config with config is null");
kinrouLib.showConfig().then(v => {
mainWindow.loadURL(v.url);
});
return;
}
// 設定があれば打刻処理を実施
kinrouLib.dakoku(app, puppeteer, mainWindow, conf).then(v => {
text = v.text;
mainWindow.loadURL(v.url);
});
});
app.on("browser-window-created", (o) => {
// こっちで処理を開始すべき?
logger.log("browser-window-created");
});
app.on('window-all-closed', () => {
console.info("platformがdarwinの場合にapp.quitが呼ばれないが良いのか?");
// if (process.platform != 'darwin') {
app.quit();
// }
});
app.on('activate', () => {
if (mainWindow === null) {
createWindow();
}
});
}
function makeSingleInstance () {
if (process.mas) {
return
}
app.requestSingleInstanceLock()
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore()
}
mainWindow.focus();
}
})
}
process.on('uncaughtException', function(err) {
logger.error('electron:event:uncaughtException');
logger.error(err);
logger.error(err.stack);
app.quit();
});
// 実行開始
initialize();