-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
135 lines (115 loc) · 3.64 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
process.on('uncaughtException', function (err) {
console.error('>>>>> note ', err)
})
const { app, BrowserWindow, shell, ipcMain } = require('electron')
const glob = require('glob')
const path = require('path')
let win = null
const windowConfig = {
width: 1000,
height: 800,
minWidth: 320,
webPreferences: {
// contextIsolation: true, // 为远程内容开启上下文隔离
// nodeIntegration: false, // 禁止Node.js集成远程内容
// allowRunningInsecureContent: true // 允许https站点执行http脚本, 不推荐
// experimentalFeatures: true // 开启实验室特性, 不推荐
// // preload: './preload.js'
}
}
function initialize () {
makeSingleInstance()
function createWindow () {
win = new BrowserWindow(windowConfig)
// win.loadURL(`file://${__dirname}/bookstore/index.html`)
win.loadURL(`http://www.baidu.com`)
// 开启调试工具
// win.webContents.openDevTools();
win.once('ready-to-show', () => {
win.show()
})
win.on('close', () => {
// 回收BrowserWindow对象
win = null
})
win.on('resize', () => {
// win.reload();
})
win.on('closed', () => {
win = null
})
}
app.on('ready', () => {
createWindow()
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (win == null) {
createWindow()
}
})
// 强调一下,这份列表只是将风险降到最低,并不会完全屏蔽风险。 如果您的目的是展示一个网站,浏览器将是一个更安全的选择。
app.on('web-contents-created', (event, contents) => {
// 创建WebView前确认其选项
contents.on('will-attach-webview', (event, webPreferences, params) => {
// Strip away preload scripts if unused or verify their location is legitimate
delete webPreferences.preload
delete webPreferences.preloadURL
// Disable Node.js integration
webPreferences.nodeIntegration = false
// Verify URL being loaded
if (!params.src.startsWith('https://yourapp.com/')) {
event.preventDefault()
}
})
// 链接http开头的,直接用浏览器打开
contents.on('new-window', (event, navigationUrl) => {
// In this example, we'll ask the operating system
// to open this event's url in the default browser.
console.log('>>>> test', navigationUrl)
if (navigationUrl.indexOf('http') === 0) {
event.preventDefault()
shell.openExternal(navigationUrl)
}
})
// Disable or limit navigation
contents.on('will-navigate', (event, navigationUrl) => {
const parsedUrl = new URL(navigationUrl)
if (parsedUrl.origin !== 'https://my-own-server.com') {
event.preventDefault()
shell.openExternal(navigationUrl)
}
})
})
}
// Make this app a single instance app.
//
// The main window will be restored and focused instead of a second window
// opened when a person attempts to launch a second instance.
//
// Returns true if the current version of the app should quit instead of
// launching.
function makeSingleInstance () {
if (process.mas) return
app.requestSingleInstanceLock()
app.on('second-instance', () => {
if (win) {
if (win.isMinimized()) win.restore()
win.focus()
}
})
}
initialize()
function loadDemos () {
const files = glob.sync(path.join(__dirname, 'main-process/**/*.js'))
files.forEach((file) => { require(file) })
}
loadDemos()
if (process.platform === 'darwin') {
// app.setBadgeCount(10)
app.dock.bounce('critical') // macOS 会在激活前跳一直跳
}