// ==UserScript== // @name 表格粘贴助手 // @namespace http://tampermonkey.net/ // @version 1.0.0 // @description Excel 复制内容逐格填入页面输入框(Shift+Ctrl+C 触发) // @author jbn // @match https://sso.geiwohuo.com/* // @match https://agentseller.temu.com/* // @grant none // @license MIT // ==/UserScript== (function() { 'use strict'; console.log("✅ 粘贴表格脚本加载成功"); /** * 兼容Vue el-input,赋值触发页面响应更新 */ function setInputValue(inputDom, value) { const nativeSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set; nativeSetter.call(inputDom, value ?? ''); inputDom.dispatchEvent(new Event('input', { bubbles: true })); inputDom.dispatchEvent(new Event('change', { bubbles: true })); } /** * 从当前焦点input向上查找tbody,构建【二维数组 tableInputs[row][col] = inputDom】 * @param {HTMLInputElement} focusInput * @returns {{tableInputs: HTMLInputElement[][], startRow: number, startCol: number}} */ function buildTableInputMatrix(focusInput) { // 向上查找表格主体 const tbody = focusInput.closest('tbody'); if (!tbody) throw new Error("当前输入框不在tbody内,无法识别表格"); // 获取所有数据行tr(过滤表头tr) const trList = Array.from(tbody.querySelectorAll('tr')); const tableInputs = []; let startRow = -1; let startCol = -1; for (let r = 0; r < trList.length; r++) { const tr = trList[r]; // 获取当前行内所有input(按页面从左到右顺序) const inputsInRow = Array.from(tr.querySelectorAll('input')); tableInputs.push(inputsInRow); // 查找焦点input所在行列坐标 const colIndex = inputsInRow.indexOf(focusInput); if (colIndex > -1) { startRow = r; startCol = colIndex; } } if (startRow === -1 || startCol === -1) throw new Error("无法定位焦点输入框在表格中的位置"); return { tableInputs, startRow, startCol }; } let open = false; document.addEventListener("keydown", (event) => { if (event.shiftKey && event.ctrlKey) { open = true; } }); document.addEventListener("keyup", (event) => { if (event.shiftKey || event.ctrlKey) { open = false; } }); document.addEventListener('paste', function (e) { if (!open) { return; } e.preventDefault(); const focusEl = document.activeElement; // 校验:聚焦元素必须是原生input if (focusEl.tagName !== 'INPUT') { console.log("❌焦点不是input,终止执行"); return; } try { // 构建二维表格Input矩阵 + 获取起始坐标 const { tableInputs, startRow, startCol } = buildTableInputMatrix(focusEl); console.log(`📍起始位置:行${startRow},列${startCol}`); // 读取剪贴板Excel数据,解析二维数组 const pasteText = e.clipboardData.getData('text/plain').trim(); if (!pasteText) return; const excelData = pasteText .split(/\r?\n/) .map(line => line.split('\t').map(cell => cell.trim())); // 逐行逐列填充 for (let dr = 0; dr < excelData.length; dr++) { const targetRowIdx = startRow + dr; const rowData = excelData[dr]; // 超出表格行数,停止填充 if (!tableInputs[targetRowIdx]) break; for (let dc = 0; dc < rowData.length; dc++) { const targetColIdx = startCol + dc; const rowInputs = tableInputs[targetRowIdx]; // 超出当前行input数量,跳过本单元格 if (!rowInputs[targetColIdx]) continue; setInputValue(rowInputs[targetColIdx], rowData[dc]); } } console.log(`✅填充完成,共粘贴${excelData.length}行数据`); } catch (err) { alert("填充失败:" + err.message); console.error(err); } }); })();