forked from kreativgebiet/taskana
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
164 lines (133 loc) · 3.81 KB
/
index.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
import fs from 'node:fs';
import path from 'node:path';
import { BrowserWindow, Menu, app, ipcMain, shell } from 'electron';
import contextMenu from 'electron-context-menu';
import Store from 'electron-store';
import electronUpdater from 'electron-updater';
import menu from './menu/index.js';
const config = new Store();
const BASE_URL = 'https://app.asana.com/';
const notificationIndicator = '●';
let isQuitting = false;
let mainWindow;
let page;
function basicURL(url) {
if (typeof url !== 'string') return false;
const parsed = new URL(url);
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:')
return false;
return true;
}
function isURLAllowed(url) {
return [
/^https:\/\/accounts\.google\.com\/.*/i,
/^https:\/\/app\.asana\.com\/.*/i,
/^https:\/\/asana-user-private-us-east-1\.s3\.amazonaws\.com\/.*/i,
/^https:\/\/.*\.doubleclick.net\/.*/i,
].some((re) => url.match(re));
}
function updateBadgeInfo(title) {
if (process.platform !== 'darwin' && process.platform !== 'linux') return;
if (title.startsWith(notificationIndicator)) {
app.dock.setBadge(notificationIndicator);
} else {
app.dock.setBadge('');
}
}
function createMainWindow() {
contextMenu();
const opts = {
title: app.getName(),
width: 1200,
height: 600,
show: false,
acceptFirstMouse: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
sandbox: false, // needed to allow "require" in preload script - as per https://github.com/electron/electron/issues/35587#issuecomment-1238940105
preload: path.resolve(app.getAppPath(), 'browser.cjs'),
partition: 'persist:asana',
spellcheck: true,
},
};
Object.assign(opts, config.get('winBounds'));
const win = new BrowserWindow(opts);
win.loadURL(BASE_URL);
win.on('close', (e) => {
if (!isQuitting) {
e.preventDefault();
if (process.platform === 'darwin') {
app.hide();
} else {
win.hide();
}
} else {
// save window size and position
config.set('winBounds', win.getBounds());
}
});
win.on('page-title-updated', (e, title) => {
e.preventDefault();
updateBadgeInfo(title);
});
return win;
}
if (!app.requestSingleInstanceLock()) {
app.quit();
}
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.show();
}
});
ipcMain.on('update-menu', () => {
Menu.setApplicationMenu(menu.slice(1));
});
app.on('before-quit', () => {
isQuitting = true;
});
app.on('ready', () => {
mainWindow = createMainWindow();
page = mainWindow.webContents;
electronUpdater.autoUpdater.checkForUpdatesAndNotify();
// Open new browser window on external open
page.setWindowOpenHandler(({ url }) => {
if (basicURL(url)) shell.openExternal(url);
return { action: 'deny' }; // prevent a new Electron window
});
page.on('will-navigate', (e, url) => {
if (basicURL(url) && !isURLAllowed(url)) {
e.preventDefault();
shell.openExternal(url);
}
});
page.on('will-redirect', (e, url) => {
// `will-navigate` doesn't catch redirects
if (basicURL(url) && !isURLAllowed(url)) {
e.preventDefault();
mainWindow.loadURL(BASE_URL);
shell.openExternal(url);
}
});
// Set the menu application menu
Menu.setApplicationMenu(menu);
// Insert CSS
page.on('dom-ready', () => {
page.insertCSS(fs.readFileSync('browser.css', 'utf8'));
mainWindow.show();
});
});
// Quit when all windows are closed, except on macOS. There, it's common for applications and their menu bar to stay active until the user quits explicitly with Cmd + Q
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// On OS X it's common to re-create a window in the app when the dock icon is clicked and there are no other windows open
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createMainWindow();
}
});