-
Notifications
You must be signed in to change notification settings - Fork 102
/
popup.js
464 lines (399 loc) · 14.1 KB
/
popup.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
let plan = null;
let checking_server_plan = false;
let rendering_server_plan = false;
function sleep(t) {
return new Promise(resolve => setTimeout(t));
}
function get_loading_html() {
return '<div class="loading"><div></div><div></div><div></div><div></div></div>';
}
function number_with_comma(n) {
n = !n ? 0 : n;
return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
async function check_plan() {
const settings = await BG.exec('Settings.get');
if (!settings) {
return;
}
if (checking_server_plan) {
return;
}
checking_server_plan = true;
plan = await BG.exec('Server.get_plan', {key: settings.key}); // plan = {plan, credit, quota, duration, lastreset}
if (plan.error) {
plan = {
error: true,
plan: plan.message,
credit: 0,
quota: 0,
duration: null,
lastreset: null,
current_period_start: 1,
current_period_end: 1,
};
}
plan.subscription = ['Starter', 'Basic', 'Professional', 'Enterprise'].includes(plan.plan);
plan.invalid = false;
if (['Banned IP', 'Invalid key', 'Rate limit reached'].includes(plan.plan)) {
plan.invalid = true;
}
else {
plan.plan = `${plan.plan} Plan`;
}
plan.expired = false;
if (plan.subscription) {
const now = Date.now() / 1000;
const delta = plan.current_period_end - now;
if (delta < 0) {
plan.expired = true;
plan.credit = 0;
plan.quota = 0;
}
}
checking_server_plan = false;
const $loading_overlay = document.querySelector('#loading_overlay');
$loading_overlay.classList.add('hidden');
}
async function render_plan() {
const settings = await BG.exec('Settings.get');
if (!settings) {
return;
}
if (!plan) {
return;
}
if (rendering_server_plan) {
return;
}
rendering_server_plan = true;
const $plan = document.querySelector('#plan');
const $credit = document.querySelector('#credit');
const $refills = document.querySelector('#refills');
const $ipbanned_warning = document.querySelector('#ipbanned_warning');
const now = Date.now() / 1000;
let secs_until_reset = null;
if (plan.lastreset && plan.duration) {
secs_until_reset = Math.floor(Math.max(0, plan.duration - (now - plan.lastreset)));
}
// Display plan name
$plan.innerHTML = plan.plan;
if (plan.invalid || plan.error) {
$plan.classList.add('red');
}
else {
$plan.classList.remove('red');
}
// Display banned message
if (plan.plan === 'Banned IP') {
$ipbanned_warning.classList.remove('hidden');
}
else {
$ipbanned_warning.classList.add('hidden');
}
// Display remaining credits
$credit.innerHTML = `${number_with_comma(plan.credit)} / ${number_with_comma(plan.quota)}`;
if (plan.credit === 0) {
$credit.classList.add('red');
}
else {
$credit.classList.remove('red');
}
// Display time until reset
if (plan.expired) {
$refills.innerHTML = 'Expired';
$refills.classList.add('red');
}
else {
if (plan.duration < 0) {
$refills.innerHTML = 'No refills';
$refills.classList.add('red');
}
else if (secs_until_reset) {
const hms = Time.seconds_as_hms(secs_until_reset);
$refills.innerHTML = `${hms}`;
$refills.classList.remove('red');
}
else {
$refills.innerHTML = get_loading_html();
$refills.classList.remove('red');
}
// Plan may have been reset. Fetch data from server
if (plan.lastreset === 1) {
$refills.innerHTML = 'Not activated';
$refills.classList.add('red');
}
else if (plan.duration > 0 && secs_until_reset === 0) {
await sleep(1000);
await check_plan();
}
}
rendering_server_plan = false;
}
async function init_ui() {
const settings = await BG.exec('Settings.get');
console.log('settings', settings);
/**
* Power button
*/
const $power_wrapper = document.querySelector('#power');
const $power_spinning = $power_wrapper.querySelector('.spinning');
const $power_static = $power_wrapper.querySelector('.static');
const $power_btn = $power_wrapper.querySelector('.btn');
if (settings.enabled) {
$power_static.classList.remove('hidden');
$power_btn.classList.remove('off');
}
else {
$power_btn.classList.add('off');
}
let last_anim = null;
$power_wrapper.addEventListener('click', async () => {
clearTimeout(last_anim);
$power_spinning.classList.add('hidden');
$power_static.classList.add('hidden');
if ($power_btn.classList.contains('off')) {
$power_btn.classList.remove('off');
$power_spinning.classList.remove('hidden');
await BG.exec('Settings.set', {id: 'enabled', value: true});
await BG.exec('Icon.set', {status: 'on'});
last_anim = setTimeout(() => {
$power_spinning.classList.add('hidden');
$power_static.classList.remove('hidden');
}, 1000);
}
else {
await BG.exec('Settings.set', {id: 'enabled', value: false});
await BG.exec('Icon.set', {status: 'off'});
$power_btn.classList.add('off');
}
});
/**
* Subscription key
*/
const $key = document.querySelector('.settings_text[data-settings="key"]');
const $edit_icon = document.querySelector('.edit_icon');
const $key_label = document.querySelector('.key_label');
function toggle_edit_key() {
if ($key.classList.contains('hiddenleft')) {
$key.classList.remove('hiddenleft');
$key.focus();
$edit_icon.classList.remove('hidden');
$key_label.classList.add('hidden');
}
else {
$key.classList.add('hiddenleft');
$edit_icon.classList.add('hidden');
$key_label.classList.remove('hidden');
}
}
document.querySelector('#edit_key').addEventListener('click', () => {
toggle_edit_key();
check_plan();
});
$key.addEventListener('keydown', e => {
e = e || window.event;
if (e.key === 'Enter') {
toggle_edit_key();
check_plan();
// Allow settings export when key is present
if ($key.value.length > 0) {
document.querySelector('#export').classList.remove('hidden');
}
else {
document.querySelector('#export').classList.add('hidden');
}
}
});
// Allow settings export when key is present
if (settings.key?.length > 0) {
document.querySelector('#export').classList.remove('hidden');
}
else {
document.querySelector('#export').classList.add('hidden');
}
/**
* Tab switching
*/
for (const $e of document.querySelectorAll('[data-tabtarget]:not([data-tabtarget=""])')) {
$e.addEventListener('click', () => {
for (const $t of document.querySelectorAll('.tab')) {
$t.classList.add('hidden');
}
const $tab = document.querySelector(`[data-tab="${$e.dataset.tabtarget}"]`);
$tab.classList.remove('hidden');
});
}
/**
* Navigate backwards on mouse back or backspace
*/
function back() {
const $active_tab = document.querySelector('.tab:not(.hidden)');
$active_tab.querySelector('.back')?.click();
}
document.addEventListener('mousedown', e => {
e = e || window.event;
if ((e.buttons & 8) > 0) {
back();
}
});
document.addEventListener('keydown', e => {
e = e || window.event;
if (e.key === 'Backspace' && !(e.target instanceof HTMLInputElement)) {
back();
}
});
/**
* Set UI from settings and attach listeners
*/
for (const [k, v] of Object.entries(settings)) {
const $toggles = document.querySelectorAll(`.settings_toggle[data-settings="${k}"]`);
for (const $toggle of $toggles) {
$toggle.classList.remove('on', 'off');
$toggle.classList.add(v ? 'on' : 'off');
// Listen
$toggle.addEventListener('click', async () => {
const value = $toggle.classList.contains('off');
await BG.exec('Settings.set', {id: k, value: value});
$toggle.classList.remove('on', 'off');
$toggle.classList.add(value ? 'on' : 'off');
});
}
const $options = document.querySelectorAll(`.settings_dropdown[data-settings="${k}"]`);
for (const $option of $options) {
if ($option.dataset.value === v) {
$option.classList.add('selected');
document.querySelector($option.dataset.displays).innerHTML = $option.innerHTML;
}
// Listen
$option.addEventListener('click', async () => {
document.querySelector(`.settings_dropdown.selected[data-settings="${k}"]`)?.classList?.remove('selected');
const value = $option.dataset.value;
await BG.exec('Settings.set', {id: k, value: value});
$option.classList.add('selected');
document.querySelector($option.dataset.displays).innerHTML = $option.innerHTML;
});
}
const $texts = document.querySelectorAll(`.settings_text[data-settings="${k}"]`);
for (const $text of $texts) {
$text.value = v;
// Listen
$text.addEventListener('input', async () => {
const value = $text.value;
await BG.exec('Settings.set', {id: k, value: value});
// console.log(k, value);
});
}
}
/**
* Locate element
*/
for (const $e of document.querySelectorAll('.locate')) {
$e.addEventListener('click', async () => {
const key = $e.dataset.key;
await BG.exec('Relay.send', {data: {action: 'start_locate', locate: key}});
// await BG.exec('Relay.send', {action: 'start_locate', locate: key});
window.close();
});
}
/**
* Disabled hosts
*/
const $bl = document.querySelector('#disabled_hosts');
async function set_disabled_hosts(render=true) {
const hosts = new Set();
for (const e of settings.disabled_hosts) {
hosts.add(e.trim());
}
settings.disabled_hosts = [...hosts];
await BG.exec('Settings.set', {id: 'disabled_hosts', value: settings.disabled_hosts});
if (render) {
await render_disabled_hosts();
}
}
async function set_current_host() {
const active_tab = await BG.exec('Tab.active');
const active_url = active_tab.url ? active_tab.url : 'Unknown Host';
const current_host = active_url.replace(/^(.*:)\/\/([A-Za-z0-9\-\.]+)(:[0-9]+)?(.*)$/, '$2');
const $current_host = document.querySelector('#current_page_host');
$current_host.innerHTML = current_host;
let can_add = true;
if (!active_tab.url || settings.disabled_hosts.includes(settings.disabled_hosts)) {
can_add = false;
}
const $add_current_host = document.querySelector('#add_current_page_host');
if (can_add) {
$add_current_host.addEventListener('click', async () => {
settings.disabled_hosts.push(current_host);
await set_disabled_hosts();
});
}
else {
$add_current_host.disabled = true;
}
}
async function render_disabled_hosts() {
$bl.innerHTML = ''; // Clear disabled_hosts
const $bl_item_template = document.querySelector('#template > #disabled_hosts_item');
let change_delay_timer = null;
for (const i in settings.disabled_hosts) {
const e = settings.disabled_hosts[i]?.trim();
if (!e) {
continue;
}
const $e = $bl_item_template.cloneNode(true);
$e.id = null;
// Change hostname
const $input = $e.querySelector('input.hostname');
$input.value = e;
$input.addEventListener('input', () => {
clearTimeout(change_delay_timer);
console.log('$input.value', $input.value);
settings.disabled_hosts[i] = $input.value;
change_delay_timer = setTimeout(async () => {
await set_disabled_hosts(false);
}, 200);
});
// Remove hostname
const $remove = $e.querySelector('.remove');
$remove.addEventListener('click', () => {
const index = settings.disabled_hosts.indexOf($input.value);
if (index !== -1) {
settings.disabled_hosts.splice(index, 1);
set_disabled_hosts(false);
}
$e.remove();
});
$bl.append($e);
}
}
set_current_host();
render_disabled_hosts();
/**
* Export settings
*/
document.querySelector('#export').addEventListener('click', async () => {
const settings = await BG.exec('Settings.get');
const url = SettingsManager.export(settings);
window.open(url, '_blank');
});
/**
* Footer
*/
const VERSION = `Version ${chrome.runtime.getManifest().version}`;
for (const $footer of document.querySelectorAll('.footer')) {
const $version = document.createElement('div');
$version.innerHTML = VERSION;
const $copyright = document.createElement('div');
$copyright.innerHTML = `2022 NopeCHA`;
$footer.append($version);
$footer.append($copyright);
}
}
async function main() {
await init_ui();
await check_plan();
await render_plan();
setInterval(render_plan, 500);
}
document.addEventListener('DOMContentLoaded', main);