|
document.addEventListener('DOMContentLoaded', function() { |
|
const convertForm = document.getElementById('convertForm'); |
|
const resultCard = document.getElementById('resultCard'); |
|
const convertResult = document.getElementById('convertResult'); |
|
const copyBtn = document.getElementById('copyBtn'); |
|
const toast = document.getElementById('toast'); |
|
|
|
|
|
convertForm.addEventListener('submit', function(e) { |
|
e.preventDefault(); |
|
|
|
|
|
const formData = new FormData(convertForm); |
|
|
|
|
|
showToast('正在处理,请稍候...'); |
|
|
|
|
|
fetch('/convert', { |
|
method: 'POST', |
|
body: formData |
|
}) |
|
.then(response => response.json()) |
|
.then(data => { |
|
if (data.status === 'success') { |
|
|
|
convertResult.value = data.result; |
|
resultCard.style.display = 'block'; |
|
|
|
|
|
resultCard.scrollIntoView({ behavior: 'smooth' }); |
|
} else { |
|
showToast('错误: ' + data.message, 'error'); |
|
} |
|
}) |
|
.catch(error => { |
|
showToast('请求失败: ' + error, 'error'); |
|
}); |
|
}); |
|
|
|
|
|
copyBtn.addEventListener('click', function() { |
|
convertResult.select(); |
|
document.execCommand('copy'); |
|
|
|
|
|
window.getSelection().removeAllRanges(); |
|
|
|
showToast('已复制到剪贴板!'); |
|
}); |
|
|
|
|
|
function showToast(message, type = 'info') { |
|
toast.textContent = message; |
|
toast.className = 'toast show ' + type; |
|
|
|
|
|
setTimeout(function() { |
|
toast.className = 'toast'; |
|
}, 3000); |
|
} |
|
|
|
|
|
const inputs = document.querySelectorAll('input, textarea, select'); |
|
inputs.forEach(input => { |
|
input.addEventListener('focus', function() { |
|
this.parentElement.classList.add('focused'); |
|
}); |
|
|
|
input.addEventListener('blur', function() { |
|
this.parentElement.classList.remove('focused'); |
|
}); |
|
}); |
|
}); |
|
|