-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsettings.js
469 lines (412 loc) · 14.9 KB
/
settings.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
const SaveBtn = document.getElementById('Save');
const Elements = Array.from(document.getElementsByClassName('setting-container'));
var RecentSave;
var Settings;
var Utilities;
(async () => {
Utilities = await import(chrome.runtime.getURL('resources/utils.js'));
Utilities = Utilities.default;
LoadCurrent();
document.getElementById('PinnedGames-limit').innerText = Utilities.Limits.PinnedGames;
//document.getElementById('ImprovedFrLists-limit').innerText = Utilities.Limits.ImprovedFrLists;
//document.getElementById('ItemWishlist-limit').innerText = Utilities.Limits.ItemWishlist;
document.getElementById('HoardersList-pageLimit').innerText = Utilities.Limits.HoardersListPages;
})();
// Handle buttons at the bottom of the page
document.getElementById('ResetDefaults').addEventListener('click', function () {
document.getElementById('ResetDefaults-Modal').showModal();
});
SaveBtn.addEventListener('click', Save);
// Handle modal buttons for Reset Defaults modal
document.getElementById('ResetDefaults-Modal-Yes').addEventListener('click', function () {
Settings = Utilities.DefaultSettings;
Save();
setTimeout(function () {
LoadCurrent();
document.getElementById('ResetDefaults-Modal').close();
}, 400);
});
document.getElementById('ResetDefaults-Modal-No').addEventListener('click', function () {
document.getElementById('ResetDefaults-Modal').close();
});
// Loop thru each setting container and handle toggling, selecting, opening modal, etc
Elements.forEach((element) => {
let Button = element.getElementsByClassName('toggle-btn')[0];
let Options = element.getElementsByClassName('options-btn')[0];
let Select = element.getElementsByTagName('select')[0];
let Checkbox = element.getElementsByTagName('input');
if (Button) {
Button.addEventListener('click', function () {
SetSetting(Button, 'bool');
});
}
if (Select) {
Select.addEventListener('change', function () {
if (Select.getAttribute('data-useValue') !== null) {
let Value = Select.options[Select.selectedIndex].value;
if (!isNaN(Value)) {
Value = parseInt(Value);
}
SetSetting(Select, Value, false);
} else {
SetSetting(Select, Select.selectedIndex, false);
}
});
}
if (Checkbox) {
Array.from(Checkbox).forEach(check => {
check.addEventListener('change', function () {
SetSetting(check, check.checked, false);
});
})
}
if (Options) {
const Modal = document.getElementById(Options.getAttribute('data-modal') + '-Modal');
const ModalButtons = Modal.getElementsByTagName('button');
const ModalInputs = Modal.getElementsByTagName('input');
const ModalSelect = Modal.getElementsByTagName('select');
Options.addEventListener('click', function () {
Array.from(ModalButtons)
.filter((x) => !x.classList.contains('ignore'))
.forEach((button) => {
button.addEventListener('click', function () {
const Setting = button.getAttribute('data-setting');
if (Setting === '[save]') {
// Save Modal Button
// Save Modal Inputs
Array.from(ModalInputs)
.filter((x) => !x.classList.contains('ignore'))
.forEach((input) => {
SetSetting(input, input.value, false, Modal.getAttribute('data-setting'));
});
// Save Modal Select Menus
Array.from(ModalSelect)
.filter((x) => !x.classList.contains('ignore'))
.forEach((select) => {
SetSetting(select, select.selectedIndex, false, Modal.getAttribute('data-setting'));
});
Save();
setTimeout(function () {
LoadCurrent();
Modal.close();
}, 400);
} else if (Setting === '[reset-default]') {
// Reset to Defaults Modal Button
if (confirm("Are you sure you'd like to reset these options to their defaults?") === true) {
Settings[Modal.getAttribute('data-setting')] = Utilities.DefaultSettings[Modal.getAttribute('data-setting')];
Save();
Modal.close();
}
} else if (Setting === '[cancel]') {
// Cancel Changes Button
Modal.close();
} else {
// Default Toggle Button
SetSetting(button, 'bool', false, Modal.getAttribute('data-setting'));
}
});
});
Array.from(ModalInputs)
.filter((x) => !x.classList.contains('ignore'))
.forEach((input) => {
const Status = GetSettingValue(input, Modal.getAttribute('data-setting'));
if (Status !== 'undefined' && Status !== undefined) {
input.value = Status;
} else {
input.value = '';
}
});
Array.from(ModalSelect)
.filter((x) => !x.classList.contains('ignore'))
.forEach((select) => {
const Status = GetSettingValue(select, Modal.getAttribute('data-setting'));
if (Status !== 'undefined' && Status !== undefined) {
select.selectedIndex = Status;
}
});
Modal.showModal();
});
}
});
function LoadCurrent() {
chrome.storage.sync.get(['PolyPlus_Settings'], function (result) {
Settings = Utilities.MergeObjects(result.PolyPlus_Settings || Utilities.DefaultSettings, Utilities.DefaultSettings);
RecentSave = structuredClone(Settings)
console.log('Current Settings: ', Settings);
Elements.forEach((element) => {
UpdateElementState(element);
});
});
}
function SetSetting(element, value, update, modalParent) {
document.title = '*unsaved | Poly+ Settings'
const name = element.getAttribute('data-setting');
let parent = element.getAttribute('data-parent');
if (modalParent !== undefined) {
console.log(modalParent);
parent = modalParent;
}
if (value === 'bool') {
value = !GetSettingValue(element, modalParent);
}
if (parent !== null) {
let Parent = Object.values(Settings)[Object.keys(Settings).indexOf(parent)];
if (!isNaN(element.getAttribute('data-parent')) && element.getAttribute('data-parent') !== null) {
Parent = Parent[parseInt(element.getAttribute('data-parent'))];
}
Parent[name] = value;
} else {
Settings[name] = value;
}
if (update !== false) {
UpdateElementState(document.querySelector(`.setting-container:has([data-setting="${name}"])${parent !== null ? `:has([data-parent="${parent}"])` : ''}`), value);
}
if (SaveBtn.getAttribute('disabled')) {
SaveBtn.removeAttribute('disabled');
// Handle leaving the settings page before saving
window.onbeforeunload = function (e) {
return "Are you sure you'd like to leave? Your Poly+ settings haven't been saved."
};
}
/*
if (AreIdentical(Settings, RecentSave) === true) {
document.title = 'Poly+ Settings'
SaveBtn.disabled = true
}
*/
}
function GetSettingValue(element, modalParent) {
const name = element.getAttribute('data-setting');
let parent = element.getAttribute('data-parent');
if (modalParent !== undefined) {
parent = modalParent;
}
let Status = name;
if (parent !== null) {
let Parent = Object.values(Settings)[Object.keys(Settings).indexOf(parent)];
if (!isNaN(element.getAttribute('data-parent')) && element.getAttribute('data-parent') !== null) {
Parent = Parent[parseInt(element.getAttribute('data-parent'))];
Status = Parent[name];
} else {
Status = Object.values(Parent)[Object.keys(Parent).indexOf(name)];
}
} else {
Status = Settings[Status];
}
if (element.tagName === 'SELECT' && element.getAttribute('data-useValue') === 'true') {
Status = Array.from(element.children).indexOf(element.querySelector('option[value="' + Status + '"]'))
}
return Status;
}
function UpdateElementState(element, status) {
const Button = element.getElementsByClassName('toggle-btn')[0];
if (status === undefined) {
status = GetSettingValue(Button);
}
if (status === true) {
element.classList.add('enabled');
element.classList.remove('disabled');
Button.innerText = 'Disable';
Button.classList.add('btn-danger');
Button.classList.remove('btn-success');
} else {
element.classList.add('disabled');
element.classList.remove('enabled');
Button.innerText = 'Enable';
Button.classList.add('btn-success');
Button.classList.remove('btn-danger');
}
let SelectInput = element.getElementsByTagName('select')[0];
if (SelectInput) {
SelectInput.selectedIndex = GetSettingValue(SelectInput);
}
let Checkbox = Array.from(element.getElementsByTagName('input'));
if (Checkbox.length > 0) {
Checkbox.forEach((check) => {
check.checked = GetSettingValue(check);
});
}
}
function Save() {
document.title = 'Poly+ Settings';
SaveBtn.setAttribute('disabled', 'true');
chrome.storage.sync.set({PolyPlus_Settings: Settings}, function () {
console.log('Saved successfully!');
RecentSave = Settings
});
// Handle leaving the settings page after saving
window.onbeforeunload = null
console.log('Save:', Settings);
}
let LoadThemeFromJSONBtn = document.getElementById('LoadThemeFromJSONBtn');
let SaveThemeToJSONInput = document.getElementById('SaveThemeToJSONInput');
let CopyThemeJSONBtn = document.getElementById('CopyThemeJSONBtn');
LoadThemeFromJSONBtn.addEventListener('click', function () {
LoadThemeJSON(LoadThemeFromJSONBtn.previousElementSibling.value);
});
document
.getElementById('theme-creator')
.getElementsByTagName('button')[1]
.addEventListener('click', function () {
SaveThemeToJSONInput.value = JSON.stringify(Settings.ThemeCreator);
});
CopyThemeJSONBtn.addEventListener('click', function () {
if (SaveThemeToJSONInput.value.length > 0) {
navigator.clipboard
.writeText(SaveThemeToJSONInput.value)
.then(() => {
alert('Successfully copied theme data to clipboard!');
})
.catch(() => {
alert('Failure to copy theme data to clipboard.');
});
}
});
function LoadThemeJSON(string) {
try {
let JSONTable = JSON.parse(string);
if (JSONTable.length === Utilities.DefaultSettings.ThemeCreator.length) {
if (confirm("Are you sure you'd like to replace this theme with the theme specified in the JSON?") === true) {
LoadThemeFromJSONBtn.previousElementSibling.value = '';
document.getElementById('ThemeCreator-Modal').close();
for (let i = 0; i < JSONTable.length; i++) {
if (JSONTable[i][0] !== '#') {
JSONTable[i] = '';
}
}
Settings.ThemeCreator = Utilities.MergeObjects(JSONTable, Utilities.DefaultSettings.ThemeCreator);
Save();
console.log(JSONTable.length, JSONTable, 'applied');
document.getElementById('ThemeCreator').getElementsByTagName('button')[1].click();
}
} else {
alert('JSON is not a theme!');
}
} catch (error) {
alert('JSON is invalid!');
}
}
chrome.storage.sync.get(['PolyPlus_AutoAds'], function(result){
let AutoAds = result.PolyPlus_AutoAds || [];
const Modal = document.getElementById("AutoAdBidding-Modal")
const AddButton = document.getElementById('auto-ad-bidding-add')
AddButton.addEventListener('click', async function() {
const Page = new DOMParser().parseFromString((await (await fetch('https://polytoria.com/create/ad/' + AddButton.previousElementSibling.value)).text()), 'text/html')
})
const AddRow = function(index, info) {
const Row = document.createElement('tr')
Row.innerHTML = `
<th scope="row">${index+1}</th>
<td><a href="https://polytoria.com/create/ad/${info.id}">"${info.name}"</a></td>
<td class="text-success"><span class="pi">$</span> 150</td>
<td>
<select class="form-select ignore">
<option value="daily" selected>Daily</option>
<option value="weekly">Weekly</option>
<option value="monthly">Monthly</option>
</select>
</td>
<td>
<div role="group" class="btn-group w-100">
<button class="btn btn-success w-25">
BID
</button>
<button class="btn btn-orange w-25">
EDIT
</button>
<button class="btn btn-danger w-25">
DELETE
</button>
</div>
</td>
`
}
})
function AreIdentical(obj1, obj2) {
if (obj1.length !== obj2.length) { return false }
return JSON.stringify(obj1) === JSON.stringify(obj2)
}
function FormatBool(bool) {
if (bool === true) {
return 'enabled';
} else {
return 'disabled';
}
}
const Manifest = chrome.runtime.getManifest();
let BuildType = 'Stable';
if (Manifest.version_name !== undefined) {
BuildType = 'Pre-Release';
}
const FooterText = document.getElementById('footer-text');
FooterText.children[0].innerHTML = `Version: v${Manifest.version} | Build Type: ${BuildType}`;
const CheckForUpdatesButton = document.getElementById('check-for-updates');
function CheckForUpdates() {
CheckForUpdatesButton.removeEventListener('click', CheckForUpdates);
CheckForUpdatesButton.disabled = true;
fetch('https://polyplus.vercel.app/data/version.json')
.then((response) => {
if (!response.ok) {
throw new Error('Network not ok');
}
return response.json();
})
.then((data) => {
if (data.version === Manifest.version || Math.floor((data.version - Manifest.version) * 10) === 0) {
CheckForUpdatesButton.innerHTML = '<b>No updates available</b>';
alert('No updates available');
} else {
const NumberOfUpdatesAvailable = Math.floor((data.version - Version) * 10);
CheckForUpdatesButton.innerHTML = '<b>' + NumberOfUpdatesAvailable + ' update(s) available</b>';
alert(NumberOfUpdatesAvailable + ' updates available');
}
})
.catch((error) => {
console.log(error);
});
}
CheckForUpdatesButton.addEventListener('click', CheckForUpdates);
/*
fetch(chrome.runtime.getURL('resources/currencies.json'))
.then((response) => {
if (!response.ok) {
throw new Error('Network not ok');
}
return response.json();
})
.then((data) => {
const DateText = new Date(data.Date).toLocaleDateString('en-US', {day: 'numeric', month: 'long', year: 'numeric'});
document.getElementById('IRLPriceWithCurrency-Date').innerText = DateText;
})
.catch((error) => {
console.log(error);
});
*/
chrome.storage.local.get(['PolyPlus_OutOfDate', 'PolyPlus_LiveVersion', 'PolyPlus_ReleaseNotes', 'PolyPlus_SkipUpdate'], function (result) {
const OutOfDate = result.PolyPlus_OutOfDate || false;
const SkipUpdate = result.PolyPlus_SkipUpdate || null;
const LiveVersion = result.PolyPlus_LiveVersion || Manifest.version;
if (OutOfDate === true && SkipUpdate !== LiveVersion) {
const Banner = document.createElement('div');
Banner.classList = 'alert position-sticky p-3';
Banner.style = 'top: 30px; box-shadow: 0 0 20px 2px #000; z-index: 2000; background: rgb(163 39 39);';
Banner.innerHTML = `
<b>New Update Available!</b>
<br>
Your Poly+ installation is out of date! If you would like to get the latest and greatest features, improvements, and bug fixes click on one of the links below to dismiss this banner!
<br>
<div role="group" class="btn-group w-100 mt-2">
<a href="${result.PolyPlus_ReleaseNotes}" class="btn btn-primary btn-sm w-25" target="_blank">Go to Release Notes</a>
<button id="skip-this-update" class="btn btn-warning btn-sm w-25">(Not Recommended) Skip this Update</button>
</div>
`;
document.getElementById('page').insertBefore(Banner, document.getElementById('page').children[1]);
const SkipButton = document.getElementById('skip-this-update');
SkipButton.addEventListener('click', function () {
Banner.remove();
chrome.storage.local.set({PolyPlus_SkipUpdate: result.PolyPlus_LiveVersion}, function () {
console.log('set skip update to live version: ', result.PolyPlus_LiveVersion);
});
});
}
});