-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
228 lines (221 loc) · 5.41 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
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
const { default: axios } = require("axios");
const electron = require("electron");
const {
remote,
app,
Menu,
Tray,
BrowserWindow,
ipcMain,
dialog,
} = require("electron");
const ICON = __dirname + "/assets/images/aka.ico";
let tray = null;
let MainWin = null;
let InfoWin = null;
let LoginWin = null;
const Store = require("electron-store");
const store = new Store();
// Uncomment this if you want to use Hot reload in development mode
// try {
// require("electron-reloader")(module);
// } catch (_) {}
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
const gotTheLock = app.requestSingleInstanceLock();
function createMainWindow() {
// Main Window
MainWin = new BrowserWindow({
show: false,
frame: false,
resizable: true,
titleBarStyle: "customButtonsOnHover",
title: "CRYPTO-C by 剣",
roundedCorners: true,
autoHideMenuBar: true,
width: 250,
maxWidth: 250,
minWidth: 250,
height: 450,
minHeight: 450,
maxHeight: 800,
x: electron.screen.getPrimaryDisplay().bounds.width - 250,
y: (electron.screen.getPrimaryDisplay().bounds.height - 450) / 2,
skipTaskbar: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
devTools: false
},
});
MainWin.setAlwaysOnTop(true, "normal");
MainWin.setIcon(ICON);
MainWin.loadFile("html/index.html");
}
function createWindows() {
// Info Win
InfoWin = new BrowserWindow({
show: false,
frame: false,
resizable: false,
titleBarStyle: "customButtonsOnHover",
title: "CRYPTO-C by 剣",
roundedCorners: true,
autoHideMenuBar: true,
width: 600,
height: 400,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
devTools: false
},
});
InfoWin.setIcon(ICON);
InfoWin.loadFile("html/info.html");
// Login Win
LoginWin = new BrowserWindow({
show: false,
frame: false,
resizable: false,
titleBarStyle: "customButtonsOnHover",
title: "CRYPTO-C by 剣",
roundedCorners: true,
autoHideMenuBar: true,
width: 600,
height: 400,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
devTools: false
},
});
LoginWin.setIcon(__dirname + "/assets/images/login.png");
LoginWin.loadFile("html/login.html");
}
if (!gotTheLock) {
app.quit();
} else {
app.on("second-instance", (event, commandLine, workingDirectory) => {
MainWin.show();
});
ipcMain.on("asynchronous-message", (event, arg) => {
switch (arg) {
case "hide":
MainWin.hide();
break;
case "show-info":
InfoWin.show();
break;
case "close-info":
InfoWin.hide();
break;
case "close-app":
app.exit();
break;
}
});
ipcMain.on("login", function (event, data) {
setTimeout(() => {
CheckLogin(data)
.then((res) => {
if (res) {
LoginWin.webContents.send("login", "success");
LoginWin.hide();
createMainWindow();
MainWin.show();
RunApp();
store.set("token", data);
} else {
LoginWin.webContents.send("login", "Invalid Token");
}
})
.catch((err) => {
LoginWin.webContents.send("login", "Trying after 3 second");
});
}, 2000);
});
app.whenReady().then(() => {
// Create All Windows
createWindows();
setTimeout(() => {
axios
.get("https://www.google.com/")
.then(() => {
// Check if Login
CheckLogin(store.get("token")).then((res) => {
if (res === false) {
LoginWin.show();
} else {
createMainWindow();
RunApp();
}
});
})
.then(() => {
InfoWin.on("close", async (e) => {
e.preventDefault();
});
})
.catch(() => {
return dialog
.showMessageBox({
title: "There's no internet",
message: "No internet available",
type: "warning",
buttons: ["close"],
defaultId: 0,
})
.then(() => {
store.set("token", "");
app.exit();
});
});
}, 1000);
});
}
const RunApp = () => {
// === Run App ===
MainWin.show();
tray = new Tray(ICON);
const contextMenu = Menu.buildFromTemplate([
{ label: "Exit", type: "normal", click: () => app.exit() },
{
label: "Logout and Exit",
type: "normal",
click: () => {
store.set("token", "");
app.exit();
},
},
]);
// Set tray icon
tray.setToolTip("CRYPTO-C");
tray.setContextMenu(contextMenu);
tray.addListener("double-click", function () {
MainWin.show();
});
};
const CheckLogin = (token, checkErr = false) => {
return axios
.get(
`https://api.nomics.com/v1/currencies/ticker?key=${token}&ids=BTC&interval=1d,30d&convert=USD&per-page=100&page=1`
)
.then((res) => {
return true;
})
.catch((error) => {
if (error.response) {
if (error.response.status === 401) {
return false;
}
if (checkErr) {
if (error.response.status === 429) {
return 429;
}
}
}
return false;
});
};