-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrenderer.js
340 lines (278 loc) · 12 KB
/
renderer.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
let accounts = []
let iii = null
async function openLauncher() {
if (!await window.electron.jcefExists()) {
document.getElementById('loader-container').style.display = 'block';
await window.electron.downloadJcef()
}
if (!await window.electron.launcherExists()) {
document.getElementById('loader-container').style.display = 'block';
await window.electron.downloadMicrobotLauncher()
}
document.getElementById('loader-container').style.display = 'none';
await window.electron.openLauncher()
}
async function openClient(version) {
if (!await window.electron.clientExists()) {
document.getElementById('loader-container').style.display = 'block';
await window.electron.downloadClient(version)
}
const proxy = getProxyValues()
document.getElementById('loader-container').style.display = 'none';
// Get the select element by its ID
const selectElement = document.getElementById('character');
// Get the selected value
const selectedValue = selectElement.value;
const selectedAccount = accounts?.find(x => x.accountId === selectedValue)
if (selectedAccount) {
await window.electron.overwriteCredentialProperties(selectedAccount)
const selectedVersion = document.getElementById('client').value
const regex = selectedVersion.match(/\d+\.\d+\.\d+(\.\d+)?/);
if (regex) {
const version = regex[0];
console.log(version[0])
await window.electron.openClient(version, proxy)
} else {
await window.electron.openClient(selectedVersion, proxy)
}
} else {
alert('Account not found. Please restart your client.')
}
}
window.electron.ipcRenderer.receive('progress', (event, data) => {
if (data) {
updateProgress(data.percent, data.status);
}
});
function updateProgress(percent, status) {
const progressBar = document.getElementById('progress-bar');
const statusText = document.getElementById('status');
progressBar.style.width = percent + '%';
progressBar.textContent = percent + '%';
statusText.textContent = status;
}
async function handleJagexAccountLogic(clientVersion) {
setInterval(async () => {
const hasChanged = await window.electron.checkFileChange()
if (hasChanged) {
document.getElementById(('play')).innerHTML = 'Play With Jagex Account'
document.getElementById(('logout')).style.display = 'block'
document.getElementById(('add-accounts')).style.display = 'block'
accounts = await window.electron.readAccounts()
populateAccountSelector(accounts)
logoutButton()
addAccountsButton()
accounts = await window.electron.readAccounts()
populateAccountSelector(accounts)
populateSelectElement('client', [clientVersion + ".jar"])
document.getElementById(('logout')).style.display = 'block'
document.getElementById(('add-accounts')).style.display = 'block'
}
}, 1000)
}
window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
alert("Error occured: " + errorMsg + " - Version 1.0.5");//or any message
window.electron.logError(errorMsg)
return false;
}
window.addEventListener("error", function (e) {
if (e.error) {
alert("Error occured: " + e.error.stack + " - Version 1.0.5");//or any message
window.electron.logError(e.error.stack)
} else if (e.reason) {
alert("Error occured: " + e.reason.stack + " - Version 1.0.5");//or any message
window.electron.logError( e.reason.stack)
}
return false;
})
window.addEventListener("unhandledrejection", (event) => {
event.preventDefault(); // This will not print the error in the console });
alert("Error occured: " + event.reason.stack + " - Version 1.0.5");//or any message
window.electron.logError( event.reason.stack)
});
window.addEventListener('load', async () => {
const properties = await window.electron.readProperties()
const launcherVersion = await window.electron.fetchLauncherVersion() // jagex launcher version
const clientVersion = await window.electron.fetchClientVersion()
const launcherHtmlVersion = await window.electron.fetchLauncherHtmlVersion()
const microbotLauncherVersion = await window.electron.launcherVersion()
document.title = 'Microbot Launcher ' + microbotLauncherVersion
if (properties['launcher'] !== launcherVersion) {
document.getElementById('loader-container').style.display = 'block';
if (!await window.electron.jcefExists()) {
await window.electron.downloadJcef()
}
properties['launcher'] = launcherVersion
await window.electron.downloadMicrobotLauncher()
}
if (properties['client'] === '0.0.0') {
document.getElementById('loader-container').style.display = 'block';
properties['client'] = clientVersion
await window.electron.downloadClient(clientVersion)
}
if (properties['launcher_html'] !== launcherHtmlVersion) {
document.getElementById('loader-container').style.display = 'block';
properties['launcher_html'] = launcherHtmlVersion
await window.electron.downloadLauncherHtml()
}
document.getElementById('loader-container').style.display = 'none';
await window.electron.writeProperties(properties)
document.getElementById('play').addEventListener('click', async () => {
if (document.getElementById('play')?.innerText.toLowerCase() === 'Play With Jagex Account'.toLowerCase()) {
const proxy = getProxyValues()
const selectedVersion = document.getElementById('client').value
await openClient(selectedVersion, proxy)
} else {
await openLauncher()
}
})
//Init buttons and UI
await initUI(properties)
await checkForClientUpdate(properties)
iii = setInterval(async () => {
const properties = await window.electron.readProperties()
await checkForClientUpdate(properties)
}, 5 * 60 * 1000); // 5 minutes
await handleJagexAccountLogic(clientVersion);
document.querySelectorAll('.loadingButton').forEach(button => {
button.addEventListener('click', startLoading);
});
document.getElementById('website').src = "https://www.themicrobot.com?source=launcher"
});
function populateSelectElement(selectId, options) {
const selectElement = document.getElementById(selectId);
// Clear any existing options
selectElement.innerHTML = '';
// Add each option from the array to the select element
options.forEach(optionText => {
const optionElement = document.createElement('option');
optionElement.value = optionText;
optionElement.textContent = optionText;
selectElement.appendChild(optionElement);
});
}
function addSelectElement(selectId, option) {
// Get the select element by its ID
const selectElement = document.getElementById(selectId);
// Create a new option element
const newOption = document.createElement('option');
// Set the value and text of the new option
newOption.value = option;
newOption.text = option;
// Add the new option to the select element
selectElement.appendChild(newOption);
}
function populateAccountSelector(characters = []) {
// Get the select element by its ID
const characterSelect = document.getElementById('character');
// Clear any existing options (optional)
characterSelect.innerHTML = "";
// Iterate over the characters array and create option elements
characters.forEach(character => {
const option = document.createElement('option');
option.value = character.accountId; // Set sessionId as the value
option.textContent = character.displayName; // Set displayName as the text
characterSelect.appendChild(option); // Add the option to the select element
});
}
function logoutButton() {
const logoutBtn = document.getElementById('logout');
logoutBtn?.addEventListener('click', async () => {
const userConfirmed = confirm("Are you sure you want to proceed?");
if (!userConfirmed) return
await window.electron.removeAccounts()
document.getElementById(('play')).innerHTML = 'Login Jagex Account'
document.querySelector('#add-accounts').style = 'display:none'
document.querySelector('#logout').style = 'display:none'
accounts = []
populateAccountSelector([])
})
}
function addAccountsButton() {
const addAccounts = document.getElementById('add-accounts');
addAccounts?.addEventListener('click', async () => {
await openLauncher()
})
}
function playNoJagexAccount() {
document.querySelector('#play-no-jagex-account').addEventListener('click', async () => {
const proxy = getProxyValues()
const selectedVersion = document.getElementById('client').value
const regex = selectedVersion.match(/\d+\.\d+\.\d+(\.\d+)?/);
if (regex) {
const version = regex[0];
await window.electron.playNoJagexAccount(version, proxy)
} else {
await window.electron.playNoJagexAccount(selectedVersion, proxy)
}
})
}
function updateNowBtn() {
document.querySelector('#update-now-btn').addEventListener('click', async () => {
if (iii) clearInterval(iii)
document.querySelector('#update-available').style = 'display:none'
document.getElementById('loader-container').style.display = 'block';
const clientVersion = await window.electron.fetchClientVersion()
await window.electron.downloadClient(clientVersion)
addSelectElement('client', ['microbot-' + clientVersion + '.jar'])
const properties = await window.electron.readProperties()
properties['client'] = clientVersion
await window.electron.writeProperties(properties)
document.getElementById('loader-container').style.display = 'none';
})
}
function reminderMeLaterBtn() {
document.querySelector('#remind-me-later-btn').addEventListener('click', async () => {
document.querySelector('#update-available').style = 'display:none'
})
}
function getProxyValues() {
// Get the value of the proxy IP
var proxyIp = document.getElementById('proxy-ip').value;
// Get the selected value of the proxy type
var proxyType = document.getElementById('proxy-type').value;
return {
proxyIp,
proxyType,
}
}
function startLoading(event) {
const button = event.target;
button.classList.add('loading');
setTimeout(() => {
button.classList.remove('loading');
}, 1000);
}
async function setVersionPreference(properties) {
if (properties['version_pref'] && properties['version_pref'] !== '0.0.0') {
document.getElementById('client').value = properties['version_pref']
} else {
properties['version_pref'] = document.getElementById('client').value
await window.electron.writeProperties(properties)
}
document.getElementById('client').addEventListener('change', async (event) => {
const selectedValue = event.target.value
const properties = await window.electron.readProperties()
properties['version_pref'] = selectedValue
await window.electron.writeProperties(properties)
})
}
async function initUI(properties) {
updateNowBtn()
reminderMeLaterBtn()
playNoJagexAccount()
const listOfJars = await window.electron.listJars()
populateSelectElement('client', listOfJars)
await setVersionPreference(properties)
document.querySelector('.game-info').style = 'display:block'
}
async function checkForClientUpdate(properties) {
const clientVersion = await window.electron.fetchClientVersion()
const listOfJars = await window.electron.listJars()
if (properties['client'] !== clientVersion && listOfJars.every(file => file.indexOf(clientVersion) < 0)) {
document.querySelector('#update-available').style = 'display:block'
} else if (properties['client'] !== clientVersion) {
properties['client'] = clientVersion
await window.electron.writeProperties(properties)
}
}