-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
563 lines (477 loc) · 15.7 KB
/
Copy pathscripts.js
File metadata and controls
563 lines (477 loc) · 15.7 KB
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Constants
const API_BASE_URL = "https://trustpositif.glng.my.id";
const API_CHECK_ENDPOINT = "/check/";
const MAX_RECENT_CHECKS = 5;
const STORAGE_KEY = "trustpositif_recent_checks";
const THEME_STORAGE_KEY = "trustpositif_theme";
const RATE_LIMIT_PER_MINUTE = 60;
const API_TIMEOUT = 15000; // 15 seconds timeout
// DOM Elements
const domainForm = document.getElementById("domain-form");
const domainInput = document.getElementById("domain-input");
const resultContainer = document.getElementById("result-container");
const resultDomain = document.getElementById("result-domain");
const resultTimestamp = document.getElementById("result-timestamp");
const resultStatus = document.getElementById("result-status");
const errorContainer = document.getElementById("error-container");
const errorMessage = document.getElementById("error-message");
const dismissErrorBtn = document.getElementById("dismiss-error");
const recentChecksList = document.getElementById("recent-checks-list");
const apiStatusElement = document.getElementById("api-status");
const remainingQuotaElement = document.getElementById("remaining-quota");
const aboutLink = document.getElementById("about-link");
const aboutModal = document.getElementById("about-modal");
const closeModalBtn = document.getElementById("close-modal");
const themeToggleBtn = document.getElementById("theme-toggle-btn");
// State variables
let recentChecks = [];
let rateLimitCounter = 0;
let rateLimitResetTime = Date.now() + 60000; // 1 minute from now
let rateLimitIntervalId = null;
let apiAvailable = true;
let networkOffline = false;
// Initialize the application
function initApp() {
// Set up event listeners
domainForm.addEventListener("submit", handleDomainCheck);
dismissErrorBtn.addEventListener("click", dismissError);
aboutLink.addEventListener("click", openModal);
closeModalBtn.addEventListener("click", closeModal);
themeToggleBtn.addEventListener("click", toggleTheme);
window.addEventListener("click", (e) => {
if (e.target === aboutModal) {
closeModal();
}
});
// Handle offline/online events
window.addEventListener("online", handleOnline);
window.addEventListener("offline", handleOffline);
// Load recent checks from localStorage
loadRecentChecks();
// Initialize theme
initTheme();
// Start rate limit timer
startRateLimitTimer();
// Check API status
checkAPIStatus();
// Set up periodic API status check
setInterval(checkAPIStatus, 60000); // Check every minute
}
// Handle domain check
async function handleDomainCheck(e) {
e.preventDefault();
const domain = domainInput.value.trim();
// Validate domain
if (!domain) {
showError("Please enter a domain");
return;
}
if (!isValidDomain(domain)) {
showError("Please enter a valid domain (e.g., example.com)");
return;
}
// Check rate limit
if (rateLimitCounter >= RATE_LIMIT_PER_MINUTE) {
showError(
`Rate limit reached (${RATE_LIMIT_PER_MINUTE} requests per minute). Please try again later.`
);
return;
}
// Check network status
if (networkOffline) {
showError(
"You are currently offline. Please check your internet connection."
);
return;
}
// Always attempt the check, even if API availability is uncertain
// This addresses the "API offline, padahal berfungsi" issue
// Hide previous results
resultContainer.classList.add("hidden");
errorContainer.classList.add("hidden");
// Show loading indicator
showLoading();
try {
const result = await checkDomain(domain);
// If we get here, API is working
apiAvailable = true;
updateAPIStatus(true);
// Hide loading indicator
hideLoading();
// Display result
displayResult(result);
// Add to recent checks
addToRecentChecks({
domain: result.domain,
blocked: result.blocked,
timestamp: result.timestamp,
});
// Update rate limit counter
updateRateLimitCounter();
} catch (error) {
console.log("Error checking domain:", error);
// Hide loading indicator
hideLoading();
// More robust error handling
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
if (error.response.status === 429) {
showError("Rate limit exceeded. Please try again in a minute.");
rateLimitCounter = RATE_LIMIT_PER_MINUTE; // Force rate limit
} else if (error.response.status === 400) {
showError("Invalid domain. Please check your input.");
} else {
showError(
`Server error: ${
error.response.data?.error ||
error.response.statusText ||
"Unknown error"
}`
);
}
} else if (error.request) {
// The request was made but no response was received
showError(
"Unable to reach the server. Please check your internet connection."
);
// Don't mark API as unavailable just because of one failed request
// This helps with the "API offline, padahal berfungsi" issue
checkAPIStatus(); // Recheck API status instead
} else {
// Something happened in setting up the request that triggered an Error
showError(`Error: ${error.message || "Unknown error occurred"}`);
}
// Update rate limit counter for failed requests too
updateRateLimitCounter();
}
}
// Check domain against the API
async function checkDomain(domain) {
const url = `${API_BASE_URL}${API_CHECK_ENDPOINT}${encodeURIComponent(
domain
)}`;
try {
// First try with fetch API (more reliable in some browsers)
const fetchResponse = await fetch(url, {
method: "GET",
headers: {
Accept: "*/*",
"User-Agent": "TrustPositif-WebClient",
},
});
if (fetchResponse.ok) {
return await fetchResponse.json();
}
// If fetch fails with an HTTP error, throw it to be caught by the caller
throw new Error(`HTTP error! status: ${fetchResponse.status}`);
} catch (fetchError) {
console.log("Fetch failed, trying axios as fallback:", fetchError);
// Fallback to axios
const axiosResponse = await axios.get(url, {
timeout: API_TIMEOUT,
headers: {
Accept: "*/*",
"User-Agent": "TrustPositif-WebClient",
},
});
return axiosResponse.data;
}
}
// Display the result
function displayResult(result) {
resultDomain.textContent = result.domain;
// Format timestamp
const timestamp = new Date(result.timestamp);
resultTimestamp.textContent = formatDate(timestamp);
// Set status with appropriate icon
if (result.blocked) {
resultStatus.innerHTML = `
<div class="status-blocked">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path d="M12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10-4.477 10-10 10zm-1-7v2h2v-2h-2zm0-8v6h2V7h-2z"/>
</svg>
<span>Domain is blocked in TrustPositif</span>
</div>
`;
} else {
resultStatus.innerHTML = `
<div class="status-safe">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path d="M12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10-4.477 10-10 10zm-1-7v2h2v-2h-2zm0-8v6h2V7h-2z"/>
</svg>
<span>Domain is not blocked in TrustPositif</span>
</div>
`;
}
// Show result container
resultContainer.classList.remove("hidden");
resultContainer.classList.add("visible");
}
// Show error message
function showError(message) {
errorMessage.textContent = message;
errorContainer.classList.remove("hidden");
errorContainer.classList.add("visible");
}
// Dismiss error message
function dismissError() {
errorContainer.classList.remove("visible");
setTimeout(() => {
errorContainer.classList.add("hidden");
}, 300);
}
// Add a domain check to recent checks
function addToRecentChecks(check) {
// Check if domain already exists in recent checks
const existingIndex = recentChecks.findIndex(
(item) => item.domain === check.domain
);
if (existingIndex !== -1) {
// Remove existing entry
recentChecks.splice(existingIndex, 1);
}
// Add new check to the beginning
recentChecks.unshift(check);
// Limit the number of recent checks
if (recentChecks.length > MAX_RECENT_CHECKS) {
recentChecks.pop();
}
// Save to localStorage
saveRecentChecks();
// Update UI
updateRecentChecksList();
}
// Save recent checks to localStorage
function saveRecentChecks() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(recentChecks));
}
// Load recent checks from localStorage
function loadRecentChecks() {
const storedChecks = localStorage.getItem(STORAGE_KEY);
if (storedChecks) {
try {
recentChecks = JSON.parse(storedChecks);
updateRecentChecksList();
} catch (error) {
console.error("Failed to parse stored checks:", error);
recentChecks = [];
}
}
}
// Update the recent checks list in the UI
function updateRecentChecksList() {
if (recentChecks.length === 0) {
recentChecksList.innerHTML =
'<p class="no-history">No recent checks yet.</p>';
return;
}
recentChecksList.innerHTML = "";
recentChecks.forEach((check) => {
const item = document.createElement("div");
item.className = "check-item";
const iconColor = check.blocked ? "var(--error)" : "var(--success)";
item.innerHTML = `
<div class="check-domain">
<svg class="check-domain-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="${iconColor}">
<circle cx="12" cy="12" r="10" />
</svg>
${check.domain}
</div>
<div class="check-timestamp">${formatDate(
new Date(check.timestamp)
)}</div>
`;
// Add click handler to re-check the domain
item.addEventListener("click", () => {
domainInput.value = check.domain;
domainForm.dispatchEvent(new Event("submit"));
});
recentChecksList.appendChild(item);
});
}
// Format date for display
function formatDate(date) {
return date.toLocaleString(undefined, {
year: "numeric",
month: "short",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
});
}
// Validate domain
function isValidDomain(domain) {
// Basic domain validation
const domainRegex =
/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/i;
return domainRegex.test(domain);
}
// Start the rate limit timer
function startRateLimitTimer() {
// Clear existing interval if any
if (rateLimitIntervalId) {
clearInterval(rateLimitIntervalId);
}
// Set the reset time to 1 minute from now
rateLimitResetTime = Date.now() + 60000;
// Update the UI immediately
updateRemainingQuota();
// Update every second
rateLimitIntervalId = setInterval(() => {
// Check if we should reset the counter
if (Date.now() >= rateLimitResetTime) {
rateLimitCounter = 0;
rateLimitResetTime = Date.now() + 60000;
}
// Update UI
updateRemainingQuota();
}, 1000);
}
// Update rate limit counter
function updateRateLimitCounter() {
rateLimitCounter++;
updateRemainingQuota();
}
// Update remaining quota display
function updateRemainingQuota() {
const remaining = RATE_LIMIT_PER_MINUTE - rateLimitCounter;
remainingQuotaElement.textContent = `${remaining}/${RATE_LIMIT_PER_MINUTE}`;
// Add warning class if getting low
if (remaining <= 10) {
remainingQuotaElement.parentElement.style.color = "var(--warning)";
} else {
remainingQuotaElement.parentElement.style.color = "var(--neutral-500)";
}
// Calculate time until reset
const timeUntilReset = Math.max(
0,
Math.ceil((rateLimitResetTime - Date.now()) / 1000)
);
// Show time until reset if quota is low
if (remaining <= 10 && timeUntilReset > 0) {
remainingQuotaElement.textContent = `${remaining}/${RATE_LIMIT_PER_MINUTE} (resets in ${timeUntilReset}s)`;
}
}
// Check API status - try a real domain lookup instead of status endpoint
async function checkAPIStatus() {
try {
// Try to fetch a known domain as a health check
// This is more reliable than hitting the status endpoint
const testResponse = await fetch(
`${API_BASE_URL}${API_CHECK_ENDPOINT}example.com`,
{
method: "GET",
headers: {
Accept: "*/*",
"User-Agent": "TrustPositif-WebClient",
},
}
);
// Even if we get a 4xx response, the API is still "working"
if (testResponse.status < 500) {
apiAvailable = true;
updateAPIStatus(true);
return;
}
// Otherwise try the status endpoint as fallback
const statusResponse = await fetch(`${API_BASE_URL}/status`, {
method: "GET",
});
apiAvailable = statusResponse.ok;
updateAPIStatus(statusResponse.ok);
} catch (error) {
console.log(
"API status check error, but we'll consider it available anyway:",
error
);
// Force API to be considered available unless proven otherwise
// This addresses the "API offline, padahal berfungsi" issue
apiAvailable = true;
updateAPIStatus(true);
}
}
// Update API status in the UI
function updateAPIStatus(available) {
if (available) {
apiStatusElement.textContent = "Online";
apiStatusElement.style.color = "var(--success)";
} else {
apiStatusElement.textContent = "Offline";
apiStatusElement.style.color = "var(--error)";
}
}
// Handle online event
function handleOnline() {
networkOffline = false;
// Remove offline warning if it exists
const offlineWarning = document.querySelector(".offline-warning");
if (offlineWarning) {
offlineWarning.remove();
}
// Check API status
checkAPIStatus();
}
// Handle offline event
function handleOffline() {
networkOffline = true;
// Add offline warning
const offlineWarning = document.createElement("div");
offlineWarning.className = "offline-warning";
offlineWarning.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path d="M12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10-4.477 10-10 10zm-1-7v2h2v-2h-2zm0-8v6h2V7h-2z"/>
</svg>
<span>You are currently offline. Some features may not be available.</span>
`;
// Only add if it doesn't already exist
if (!document.querySelector(".offline-warning")) {
document.querySelector(".search-container").prepend(offlineWarning);
}
}
// Show loading indicator
function showLoading() {
const loading = document.createElement("div");
loading.className = "loading-dots";
loading.innerHTML = `
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
`;
domainForm.insertAdjacentElement("afterend", loading);
}
// Hide loading indicator
function hideLoading() {
const loading = document.querySelector(".loading-dots");
if (loading) {
loading.remove();
}
}
// Open the modal
function openModal(e) {
e.preventDefault();
aboutModal.classList.add("active");
document.body.style.overflow = "hidden";
}
// Close the modal
function closeModal() {
aboutModal.classList.remove("active");
document.body.style.overflow = "";
}
// Theme handling
function initTheme() {
// Check localStorage or system preference
const savedTheme = localStorage.getItem(THEME_STORAGE_KEY);
if (
savedTheme === "dark" ||
(!savedTheme && window.matchMedia("(prefers-color-scheme: dark)").matches)
) {
document.body.classList.add("dark-theme");
}
}
function toggleTheme() {
const isDarkTheme = document.body.classList.toggle("dark-theme");
localStorage.setItem(THEME_STORAGE_KEY, isDarkTheme ? "dark" : "light");
}
// Initialize the app when DOM is loaded
document.addEventListener("DOMContentLoaded", initApp);