-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelectron.js
166 lines (149 loc) · 3.89 KB
/
electron.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
const electron = require("electron");
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const Menu = electron.Menu;
const session = electron.session;
const protocol = electron.protocol;
const packageJson = require('./package.json');
const fs = require('fs')
const path = require("path");
const isDev = require("electron-is-dev");
const isMac = process.platform === 'darwin';
const template = [
...(isMac ? [{
label: app.name,
submenu: [
{role: 'about'},
{type: 'separator'},
{role: 'services'},
{type: 'separator'},
{role: 'hide'},
{role: 'hideothers'},
{role: 'unhide'},
{type: 'separator'},
{role: 'quit'}
]
}] : []),
{
label: 'File',
submenu: [
isMac ? {role: 'close'} : {role: 'quit'}
]
},
{
label: 'Edit',
submenu: [
{role: 'undo'},
{role: 'redo'},
{type: 'separator'},
{role: 'cut'},
{role: 'copy'},
{role: 'paste'},
...(isMac ? [
{role: 'pasteAndMatchStyle'},
{role: 'delete'},
{role: 'selectAll'},
{type: 'separator'},
{
label: 'Speech',
submenu: [
{role: 'startSpeaking'},
{role: 'stopSpeaking'}
]
}
] : [
{role: 'delete'},
{type: 'separator'},
{role: 'selectAll'}
])
]
},
{
label: 'View',
submenu: [
{role: 'reload'},
{role: 'forceReload'},
{role: 'toggleDevTools'},
{type: 'separator'},
{role: 'resetZoom'},
{role: 'zoomIn'},
{role: 'zoomOut'},
{type: 'separator'},
{role: 'togglefullscreen'}
]
},
{
role: 'help',
submenu: [
{
label: 'Source Code',
click: async () => {
const {shell} = require('electron');
await shell.openExternal('https://github.com/lfuelling/pds-viewer');
}
}
]
}
]
let mainWindow;
protocol.registerSchemesAsPrivileged([{
scheme: 'pds-viewer',
privileges: {standard: true, secure: true, corsEnabled: true}
}]);
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {webSecurity: false}
});
mainWindow.loadURL(
isDev
? "http://localhost:1234"
: `pds-viewer://serve/`
).then(() => {
isDev && mainWindow.openDevTools();
});
mainWindow.on("closed", () => (mainWindow = null));
}
app.on("ready", () => {
app.setName("PDS Viewer");
protocol.registerFileProtocol('pds-viewer', (request, callback) => {
const url = request.url.substr(18);
console.log('Url:', url);
const filePath = path.join(__dirname, "./dist/", url);
if (fs.existsSync(filePath) && !fs.lstatSync(filePath).isDirectory()) {
console.log('Returning file...');
callback({path: filePath});
} else {
console.log('Returning index...');
callback({path: path.join(__dirname, "./dist/index.html")});
}
});
app.setAboutPanelOptions({
applicationName: "PDS Viewer",
applicationVersion: packageJson.version,
authors: packageJson.author
});
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
details.requestHeaders['User-Agent'] = 'Mozilla/5.0 (compatible; PDS Viewer/' + packageJson.version + ')';
details.requestHeaders['Sec-Fetch-Dest'] = undefined;
details.requestHeaders['Sec-Fetch-Mode'] = undefined;
details.requestHeaders['Sec-Fetch-Site'] = undefined;
details.requestHeaders['Origin'] = undefined;
details.requestHeaders['Referer'] = undefined;
callback({cancel: false, requestHeaders: details.requestHeaders});
});
createWindow();
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (mainWindow === null) {
createWindow();
}
});