document.addEventListener('DOMContentLoaded', function() { const urlInput = document.getElementById('urlInput'); const generateBtn = document.getElementById('generateBtn'); const qrcodeDiv = document.getElementById('qrcode'); const wordFile = document.getElementById('wordFile'); const preview = document.getElementById('preview'); const downloadBtn = document.getElementById('downloadBtn'); let qr = null; let convertedHtml = ''; // 加载翻译设置 loadTranslationSettings(); const tabs = document.querySelectorAll('.tablinks'); const tabContents = document.querySelectorAll('.tabcontent'); tabs.forEach(tab => { tab.addEventListener('click', (e) => { const tabName = e.currentTarget.getAttribute('data-tab'); tabs.forEach(t => t.classList.remove('active')); tab.classList.add('active'); tabContents.forEach(content => { content.style.display = 'none'; }); const selectedContent = document.getElementById(tabName); if (selectedContent) { selectedContent.style.display = 'block'; } }); }); chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { if (tabs[0] && tabs[0].url) { urlInput.value = tabs[0].url; generateQRCode(tabs[0].url); } }); generateBtn.addEventListener('click', function() { generateQRCode(urlInput.value); }); function generateQRCode(text) { if (!text) return; qrcodeDiv.innerHTML = ''; qr = new QRCode(qrcodeDiv, { text: text, width: 256, height: 256, colorDark: "#000000", colorLight: "#ffffff", correctLevel: QRCode.CorrectLevel.H }); } // Word转HTML功能 const fileNameDisplay = document.getElementById('fileNameDisplay'); const convertStatus = document.getElementById('convertStatus'); let currentFileName = ''; wordFile.addEventListener('change', function(e) { const file = e.target.files[0]; if (file) { currentFileName = file.name.replace(/\.docx$/i, ''); fileNameDisplay.innerHTML = ` 📄 ${file.name} `; showStatus('loading', '正在转换中...'); const reader = new FileReader(); reader.onload = function(e) { mammoth.convertToHtml({arrayBuffer: e.target.result}) .then(function(result) { convertedHtml = result.value; preview.innerHTML = convertedHtml; downloadBtn.style.display = 'block'; showStatus('success', '转换成功!'); }) .catch(function(error) { console.error(error); showStatus('error', '转换失败:' + error.message); }); }; reader.readAsArrayBuffer(file); } }); function showStatus(type, message) { convertStatus.className = 'status-message ' + type; convertStatus.textContent = message; if (type === 'success') { setTimeout(() => { convertStatus.style.display = 'none'; }, 3000); } } downloadBtn.addEventListener('click', function() { if (!convertedHtml) return; const fullHtml = ` ${currentFileName} ${convertedHtml} `; const blob = new Blob([fullHtml], {type: 'text/html'}); const url = URL.createObjectURL(blob); chrome.downloads.download({ url: url, filename: `${currentFileName}.html`, saveAs: true }); }); // 翻译设置相关 function loadTranslationSettings() { const translateEnabled = document.getElementById('translateEnabled'); const targetLang = document.getElementById('targetLang'); // 检测操作系统 const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0; const shortcutText = isMac ? 'Command+Q' : 'Ctrl+Q'; // 更新显示的快捷键文本 const settingLabel = document.querySelector('.setting-label'); if (settingLabel) { settingLabel.textContent = `启用翻译功能 (${shortcutText})`; } // 加载保存的设置 chrome.storage.sync.get(['translateEnabled', 'targetLang'], function(result) { translateEnabled.checked = result.translateEnabled !== false; // 默认为 true targetLang.value = result.targetLang || 'en'; }); // 监听设置变更 translateEnabled.addEventListener('change', function(e) { const enabled = e.target.checked; chrome.storage.sync.set({ translateEnabled: enabled }, function() { console.log('Translation enabled:', enabled); }); }); targetLang.addEventListener('change', function(e) { const lang = e.target.value; chrome.storage.sync.set({ targetLang: lang }, function() { console.log('Target language:', lang); }); }); } });