flash.moe/assets/makai.js/whois.js

106 lines
3.1 KiB
JavaScript

#include utility.js
#include xhr.js
const MakaiWHOIS = () => {
let locked = false;
const input = $i('lookup-input');
const submit = $i('lookup-submit');
const result = $i('lookup-result');
const tabs = $i('lookup-tabs');
const lock = () => {
if(locked)
return false;
document.body.classList.add('whois-locked');
input.disabled = true;
locked = true;
return true;
}
const unlock = () => {
if(!locked)
return false;
document.body.classList.remove('whois-locked');
input.disabled = false;
locked = false;
return true;
}
const lookup = target => {
if(!lock())
return;
$x.post('/whois/lookup', {}, { _csrfp: $csrfp.get(), target: target }).then(output => {
let headers = output.headers();
if(headers.has('x-csrfp'))
$csrfp.set(headers.get('x-csrfp'));
let resp = output.json();
if(resp.error)
alert(resp.text);
let count = 0;
tabs.innerHTML = '';
if(resp.result && resp.result.responses)
for(const response of resp.result.responses) {
const tab = document.createElement('a');
const tabHeader = document.createElement('div');
const tabServer = document.createElement('div');
tab.href = 'javascript:;';
tab.className = 'whois-result-tab';
if(count === 0) tab.className += ' whois-result-tab-active';
tab.onclick = () => {
const active = $q('.whois-result-tab-active');
if(active) active.classList.remove('whois-result-tab-active');
tab.classList.add('whois-result-tab-active');
result.textContent = response.lines.join("\r\n").trim();
};
tabHeader.className = 'whois-result-tab-header';
tabHeader.textContent = count === 0 ? 'Result' : ('Hop #' + (resp.result.responses.length - count).toString());
tabServer.className = 'whois-result-tab-server';
tabServer.textContent = response.server;
tab.appendChild(tabHeader);
tab.appendChild(tabServer);
tabs.appendChild(tab);
++count;
}
if(tabs.firstChild)
tabs.firstChild.click();
unlock();
});
}
const readHash = () => {
if(location.hash.length < 2) {
result.textContent = 'Enter a domain or IP address!';
return;
}
const target = decodeURIComponent(location.hash.substring(1));
if(input.value !== target)
input.value = target;
lookup(target);
};
window.addEventListener('hashchange', ev => {
readHash();
}, false);
submit.addEventListener('click', ev => {
ev.preventDefault();
location.hash = encodeURIComponent(input.value);
});
readHash();
};