134 lines
3.6 KiB
JavaScript
134 lines
3.6 KiB
JavaScript
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 = '';
|
|
|
|
const tabs = document.querySelectorAll('.tablinks');
|
|
const tabContents = document.querySelectorAll('.tabcontent');
|
|
|
|
tabs.forEach(tab => {
|
|
tab.addEventListener('click', (e) => {
|
|
const tabName = e.target.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 = `
|
|
<span class="file-name" title="${file.name}">
|
|
📄 ${file.name}
|
|
</span>
|
|
`;
|
|
|
|
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 = `
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>${currentFileName}</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; line-height: 1.6; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
${convertedHtml}
|
|
</body>
|
|
</html>
|
|
`;
|
|
|
|
const blob = new Blob([fullHtml], {type: 'text/html'});
|
|
const url = URL.createObjectURL(blob);
|
|
|
|
chrome.downloads.download({
|
|
url: url,
|
|
filename: `${currentFileName}.html`,
|
|
saveAs: true
|
|
});
|
|
});
|
|
});
|