Update public/script.js
Browse files- public/script.js +79 -26
public/script.js
CHANGED
|
@@ -1,4 +1,32 @@
|
|
| 1 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
const socket = io({
|
| 3 |
reconnection: true,
|
| 4 |
reconnectionAttempts: Infinity,
|
|
@@ -6,29 +34,33 @@ const socket = io({
|
|
| 6 |
timeout: 20000
|
| 7 |
});
|
| 8 |
|
| 9 |
-
//
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
};
|
| 16 |
|
| 17 |
-
//
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
// Connection monitoring
|
| 22 |
function updateStatus(message, isError = false) {
|
| 23 |
elements.status.textContent = message;
|
| 24 |
elements.status.style.background = isError ? '#e74c3c' : '#2ecc71';
|
| 25 |
-
|
| 26 |
}
|
| 27 |
|
| 28 |
-
// Socket event handlers
|
| 29 |
socket.on('connect', () => {
|
| 30 |
updateStatus('Connected to server');
|
| 31 |
-
socket.emit('
|
| 32 |
});
|
| 33 |
|
| 34 |
socket.on('disconnect', () => {
|
|
@@ -40,24 +72,45 @@ socket.on('connect_error', (err) => {
|
|
| 40 |
});
|
| 41 |
|
| 42 |
socket.on('terminal-output', (data) => {
|
| 43 |
-
|
| 44 |
-
elements.output.scrollTop = elements.output.scrollHeight;
|
| 45 |
});
|
| 46 |
|
| 47 |
socket.on('terminal-init', (data) => {
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
});
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
setTimeout(() => {
|
| 53 |
-
if (elements.output.
|
| 54 |
-
|
| 55 |
}
|
| 56 |
}, 5000);
|
| 57 |
|
| 58 |
-
// Force terminal visibility
|
| 59 |
-
|
| 60 |
elements.botSection.style.display = 'block';
|
| 61 |
-
|
| 62 |
-
updateStatus('System loaded');
|
| 63 |
-
});
|
|
|
|
| 1 |
+
// DOM Elements
|
| 2 |
+
const elements = {
|
| 3 |
+
authSection: document.getElementById('auth-section'),
|
| 4 |
+
botSection: document.getElementById('bot-section'),
|
| 5 |
+
output: document.getElementById('output'),
|
| 6 |
+
status: document.getElementById('connection-status'),
|
| 7 |
+
loginBtn: document.getElementById('login-btn'),
|
| 8 |
+
registerBtn: document.getElementById('register-btn'),
|
| 9 |
+
deployBtn: document.getElementById('deploy-btn'),
|
| 10 |
+
commandInput: document.getElementById('command-input'),
|
| 11 |
+
sendCommand: document.getElementById('send-command'),
|
| 12 |
+
themeToggle: document.getElementById('theme-toggle'),
|
| 13 |
+
musicToggle: document.getElementById('music-toggle'),
|
| 14 |
+
videoElement: document.getElementById('funk-video')
|
| 15 |
+
};
|
| 16 |
+
|
| 17 |
+
// State
|
| 18 |
+
let currentUser = null;
|
| 19 |
+
let isMusicPlaying = false;
|
| 20 |
+
let isDarkMode = true;
|
| 21 |
+
|
| 22 |
+
// Funk/Phunk videos
|
| 23 |
+
const funkVideos = [
|
| 24 |
+
"https://example.com/funk1.mp4",
|
| 25 |
+
"https://example.com/funk2.mp4",
|
| 26 |
+
"https://example.com/phunk1.mp4"
|
| 27 |
+
];
|
| 28 |
+
|
| 29 |
+
// Initialize Socket.io with robust settings
|
| 30 |
const socket = io({
|
| 31 |
reconnection: true,
|
| 32 |
reconnectionAttempts: Infinity,
|
|
|
|
| 34 |
timeout: 20000
|
| 35 |
});
|
| 36 |
|
| 37 |
+
// 1. Force terminal visibility on load
|
| 38 |
+
window.addEventListener('load', () => {
|
| 39 |
+
elements.botSection.style.display = 'block';
|
| 40 |
+
elements.authSection.style.display = 'none';
|
| 41 |
+
updateStatus('System loaded');
|
| 42 |
+
playRandomVideo();
|
| 43 |
+
});
|
| 44 |
|
| 45 |
+
// 2. Play random funk video
|
| 46 |
+
function playRandomVideo() {
|
| 47 |
+
const randomVideo = funkVideos[Math.floor(Math.random() * funkVideos.length)];
|
| 48 |
+
elements.videoElement.src = randomVideo;
|
| 49 |
+
elements.videoElement.play().catch(e => console.log("Video play error:", e));
|
| 50 |
+
elements.videoElement.onended = playRandomVideo;
|
| 51 |
+
}
|
| 52 |
|
| 53 |
+
// 3. Connection monitoring
|
| 54 |
function updateStatus(message, isError = false) {
|
| 55 |
elements.status.textContent = message;
|
| 56 |
elements.status.style.background = isError ? '#e74c3c' : '#2ecc71';
|
| 57 |
+
appendToOutput(message, isError ? 'error' : 'info');
|
| 58 |
}
|
| 59 |
|
| 60 |
+
// 4. Socket event handlers
|
| 61 |
socket.on('connect', () => {
|
| 62 |
updateStatus('Connected to server');
|
| 63 |
+
socket.emit('terminal-ready');
|
| 64 |
});
|
| 65 |
|
| 66 |
socket.on('disconnect', () => {
|
|
|
|
| 72 |
});
|
| 73 |
|
| 74 |
socket.on('terminal-output', (data) => {
|
| 75 |
+
appendToOutput(data, 'output');
|
|
|
|
| 76 |
});
|
| 77 |
|
| 78 |
socket.on('terminal-init', (data) => {
|
| 79 |
+
appendToOutput(`Terminal initialized at ${new Date().toLocaleTimeString()}`, 'success');
|
| 80 |
+
});
|
| 81 |
+
|
| 82 |
+
// 5. Append to terminal
|
| 83 |
+
function appendToOutput(text, type = 'output') {
|
| 84 |
+
const line = document.createElement('div');
|
| 85 |
+
line.className = type;
|
| 86 |
+
line.textContent = text;
|
| 87 |
+
elements.output.appendChild(line);
|
| 88 |
+
elements.output.scrollTop = elements.output.scrollHeight;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
// 6. Command handling
|
| 92 |
+
elements.sendCommand.addEventListener('click', sendCommand);
|
| 93 |
+
elements.commandInput.addEventListener('keypress', (e) => {
|
| 94 |
+
if (e.key === 'Enter') sendCommand();
|
| 95 |
});
|
| 96 |
|
| 97 |
+
function sendCommand() {
|
| 98 |
+
const command = elements.commandInput.value.trim();
|
| 99 |
+
if (!command) return;
|
| 100 |
+
|
| 101 |
+
appendToOutput(`$ ${command}`, 'command');
|
| 102 |
+
socket.emit('terminal-command', { command, userId: currentUser });
|
| 103 |
+
elements.commandInput.value = '';
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
// 7. Fallback: If no connection after 5 seconds
|
| 107 |
setTimeout(() => {
|
| 108 |
+
if (elements.output.children.length === 0) {
|
| 109 |
+
appendToOutput('Running in offline mode. Some features may be limited.', 'warning');
|
| 110 |
}
|
| 111 |
}, 5000);
|
| 112 |
|
| 113 |
+
// 8. Force terminal visibility (redundant check)
|
| 114 |
+
setInterval(() => {
|
| 115 |
elements.botSection.style.display = 'block';
|
| 116 |
+
}, 3000);
|
|
|
|
|
|