// Shared JavaScript across all pages // HSK word database (simplified version) const hskDatabase = { "你好": { level: 1, style: "Neutral", usage: "Common", meaning: "hello", pinyin: "nǐ hǎo" }, "学习": { level: 1, style: "Neutral", usage: "Common", meaning: "study, learn", pinyin: "xué xí" }, "谢谢": { level: 1, style: "Neutral", usage: "Common", meaning: "thank you", pinyin: "xiè xiè" }, "再见": { level: 1, style: "Neutral", usage: "Common", meaning: "goodbye", pinyin: "zài jiàn" }, "是": { level: 1, style: "Neutral", usage: "Common", meaning: "to be", pinyin: "shì" }, "不": { level: 1, style: "Neutral", usage: "Common", meaning: "no, not", pinyin: "bù" }, "我": { level: 1, style: "Neutral", usage: "Common", meaning: "I, me", pinyin: "wǒ" }, "你": { level: 1, style: "Neutral", usage: "Common", meaning: "you", pinyin: "nǐ" }, "好": { level: 1, style: "Neutral", usage: "Common", meaning: "good", pinyin: "hǎo" }, "人": { level: 1, style: "Neutral", usage: "Common", meaning: "person", pinyin: "rén" }, "冒险": { level: 4, style: "Neutral", usage: "Common", meaning: "adventure, take risks", pinyin: "mào xiǎn" }, "尴尬": { level: 5, style: "Neutral", usage: "Common", meaning: "embarrassed, awkward", pinyin: "gān gà" }, "牛逼": { level: "Not in HSK", style: "Informal", usage: "Common", meaning: "awesome, amazing", pinyin: "niú bī" }, "囧": { level: "Not in HSK", style: "Informal", usage: "Rare", meaning: "embarrassed, awkward", pinyin: "jiǒng" }, "饕餮": { level: "Not in HSK", style: "Formal", usage: "Rare", meaning: "gluttonous monster", pinyin: "tāo tiè" }, "耄耋": { level: "Not in HSK", style: "Formal", usage: "Rare", meaning: "very old age", pinyin: "mào dié" } }; // API endpoints configuration const API_ENDPOINTS = { analyze: '/api/analyze', batchAnalyze: '/api/batch-analyze', hskLevel: '/api/hsk/' }; // API utility functions async function apiCall(endpoint, method = 'GET', data = null) { const options = { method: method, headers: { 'Content-Type': 'application/json', }, }; if (data) { options.body = JSON.stringify(data); } try { const response = await fetch(endpoint, options); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return await response.json(); } catch (error) { console.error('API call failed:', error); // Fallback to local database return fallbackAnalysis(data); } } // Fallback to local database when API is not available function fallbackAnalysis(data) { if (data.word) { return { word: data.word, ...(hskDatabase[data.word] || { level: "Not in HSK", style: "Neutral", usage: "Rare", meaning: "unknown meaning", pinyin: "unknown" } }; } else if (data.words) { return { results: data.words.map(word => ({ word: word, ...(hskDatabase[word] || { level: "Not in HSK", style: "Neutral", usage: "Rare", meaning: "unknown meaning", pinyin: "unknown" })) }; } return null; } // Enhanced analyzeWord function with API support async function analyzeWord(word) { // Check if input is Chinese characters if (!isChinese(word)) { showError("Please enter a valid Chinese word."); return; } // Try API first, then fallback to local database try { const response = await apiCall(API_ENDPOINTS.analyze, 'POST', { word: word }); displayResults(word, response); saveResults(word, response); } catch (error) { // Fallback to local database const wordData = hskDatabase[word] || { level: "Not in HSK", style: "Neutral", usage: "Rare", meaning: "unknown meaning", pinyin: "unknown" }; displayResults(word, wordData); saveResults(word, wordData); } } // DOM Elements const wordForm = document.getElementById('wordForm'); const chineseWordInput = document.getElementById('chineseWord'); const resultsSection = document.getElementById('resultsSection'); const resultsDiv = document.getElementById('results'); const clearResultsBtn = document.getElementById('clearResults'); // Initialize the application document.addEventListener('DOMContentLoaded', function() { console.log('Hanzi Hunter initialized'); // Form submission handler wordForm.addEventListener('submit', function(e) { e.preventDefault(); const word = chineseWordInput.value.trim(); if (word) { analyzeWord(word); } }); // Clear results handler clearResultsBtn.addEventListener('click', function() { clearResults(); }); // Load any saved results from localStorage loadSavedResults(); }); // Analyze the Chinese word function analyzeWord(word) { // Check if input is Chinese characters if (!isChinese(word)) { showError("Please enter a valid Chinese word."); return; } // Find word in database or create default response const wordData = hskDatabase[word] || { level: "Not in HSK", style: "Neutral", usage: "Rare", meaning: "unknown meaning" }; // Display results displayResults(word, wordData); // Save to localStorage saveResults(word, wordData); } // Check if string contains Chinese characters function isChinese(str) { return /[\u4e00-\u9fff]/.test(str); } // Display analysis results function displayResults(word, data) { resultsSection.classList.remove('hidden'); resultsSection.classList.add('fade-in-up'); let warningText = ''; if (data.style === "Informal") { warningText = '
This is casual spoken Chinese.
'; } else if (data.style === "Formal" || data.usage === "Rare") { warningText = 'This word is mostly used in formal writing.
'; } resultsDiv.innerHTML = `Word
${word}
HSK Level
${data.level}
Style
${data.style}
Usage
${data.usage}
Meaning
${data.meaning}
${message}