-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.js
251 lines (232 loc) · 7.43 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import "regenerator-runtime/runtime"; // To be able to use async-await syntax
const { app, BrowserWindow, ipcMain, shell, session } = require("electron");
const path = require("path");
const url = require("url");
const axios = require("axios");
import checkCookieValidity from "./src/api/CheckCookieValidity";
import makeCookie from "./src/api/makeCookie";
import { API_URL, TIME_OUT_LIMIT } from "./src/Constants";
// for DEV purpose only
import smallResponse from "./src/DummyData/smallResponse";
// init mainWindow of the Application
let mainWindow, cookieJar;
const createWindow = () => {
mainWindow = new BrowserWindow({
width: 1000,
height: 800,
icon: __dirname + "/src/static/images/icon.png",
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
autoHideMenuBar: true,
});
cookieJar = session.defaultSession.cookies;
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, "/index.html"),
protocol: "file:",
slashes: true,
})
);
// TODO: Add Ctrl+W, shortcut for Windows if not Available.
mainWindow.on("close", () => {
mainWindow = null;
});
// To make clicked links open with default browser
mainWindow.webContents.on("new-window", (e, url) => {
e.preventDefault();
shell.openExternal(url);
});
};
// Catch handle-form event
ipcMain.on("handle-form", async (e, cookie) => {
cookie = cookie.trim();
console.log("Server: Received handle-form event with cookie:", cookie);
const cookieHeaderString = `LEETCODE_SESSION=${cookie};`;
try {
const response = await checkCookieValidity(cookieHeaderString);
console.log(
"IDHAR:",
response.message.wasCookieSent,
typeof response.message.wasCookieSent
);
response.message.wasCookieSent = true;
// Store this Cookie
cookieJar
.set(makeCookie(cookie))
.then(() => {
console.log("Cookies Set.");
cookieJar
.get({
url: "https://leetsolve.matrix.com",
name: "MY_TASTY_COOKIE",
})
.then((cookies) => {
console.log("Showing All Cookies now:");
cookies.map((cookie) => {
console.log(
"here is it:",
cookie,
cookie.expirationDate
);
});
console.log(cookies.length);
})
.catch((err) => {
console.log("ERROR: While Getting All Cookie Values");
});
mainWindow.webContents.send("user-data", response);
})
.catch((err) => {
console.log("ERROR: While Setting Cookie.", err);
});
} catch (response) {
response.message.wasCookieSent = true;
// Do not Store this Cookie
console.log(
"This Cookie is either invalid or expired./Req Timed Out",
response
);
mainWindow.webContents.send("user-data", response);
}
});
// Catch user-data event emitted by Client
ipcMain.on("user-data", async (e) => {
// TESTING BEGINS
// cookieJar
// .set(makeCookie(<ENTER_YOUR_COOKIE_HERE>))
// .then(() => {
// console.log("Cookies Set.");
// })
// .catch((err) => {
// console.log("Can't Set");
// });
// To remove a cookie: Will be used in logout, inside a different even i.e. user-logout
// try {
// await cookieJar.remove(
// "https://leetsolve.matrix.com",
// "MY_TASTY_COOKIE"
// );
// console.log("Cookie Removed");
// } catch (e) {
// console.log("Problem Removing the cookie", e);
// }
// TESTING ENDS
cookieJar
.get({ url: "https://leetsolve.matrix.com", name: "MY_TASTY_COOKIE" })
.then(async (cookies) => {
if (cookies.length !== 0) {
const cookieObj = cookies[0];
console.log("Yea... Your Cookie is Here.");
const cookieHeaderString = `LEETCODE_SESSION=${cookieObj.value};`;
try {
const response = await checkCookieValidity(
cookieHeaderString
);
mainWindow.webContents.send("user-data", response);
} catch (response) {
if (response.message.isTimeOut) {
console.log(
"We ran out of time while fetching data for your EXISTING cookie",
response
);
} else {
console.log(
"That Sounds Weird but I will allow it.",
response
);
}
mainWindow.webContents.send("user-data", response);
}
} else {
console.log("ERROR: Can't Find Your Cookie");
let response = {
message: {
username: "",
isTimeOut: false,
doesCookieExist: false,
isCookieValid: false,
wasCookieSent: false,
},
data: {},
};
mainWindow.webContents.send("user-data", response);
}
})
.catch((err) => {
console.log("ERROR: While Getting Cookie.");
});
// let response = {
// message: { username: "", isCookieValid: false, isTimeOut: false },
// data: {},
// };
/**
* For Faster Dev of Feature, instead of making axios get req every time
* use Promise.resolve() version.
*/
// axios
// .get(API_URL, {
// timeout: 1000 * TIME_OUT_LIMIT,
// headers: { cookie: cookieHeaderString },
// })
// .then((res) => {
// console.log("Response Returned by Leetcode.");
// response = { ...response, data: { ...res.data } };
// response.message.username = res.data.user_name;
// if (response.message.username === "") {
// console.log("This Cookie is either invalid or expired.");
// response.message.isCookieValid = false;
// response.message.isTimeOut = false;
// mainWindow.webContents.send("user-data", response);
// } else {
// console.log("Hi", response.message.username);
// response.message.isCookieValid = true;
// response.message.isTimeOut = false;
// mainWindow.webContents.send("user-data", response);
// }
// })
// .catch((err) => {
// console.log("May be Request took long time to reply...", err);
// response.message.isTimeOut = true;
// response.message.isCookieValid = true; // Doesn't matter when isTimeOut = true
// mainWindow.webContents.send("user-data", response);
// });
/**
* For Faster Dev of Feature, instead of making axios get req every time
* use below version.
*/
// Promise.resolve()
// .then(() => {
// response = { ...response, data: { ...smallResponse } };
// const res = { data: { user_name: "Tivan" } };
// console.log("Response Returned by Leetcode.");
// response = { ...response, ...res.data };
// response.message.username = res.data.user_name;
// if (response.message.username === "") {
// console.log("This Cookie is either invalid or expired.");
// response.message.isCookieValid = false;
// response.message.isTimeOut = false;
// mainWindow.webContents.send("user-data", response);
// } else {
// console.log("Hi", response.message.username);
// response.message.isCookieValid = true;
// response.message.isTimeOut = false;
// mainWindow.webContents.send("user-data", response);
// }
// })
// .catch((err) => {
// console.log("May be Request took long time to reply...", err);
// response.message.isTimeOut = true;
// response.message.isCookieValid = true; // Doesnot matter when isTimeOut = true
// mainWindow.webContents.send("user-data", response);
// });
});
// Spawning the Main Window
app.on("ready", createWindow);
// FOR-MAC-USERS: Quit when all windows are closed
app.on("window-all-closes", () => {
if (process.platform != "darwin") {
app.quit();
}
});