Files
portal-api/resources/js/whois.js
T
2025-07-24 16:18:27 +07:00

193 lines
8.2 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function() {
const domainInput = document.getElementById('domain');
const getDomainInfoBtn = document.getElementById('getDomainInfo');
const checkAvailabilityBtn = document.getElementById('checkAvailability');
const loading = document.getElementById('loading');
const results = document.getElementById('results');
const domainError = document.getElementById('domainError');
function showLoading() {
loading.classList.remove('hidden');
results.classList.add('hidden');
domainInput.classList.remove('border-red-500');
domainError.classList.add('hidden');
domainError.textContent = '';
}
function hideLoading() {
loading.classList.add('hidden');
}
function showResults(data) {
if (data.success && data.data) {
populateWhoisTable(data.data);
results.classList.remove('hidden');
} else if (data.data && data.data.domain && data.data.available !== undefined) {
// Availability check result
showAvailabilityResult(data.data);
results.classList.remove('hidden');
} else {
// Fallback to console log for unexpected format
console.error('Unexpected response format:', data);
results.classList.remove('hidden');
}
}
function populateWhoisTable(data) {
// Hide availability results and show whois table
const availabilityResults = document.getElementById('availabilityResults');
const whoisInfo = document.querySelector('.who-info .table-responsive');
if (availabilityResults) availabilityResults.style.display = 'none';
if (whoisInfo) whoisInfo.style.display = 'block';
// Set domain title
const domainTitle = document.getElementById('domainTitle');
if (domainTitle) domainTitle.textContent = data.domainName || '';
// Set individual fields
setElementText('domain_name', data.domainName);
setElementText('domain_reg_date', data.creationDate);
setElementText('domain_exp_date', data.expirationDate);
setElementText('domain_registrar', data.registrar);
setElementText('domain_dnssec', data.DNSSEC);
// Handle registrant name
const ownerRow = document.getElementById('domain_owner_row');
const ownerName = document.getElementById('domain_own_name');
if (data.registrantName) {
setElementText('domain_own_name', data.registrantName);
if (ownerRow) ownerRow.style.display = '';
} else {
if (ownerRow) ownerRow.style.display = 'none';
}
// Handle status array
const statusContainer = document.getElementById('domain_status_list');
if (statusContainer && data.status && Array.isArray(data.status)) {
statusContainer.innerHTML = data.status.map(status =>
`<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800 mr-1 mb-1">${status}</span>`
).join('');
}
// Handle nameserver array
const nsContainer = document.getElementById('domain_ns_list');
if (nsContainer && data.nameServer && Array.isArray(data.nameServer)) {
nsContainer.innerHTML = data.nameServer.map(ns =>
`<div class="font-mono text-sm text-gray-700">${ns}</div>`
).join('');
}
// Setup share button
const shareBtn = document.getElementById('shareBtn');
if (shareBtn && data.domainName) {
const shareUrl = `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(window.location.origin + window.location.pathname + '?domain=' + data.domainName)}`;
shareBtn.href = shareUrl;
shareBtn.classList.remove('hidden');
}
}
function showAvailabilityResult(data) {
// Hide whois table and show availability results
const availabilityResults = document.getElementById('availabilityResults');
const whoisInfo = document.querySelector('.who-info .table-responsive');
if (whoisInfo) whoisInfo.classList.add('hidden');
if (availabilityResults) availabilityResults.classList.remove('hidden');
const alert = document.getElementById('availabilityAlert');
const icon = document.getElementById('availabilityIcon');
const title = document.getElementById('availabilityTitle');
const message = document.getElementById('availabilityMessage');
if (data.available) {
alert.className = 'rounded-lg p-4 bg-green-50 border border-green-200';
icon.innerHTML = `<svg class="h-5 w-5 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
</svg>`;
title.textContent = window.translations?.domainAvailable || 'Domain Available';
title.className = 'text-sm font-medium text-green-800';
message.textContent = `${data.domain} ${window.translations?.isAvailable || 'is available for registration'}`;
message.className = 'text-sm text-green-700';
} else {
alert.className = 'rounded-lg p-4 bg-red-50 border border-red-200';
icon.innerHTML = `<svg class="h-5 w-5 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>`;
title.textContent = window.translations?.domainUnavailable || 'Domain Unavailable';
title.className = 'text-sm font-medium text-red-800';
message.textContent = data.status || `${data.domain} ${window.translations?.isNotAvailable || 'is not available for registration'}`;
message.className = 'text-sm text-red-700';
}
// Set domain title
const domainTitle = document.getElementById('domainTitle');
if (domainTitle) domainTitle.textContent = data.domain || '';
}
function setElementText(elementId, text) {
const element = document.getElementById(elementId);
if (element) {
element.textContent = text || '';
}
}
function showError(message) {
domainInput.classList.add('border-red-500');
domainError.textContent = message;
domainError.classList.remove('hidden');
}
function makeRequest(url) {
const domain = domainInput.value.trim();
if (!domain) {
showError(window.translations?.domainRequired || 'Domain name is required.');
return;
}
showLoading();
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
},
body: JSON.stringify({ domain: domain })
})
.then(response => response.json())
.then(data => {
hideLoading();
showResults(data);
if (!data.success && data.errors && data.errors.domain) {
showError(data.errors.domain[0]);
}
})
.catch(error => {
hideLoading();
showError(window.translations?.requestError || 'An error occurred while processing your request.');
console.error('Error:', error);
});
}
// Event listeners
if (getDomainInfoBtn) {
getDomainInfoBtn.addEventListener('click', function() {
makeRequest(window.routes?.domainInfo || '/api/whois/domain-info');
});
}
if (checkAvailabilityBtn) {
checkAvailabilityBtn.addEventListener('click', function() {
makeRequest(window.routes?.checkAvailability || '/api/whois/check-availability');
});
}
// Clear errors on input
if (domainInput) {
domainInput.addEventListener('input', function() {
domainInput.classList.remove('border-red-500');
domainError.classList.add('hidden');
domainError.textContent = '';
});
}
});