// ==UserScript== // @name Web表格导出助手 // @namespace http://tampermonkey.net/ // @version 1.31 // @description 从网页中提取表格并导出为Excel // @match *://*/* // @grant none // @license All Rights Reserved // @require https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.5/xlsx.full.min.js // @downloadURL https://update.greasyfork.icu/scripts/529375/Web%E8%A1%A8%E6%A0%BC%E5%AF%BC%E5%87%BA%E5%8A%A9%E6%89%8B.user.js // @updateURL https://update.greasyfork.icu/scripts/529375/Web%E8%A1%A8%E6%A0%BC%E5%AF%BC%E5%87%BA%E5%8A%A9%E6%89%8B.meta.js // ==/UserScript== (function() { 'use strict'; // 创建菜单按钮 function createMenuButton() { const button = document.createElement('div'); button.innerHTML = '📊'; button.style.cssText = ` position: fixed; top: 20px; left: 20px; background-color: #4CAF50; color: white; padding: 1px 2px; border-radius: 5px; cursor: pointer; z-index: 9999; box-shadow: 0 2px 5px rgba(0,0,0,0.2); `; button.onclick = extractTables; document.body.appendChild(button); } // 生成随机文件名 function generateRandomFileName() { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const randomString = Array.from( {length: 6}, () => chars[Math.floor(Math.random() * chars.length)] ).join(''); const date = new Date(); const dateStr = `${(date.getMonth() + 1).toString().padStart(2, '0')}${date.getDate().toString().padStart(2, '0')}`; return `${document.title || randomString}_${dateStr}`; } // 导出Excel function exportToExcel(tables, index = null) { if (index !== null) { // 导出特定表格 const table = tables[index]; const wb = XLSX.utils.book_new(); const ws = XLSX.utils.aoa_to_sheet(table); XLSX.utils.book_append_sheet(wb, ws, `Table ${index + 1}`); const fileName = `${generateRandomFileName()}_table${index + 1}.xlsx`; XLSX.writeFile(wb, fileName); } else { // 导出所有表格 tables.forEach((table, idx) => { const wb = XLSX.utils.book_new(); const ws = XLSX.utils.aoa_to_sheet(table); XLSX.utils.book_append_sheet(wb, ws, `Table ${idx + 1}`); const fileName = `${generateRandomFileName()}_table${idx + 1}.xlsx`; XLSX.writeFile(wb, fileName); }); } } // 提取表格数据 function extractTables() { const tables = Array.from(document.querySelectorAll('table')).map(table => { // 提取表格数据 return Array.from(table.rows).map(row => Array.from(row.cells).map(cell => cell.innerText.trim()) ); }); if (tables.length === 0) { alert('未找到任何表格'); return; } // 创建对话框 const previewHtml = tables.map((table, index) => `
表格 ${index + 1}
${table.slice(0, 5).map(row => ` ${row.map(cell => ` `).join('')} `).join('')}
${cell}
`).join(''); const dialogHtml = `

选择要导出的表格

${previewHtml}
`; // 创建对话框 const tempDiv = document.createElement('div'); tempDiv.innerHTML = dialogHtml; document.body.appendChild(tempDiv.firstElementChild); document.body.appendChild(tempDiv.lastElementChild); // 添加悬停效果 const previews = document.querySelectorAll('[id^="tablePreview"]'); previews.forEach(preview => { preview.addEventListener('mouseenter', () => { preview.style.transform = 'scale(1.02)'; preview.style.boxShadow = '0 6px 12px rgba(0,0,0,0.15)'; }); preview.addEventListener('mouseleave', () => { preview.style.transform = 'scale(1)'; preview.style.boxShadow = '0 4px 6px rgba(0,0,0,0.1)'; }); }); // 添加事件监听 document.getElementById('exportAllBtn').onclick = () => { exportToExcel(tables); closeDialog(); }; document.getElementById('cancelBtn').onclick = closeDialog; document.getElementById('tableExportOverlay').onclick = closeDialog; // 监听导出特定表格事件 document.addEventListener('exportSpecificTable', (e) => { exportToExcel(tables, e.detail); closeDialog(); }); } // 关闭对话框 function closeDialog() { const dialog = document.getElementById('tableExportDialog'); const overlay = document.getElementById('tableExportOverlay'); if (dialog) dialog.remove(); if (overlay) overlay.remove(); } // 初始化 function init() { createMenuButton(); } // 等待页面加载完成 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();