-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelectron.js
More file actions
89 lines (78 loc) · 2.42 KB
/
Copy pathelectron.js
File metadata and controls
89 lines (78 loc) · 2.42 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
88
89
const { app, BrowserWindow, protocol, net, Menu, nativeImage, shell } = require('electron');
const path = require('path');
const fs = require('fs');
Menu.setApplicationMenu(null);
protocol.registerSchemesAsPrivileged([{
scheme: 'app',
privileges: {
standard: true,
secure: true,
supportFetchAPI: true
}
}]);
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600,
icon: nativeImage.createFromPath(path.join(__dirname, 'files/icon.png')),
webPreferences: {
contextIsolation: false,
},
show: false,
});
win.loadURL('app:///index.html');
win.maximize();
win.show();
};
app.whenReady().then(() => {
protocol.handle('app', (request) => {
let p = request.url.slice('app://'.length).replace(/^[^/]*/, '');
p = p.split('?')[0].split('#')[0];
if (!p || p === '/') p = '/index.html';
let fullPath = path.join(__dirname, p);
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
fullPath = path.join(fullPath, 'index.html');
} else if (!path.extname(p)) {
fullPath += '.html';
}
return net.fetch('file://' + fullPath);
});
app.on('web-contents-created', (event, contents) => {
contents.setWindowOpenHandler(({ url }) => {
if (url.startsWith('app://')) {
BrowserWindow.fromWebContents(contents)?.loadURL(url);
} else {
shell.openExternal(url);
}
return { action: 'deny' };
});
contents.on('will-navigate', (event, url) => {
if (!url.startsWith('app://')) {
event.preventDefault();
shell.openExternal(url);
}
});
contents.on('did-finish-load', () => {
contents.executeJavaScript(`
new Promise(resolve => {
const check = () => {
const href = document.querySelector('link[rel="icon"]')?.href || '';
if (href) return resolve(href);
setTimeout(check, 100);
};
check();
})
`).then(href => {
if (!href) return;
const win = BrowserWindow.fromWebContents(contents);
if (!win) return;
const localPath = href.startsWith('app://')
? path.join(__dirname, href.slice('app://'.length).replace(/^[^/]*/, ''))
: path.join(__dirname, 'files/icon.png');
const icon = nativeImage.createFromPath(localPath);
if (!icon.isEmpty()) win.setIcon(icon);
});
});
});
createWindow();
});