// ==UserScript== // @name icardyou-batch // @namespace https://github.com/liushidai/icardyou-js // @version 2025-10-21-1.1 // @description 批量导出为excel、pdf;批量确认 // @author liushidai // @match https://icardyou.icu/sendpostcard/myPostCard/1* // @match https://www.icardyou.icu/sendpostcard/myPostCard/1* // @match https://www.icardyou.com/sendpostcard/myPostCard/1* // @match https://icardyou.com/sendpostcard/myPostCard/1* // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== // @grant GM_addElement // @grant GM_log // @require https://code.jquery.com/jquery-3.6.0.min.js // @require https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js // @run-at document-body // ==/UserScript== (function () { 'use strict'; GM_log("Hello World"); function addCheckbox() { // 每行添加多选框 $('tbody tr:not(:first)').each(function () { var checkbox = $('', { type: 'checkbox', class: 'row-checkbox' }); $(this).prepend('').find('td:first').append(checkbox); }); // 为首行添加全选框并设置点击事件 var allCheckbox = $('', { type: 'checkbox', id: 'all-checkbox' }); $('tbody tr:first').prepend('').find('th:first').append(allCheckbox); // 全选框点击处理 $('#all-checkbox').click(function () { var isChecked = $(this).is(':checked'); $('.row-checkbox').prop('checked', isChecked); }); // 行内多选框点击处理 $('.row-checkbox').click(function () { var allChecked = $('.row-checkbox').length === $('.row-checkbox:checked').length; $('#all-checkbox').prop('checked', allChecked); }); } /** * 添加导出选项框 */ function addExportModal() { if (document.getElementById('exportModal')) return; // 插入样式 const style = document.createElement('style'); style.textContent = ` #exportModal { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); display: none; /* 👈 默认隐藏!非常重要 */ align-items: center; justify-content: center; z-index: 9999; } /* 其他样式保持不变 */ #exportModal .modal-content { background: white; padding: 20px; border-radius: 8px; text-align: center; max-width: 300px; width: 90%; } #exportModal button { margin: 8px; padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; } #exportModal #exportExcelBtn { background: #5cb85c; color: white; } #exportModal #exportPdfBtn { background: #d9534f; color: white; } #exportModal .close { position: absolute; top: 10px; right: 15px; font-size: 24px; cursor: pointer; color: #999; } `; document.head.appendChild(style); const modal = document.createElement('div'); modal.id = 'exportModal'; modal.innerHTML = ` `; document.body.appendChild(modal); // 关闭逻辑 modal.querySelector('.close').onclick = () => modal.style.display = 'none'; modal.onclick = (e) => { if (e.target === modal) modal.style.display = 'none'; }; // 按钮逻辑 modal.querySelector('#exportExcelBtn').onclick = () => { modal.style.display = 'none'; if (window.__selectedRowDatas) { getAddressAndExportExcel(window.__selectedRowDatas); window.__selectedRowDatas = null; } }; modal.querySelector('#exportPdfBtn').onclick = () => { modal.style.display = 'none'; if (window.__selectedRowDatas) { const rowDatas = window.__selectedRowDatas; const resultArray = []; let completed = 0; if (rowDatas.length === 0) { alert('无数据可导出'); return; } rowDatas.forEach(param => { getAddress(param.id, function (response) { resultArray.push({ cardType: param.cardType, no: param.no, status: param.status, zip: response.zip || '', realName: response.realName || '', address: response.address || '' }); completed++; if (completed === rowDatas.length) { resultArray.sort((a, b) => a.id - b.id); previewForPrint(resultArray); // 👈 改为调用打印预览 } }); }); window.__selectedRowDatas = null; } }; } /** * 添加导出按钮 与 批量确认按钮 */ function addButton() { // 选择tbody中的第一行tr元素 var firstRow = $('tbody tr:first'); // 创建按钮元素 var exportExcel = $(' ${cardWidth}px `); data.forEach(item => { const top = `类型: ${item.cardType} | 状态: ${item.status}`; const bottomLines = [ `卡片编号: ${item.no || ''}`, `邮编: ${item.zip || ''}`, `姓名: ${item.realName || ''}`, `地址: ${item.address || ''}` ].join('\n'); doc.write(`
${top}
${bottomLines}
`); }); doc.write(` `); doc.close(); }; updateContent(); // 提供外部调用接口 printWin.setFontSize = (size) => { if (size >= 8 && size <= 32) { fontSize = size; updateContent(); } }; printWin.setCardWidth = (width) => { // 限制合理范围,比如 150px ~ 600px cardWidth = Math.max(150, Math.min(600, width)); updateContent(); // 更新显示 const display = printWin.document.getElementById('width-display'); if (display) display.textContent = cardWidth + 'px'; }; } // 添加多选框 addCheckbox(); // 添加导出选项框 addExportModal(); // 添加操作按钮 addButton(); // Your code here... })();