-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
97 lines (89 loc) · 2.93 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
const { app, BrowserWindow, ipcMain, Menu, Tray, dialog } = require('electron')
const path = require('path')
const package = require('./package.json')
// 清除启动时控制台的“Electron Security Warning (Insecure Content-Security-Policy)”报错信息
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
let win;
const Store = require('electron-store');
Store.initRenderer();
app.whenReady().then(() => {
createTray();
createWindow();
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
const createWindow = () => {
Menu.setApplicationMenu(null);
win = new BrowserWindow({
width: 800,
height: 600,
resizable: false,
icon: path.join(__dirname, './src/logo.png'),
webPreferences: {
preload: path.join(__dirname, './src/preload.js'),
spellcheck: false
},
useContentSize: true
})
win.loadFile('src/index.html')
}
let tray;
const createTray = () => {
tray = new Tray(path.join(__dirname, './src/logo.png'));
const menu = Menu.buildFromTemplate(trayMenuTemplate);
tray.setContextMenu(menu);
}
const trayMenuTemplate = [{
label: 'About',
type: 'normal',
click: function() {
dialog.showMessageBox({
type: 'info',
title: '关于',
message: package.name + ':' + package.version + '\n' + package.description + '\nnode:' + process.versions['node'] + '\nchrome:' + process.versions['chrome'] + '\nelectron:' + process.versions['electron']
});
}
}, {
label: 'Project Repository',
type: 'normal',
click: function() {
let exec = require('child_process').exec
, locale = app.getLocale()
, url = ''
// 使用ip的话要么自己维护一个ip库放在外部(太大,没必要放项目里),要么使用第三方,都需要进行网络交互
// 所以这里使用一个最粗略的方式“语言环境”来判断是否是中国大陆
if(locale.indexOf('zh-CN') == -1) {
url = 'https://github.com/rexlevin/hostsbox.git'
} else {
url = 'https://gitee.com/rexlevin/hostsbox.git'
}
exec('open ' + url)
}
}, {
label: 'Quit',
type: 'normal',
click: function() {
app.quit();
}
}];
// 获取app数据目录
ipcMain.on('userdata-message', (event, args) => {
// console.info('userData====' + app.getPath('userData'));
event.reply('userdata-reply', app.getPath('userData'))
});
ipcMain.on('devTools', () => {
if(win.webContents.isDevToolsOpened()) win.webContents.closeDevTools();
else win.webContents.openDevTools();
});
ipcMain.on('reload', () => {
win.reload();
// win.webContents.reload();
});
// 退出app
ipcMain.on('exit', (event, args) => {
app.quit();
});
ipcMain.on('title', (e, arg) => {
e.reply('title-reply', package.name + ' - ' + package['description'] + ' - v' + package.version);
});