windowsxp / assets /js /script.js
Tingchenliang's picture
Upload folder using huggingface_hub
be46bb4 verified
// Time update
function updateTime() {
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
const ampm = hours >= 12 ? 'PM' : 'AM';
const displayHours = hours % 12 || 12;
const displayMinutes = minutes < 10 ? '0' + minutes : minutes;
document.getElementById('taskbarTime').textContent = `${displayHours}:${displayMinutes} ${ampm}`;
}
updateTime();
setInterval(updateTime, 1000);
// Start menu toggle
const startButton = document.querySelector('.start-button');
const startMenu = document.getElementById('startMenu');
startButton.addEventListener('click', (e) => {
e.stopPropagation();
startMenu.style.display = startMenu.style.display === 'none' ? 'block' : 'none';
});
document.addEventListener('click', () => {
startMenu.style.display = 'none';
});
// Desktop icons
const desktopIcons = document.querySelectorAll('.desktop-icon');
const aiChatWindow = document.getElementById('aiChatWindow');
desktopIcons.forEach(icon => {
icon.addEventListener('dblclick', () => {
const app = icon.dataset.app;
if (app === 'ai-chat') {
aiChatWindow.style.display = 'block';
}
});
});
// Window controls
const windowControls = document.querySelectorAll('.window-btn');
windowControls.forEach(btn => {
btn.addEventListener('click', (e) => {
const window = e.target.closest('.window');
if (e.target.classList.contains('close')) {
window.style.display = 'none';
} else if (e.target.classList.contains('minimize')) {
window.style.display = 'none';
}
});
});
// Window dragging
let isDragging = false;
let dragOffset = { x: 0, y: 0 };
const windowHeader = document.querySelector('.window-header');
windowHeader.addEventListener('mousedown', (e) => {
isDragging = true;
const rect = aiChatWindow.getBoundingClientRect();
dragOffset.x = e.clientX - rect.left;
dragOffset.y = e.clientY - rect.top;
});
document.addEventListener('mousemove', (e) => {
if (isDragging) {
aiChatWindow.style.left = (e.clientX - dragOffset.x) + 'px';
aiChatWindow.style.top = (e.clientY - dragOffset.y) + 'px';
}
});
document.addEventListener('mouseup', () => {
isDragging = false;
});
// Chat functionality
const chatMessages = document.getElementById('chatMessages');
const chatInput = document.getElementById('chatInput');
const sendButton = document.getElementById('sendButton');
function addMessage(role, content) {
const messageDiv = document.createElement('div');
messageDiv.className = `chat-message ${role}`;
messageDiv.innerHTML = `
<strong>${role === 'user' ? 'You' : 'AI Assistant'}:</strong>
<p>${content}</p>
`;
chatMessages.appendChild(messageDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
async function sendMessage() {
const message = chatInput.value.trim();
if (!message) return;
addMessage('user', message);
chatInput.value = '';
const loadingDiv = document.createElement('div');
loadingDiv.className = 'chat-message assistant loading';
loadingDiv.innerHTML = '<strong>AI Assistant:</strong><p>Thinking...</p>';
chatMessages.appendChild(loadingDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
try {
const response = await fetch('https://api.novita.ai/openai/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer sk_IFT2vaiqJ1naH0HU-NS_ixSF3iAo6x_2zkpGEKcLXQM'
},
body: JSON.stringify({
model: 'deepseek/deepseek-v3-0324',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: message }
],
max_tokens: 1000,
temperature: 0.7
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
const assistantMessage = data.choices[0].message.content;
loadingDiv.remove();
addMessage('assistant', assistantMessage);
} catch (error) {
loadingDiv.remove();
addMessage('assistant', `<span class="error">Error: ${error.message}</span>`);
}
}
sendButton.addEventListener('click', sendMessage);
chatInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});