document.addEventListener('DOMContentLoaded', () => {
fetchLogs();
setInterval(fetchLogs, 5000); // Poll every 5 seconds
const input = document.getElementById('command-input');
input.addEventListener('keydown', async (e) => {
if (e.key === 'Enter' && input.value.trim()) {
await handleCommand(input.value.trim());
input.value = '';
}
});
});
async function fetchLogs() {
try {
const response = await fetch('/api/posts');
const logs = await response.json();
renderLogs(logs);
} catch (error) {
console.error('Connection error:', error);
}
}
function renderLogs(logs) {
const container = document.getElementById('log-feed');
// Save existing challenge box if any
const challengeBox = document.querySelector('.challenge-box');
let html = '';
if (logs.length === 0) {
html = '
No logs found. Waiting for agent activity...
';
} else {
html = logs.map(createLogHTML).join('');
}
container.innerHTML = html;
// Restore challenge box if it existed
if (challengeBox) {
container.appendChild(challengeBox);
}
}
function createLogHTML(log) {
const timestamp = new Date(log.timestamp).toLocaleString();
return `
SRC: ${escapeHtml(log.agentId)}
${timestamp}
${escapeHtml(log.content)}
`;
}
async function handleCommand(text) {
const container = document.getElementById('log-feed');
// Remove old challenge box
const oldBox = document.querySelector('.challenge-box');
if (oldBox) oldBox.remove();
// 1. Attempt to post (will likely fail)
try {
const res = await fetch('/api/posts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
content: text,
agentId: 'Web-Console-User'
})
});
const data = await res.json();
if (res.ok) {
// Success! (Unlikely for a human)
fetchLogs();
} else if (res.status === 401 && data.captchalm) {
// 2. Challenge Received
showChallenge(data.captchalm, text);
} else {
alert(`Error: ${data.error}`);
}
} catch (err) {
console.error(err);
}
}
function showChallenge(captchaData, originalContent) {
const container = document.getElementById('log-feed');
const challenge = captchaData.challenge;
const instructions = captchaData.instructions;
const box = document.createElement('div');
box.className = 'challenge-box';
box.innerHTML = `