This repository was archived by the owner on Feb 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathelectron-shell.js
More file actions
87 lines (72 loc) · 2.5 KB
/
Copy pathelectron-shell.js
File metadata and controls
87 lines (72 loc) · 2.5 KB
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
const path = require('path')
const url = require('url')
const Module = require('module')
const { app, shell } = require('electron')
const WindowManager = require('./WindowManager')
let win
function secureApplication() {
// FIXME replace with correct origin
let validOrigin = 'https://yourapp.com/'
app.on('web-contents-created', (event, contents) => {
// https://electronjs.org/docs/tutorial/security#11-verify-webview-options-before-creation
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(validOrigin)) {
event.preventDefault()
}
})
// https://electronjs.org/docs/tutorial/security#12-disable-or-limit-navigation
contents.on('will-navigate', (event, navigationUrl) => {
const parsedUrl = new URL(navigationUrl)
if (parsedUrl.origin !== validOrigin) {
event.preventDefault()
}
})
// https://electronjs.org/docs/tutorial/security#13-disable-or-limit-creation-of-new-windows
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.
event.preventDefault()
shell.openExternal(navigationUrl)
})
})
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
}
function createRenderer(clientUrl, options, args) {
// secureApplication()
app.on('web-contents-created', (event, contents) => {
contents.on('will-navigate', (event, navigationUrl) => {
const parsedUrl = new URL(navigationUrl)
// console.log('parsedUrl', parsedUrl)
if (parsedUrl.protocol === 'https:') {
shell.openExternal(navigationUrl)
}
event.preventDefault()
})
})
const loadRenderer = () => {
win = WindowManager.createWindow(options)
win.args = args
win.loadURL(clientUrl)
// win.webContents.openDevTools()
}
if (app.isReady()) {
loadRenderer()
} else {
app.once('ready', loadRenderer)
}
return win
}
module.exports = createRenderer