|
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('正在处理,请稍候...', 'info'); |
|
|
|
|
|
const submitBtn = this.querySelector('.btn-convert'); |
|
const originalBtnText = submitBtn.innerHTML; |
|
submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 处理中...'; |
|
submitBtn.disabled = true; |
|
|
|
|
|
fetch('/convert', { |
|
method: 'POST', |
|
body: formData |
|
}) |
|
.then(response => response.json()) |
|
.then(data => { |
|
|
|
submitBtn.innerHTML = originalBtnText; |
|
submitBtn.disabled = false; |
|
|
|
if (data.status === 'success') { |
|
|
|
convertResult.value = data.result; |
|
resultCard.style.display = 'block'; |
|
|
|
|
|
resultCard.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
|
|
|
|
|
resultCard.classList.add('highlight'); |
|
setTimeout(() => { |
|
resultCard.classList.remove('highlight'); |
|
}, 1000); |
|
|
|
showToast('转换成功!', 'success'); |
|
} else { |
|
showToast('错误: ' + data.message, 'error'); |
|
} |
|
}) |
|
.catch(error => { |
|
|
|
submitBtn.innerHTML = originalBtnText; |
|
submitBtn.disabled = false; |
|
|
|
showToast('请求失败: ' + error, 'error'); |
|
}); |
|
}); |
|
|
|
|
|
copyBtn.addEventListener('click', function() { |
|
|
|
convertResult.select(); |
|
|
|
|
|
try { |
|
const successful = document.execCommand('copy'); |
|
if (successful) { |
|
showToast('已复制到剪贴板', 'success'); |
|
|
|
|
|
this.innerHTML = '<i class="fas fa-check"></i> 已复制'; |
|
setTimeout(() => { |
|
this.innerHTML = '<i class="fas fa-copy"></i> 复制'; |
|
}, 2000); |
|
} else { |
|
showToast('复制失败,请手动复制', 'error'); |
|
} |
|
} catch (err) { |
|
showToast('复制失败: ' + err, 'error'); |
|
} |
|
|
|
|
|
window.getSelection().removeAllRanges(); |
|
}); |
|
|
|
|
|
function showToast(message, type = 'info') { |
|
|
|
let icon = ''; |
|
if (type === 'success') icon = '<i class="fas fa-check-circle"></i> '; |
|
else if (type === 'error') icon = '<i class="fas fa-exclamation-circle"></i> '; |
|
else if (type === 'info') icon = '<i class="fas fa-info-circle"></i> '; |
|
|
|
|
|
toast.innerHTML = icon + message; |
|
|
|
|
|
toast.className = 'toast show ' + type; |
|
|
|
|
|
clearTimeout(toast.timerId); |
|
toast.timerId = 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'); |
|
}); |
|
}); |
|
|
|
|
|
convertForm.addEventListener('invalid', function(e) { |
|
|
|
e.preventDefault(); |
|
|
|
|
|
const invalidInput = e.target; |
|
|
|
|
|
invalidInput.classList.add('shake'); |
|
setTimeout(() => { |
|
invalidInput.classList.remove('shake'); |
|
}, 500); |
|
|
|
|
|
showToast('请填写所有必填字段', 'error'); |
|
}, true); |
|
|
|
|
|
const originalUrlField = document.getElementById('original_url'); |
|
originalUrlField.addEventListener('paste', function(e) { |
|
|
|
let pasteData = e.clipboardData.getData('text'); |
|
|
|
pasteData = pasteData.trim(); |
|
|
|
|
|
if (!/^https?:\/\//.test(pasteData) && pasteData.length > 0) { |
|
e.preventDefault(); |
|
|
|
|
|
if (pasteData.includes('://')) { |
|
|
|
const urlMatch = pasteData.match(/[a-z]+:\/\/[^\s]+/i); |
|
if (urlMatch) { |
|
pasteData = urlMatch[0]; |
|
} |
|
} |
|
|
|
|
|
this.value = pasteData; |
|
|
|
|
|
if (!/^[a-z]+:\/\//i.test(pasteData)) { |
|
showToast('您粘贴的内容不是标准的URL格式,请检查', 'info'); |
|
} |
|
} |
|
}); |
|
|
|
|
|
if (document.getElementById('themeToggle')) { |
|
document.getElementById('themeToggle').addEventListener('click', function() { |
|
document.body.classList.toggle('light-mode'); |
|
const isDark = !document.body.classList.contains('light-mode'); |
|
this.innerHTML = isDark ? |
|
'<i class="fas fa-sun"></i>' : |
|
'<i class="fas fa-moon"></i>'; |
|
}); |
|
} |
|
}); |
|
|