-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
171 lines (147 loc) · 4.84 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
165
166
167
168
169
170
171
// include the Node.js 'path' module at the top of your file
const path = require('path')
const { app, BrowserWindow, Menu, nativeImage, session, Tray } = require('electron')
const { windowStateKeeper } = require("./stateKeeper")
// const isDevelopment = process.env.NODE_ENV !== "production";
const isDevelopment = require("electron-is-dev");
const { readFileSync } = require('fs');
// const iconPath = isDevelopment ? path.join('assets', 'icon.png') : path.resolve(app.getAppPath(), 'assets', 'icon.png');
const iconPath = path.join(
isDevelopment ? process.cwd() + "/resources" : process.resourcesPath,
"icon.ico"
)
console.log(`iconPath: ${iconPath}`)
const CALENDER_HOME = `https://calendar.google.com/calendar/u/0/r/agenda`
const setHomeCss = (mainWindow) => {
const stylesPath = path.join(
isDevelopment ? process.cwd() + "/resources" : process.resourcesPath,
"styles.css"
)
const styles = readFileSync(stylesPath).toString()
mainWindow.webContents.insertCSS(styles)
console.log(`CSS Set`)
}
// modify your existing createWindow() function
const createWindow = () => {
const mainWindow = new BrowserWindow({
height: 480,
width: 320,
maximizable: false,
minimizable: false,
icon: iconPath,
skipTaskbar: !isDevelopment,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
}
})
mainWindow.loadURL(CALENDER_HOME);
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
mainWindow.loadURL(url)
return { action: 'deny' };
});
mainWindow.webContents.on("will-navigate", (e, url) => {
console.log(`will-navigate`, url)
// if (url.includes(`https://accounts.google.com/CheckCookie`)) {
// setTimeout(() => {
// mainWindow.loadURL(CALENDER_HOME)
// }, 1500)
// }
if (url === CALENDER_HOME || url.includes(CALENDER_HOME)) {
setHomeCss(mainWindow)
}
})
setHomeCss(mainWindow)
windowStateKeeper('main')
.then((mwk) => {
if (mwk) {
const { x, y, width, height } = mwk;
if (x !== undefined && y !== undefined && width && height) {
mainWindow.setBounds({ x, y, width, height });
}
mwk.track(mainWindow);
}
})
.catch((e) => {
console.log(`Error in windowStateKeeper:`, e);
});
if (isDevelopment) {
mainWindow.webContents.openDevTools({ mode: "undocked" });
}
else {
mainWindow.setMenu(null)
app.setLoginItemSettings({
openAtLogin: true,
});
}
const isSingleInstance = app.requestSingleInstanceLock()
if (!isSingleInstance) {
app.quit()
mainWindow.focus()
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
})
}
createTray(mainWindow)
}
const createTray = (mainWindow) => {
// Load your tray icon image
const trayIcon = nativeImage.createFromPath(iconPath);
// Create the tray
const tray = new Tray(trayIcon);
// Create a context menu
const contextMenu = Menu.buildFromTemplate([
{
label: 'Reload', click: () => {
// mainWindow.reload()
// mainWindow.loadURL(CALENDER_HOME);
// setHomeCss(mainWindow)
app.relaunch()
app.exit()
}
},
{
label: 'Logout', click: () => {
session.defaultSession.clearStorageData()
.then(() => {
mainWindow.loadURL(CALENDER_HOME)
})
.catch((e) => {
console.log(`clearStorageData`, e)
})
}
},
{
label: 'Exit', click: () => {
app.quit()
}
}
]);
// Set the context menu for the tray
tray.setContextMenu(contextMenu);
// Add a tooltip (optional)
tray.setToolTip('GCW');
tray.setTitle("GCW")
// Show the tray icon
tray.on('click', () => {
mainWindow.show();
});
console.log(`Tray icon added`)
};
app.whenReady().then(() => {
createWindow()
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', () => {
// 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.
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});