// ==UserScript== // @name KJL后台-商品列表添加更新模型 // @namespace Violentmonkey Scripts // @match https://www.kujiale.com/pub/saas/admin/brandgoods/all* // @match https://www.kujiale.com/pub/saas/workbench/brandgoods/all* // @grant none // @version 1.0 // @author hejie13250 // @description 2025/2/26 10:15:58 // ==/UserScript== (function() { 'use strict'; // 配置参数 const CONFIG = { ROW_SELECTOR: 'tr.StyledBodyRowTr-kSBvgt', // 行选择器 TARGET_TD_POSITION: 'nth-last-child(2)', // 按钮插入位置 HEADER_SELECTOR: '.StyledTable-diJSpn > thead:nth-child(2) > tr', // 表头选择器 DEBUG_MODE: false // 调试模式 }; let idColumnIndex = null; // 动态ID列索引 // 调试日志 function log(...args) { if (CONFIG.DEBUG_MODE) console.log('[KUJIALE]', ...args); } // 初始化ID列索引 function initIdColumnIndex() { const headerRow = document.querySelector(CONFIG.HEADER_SELECTOR); if (!headerRow) { log('表头行未找到'); return false; } const headers = headerRow.querySelectorAll('th'); for (let i = 0; i < headers.length; i++) { if (headers[i].textContent.includes('商品ID')) { idColumnIndex = i + 1; log('ID列索引:', idColumnIndex); return true; } } log('未找到"商品ID"列'); return false; } // 通用按钮创建函数 function createButton(text, url) { const btn = document.createElement('div'); btn.className = 'custom-update-btn'; btn.style.cssText = 'display: inline-block;'; const link = document.createElement('a'); link.className = 'StyledLink-kjrCJO gvhEJs StyledInlineButton-fyUImi djGvpx WrapInlineButton-nnTTR kUBFEP Link-qfJUS dtwGsR'; link.style.cssText = 'cursor: pointer; color: rgb(51, 139, 255);'; link.textContent = text; // 点击事件处理 link.addEventListener('click', function(e) { e.preventDefault(); e.stopPropagation(); // 向上查找数据行 const row = this.closest(CONFIG.ROW_SELECTOR); if (!row) { log('未找到数据行元素'); return; } // 动态获取ID列 const idCell = row.querySelector(`td:nth-child(${idColumnIndex})`); if (!idCell) { log('ID列元素不存在'); return; } // 兼容多层嵌套内容 const actualElement = idCell.querySelector('span, div') || idCell; const currentId = (actualElement.textContent || '').trim(); if (!currentId) { log('获取到空ID'); return; } log('成功获取ID:', currentId); window.open(`${url}${encodeURIComponent(currentId)}`, '_blank'); }); btn.appendChild(link); return btn; } // 创建“更新模型”按钮 function createUpdateButton() { return createButton('更新模型', `https://www.kujiale.com/vc/editor/globalvariable?tooltype=cabinet¤tTab=model-DAG&bgId=`); } // 创建“关联模型”按钮 function createLinkButton() { return createButton('关联模型', `https://www.kujiale.com/pub/saas/brandgoods/group/list?obsBrandGoodsId=`); } // 按钮注入逻辑 function addButtons() { if (idColumnIndex === null) { if (!initIdColumnIndex()) { log('无法初始化ID列索引,按钮注入中止'); return; } } document.querySelectorAll(CONFIG.ROW_SELECTOR).forEach(row => { const targetTd = row.querySelector(`td:${CONFIG.TARGET_TD_POSITION}`); if (!targetTd) { log('目标单元格不存在'); return; } // 使用更稳定的容器选择方式 const container = targetTd.querySelector('div:first-child'); if (container && !container.querySelector('.custom-update-btn')) { container.append(createUpdateButton()); container.append(createLinkButton()); log('按钮注入成功'); } }); } // DOM观察器,监听表格动态加载 const observer = new MutationObserver(mutations => { if (document.querySelector(CONFIG.ROW_SELECTOR)) { addButtons(); } }); // 启动观察 observer.observe(document.body, { childList: true, subtree: true, attributes: false }); // 初始执行 if (document.readyState === 'complete') { addButtons(); } else { window.addEventListener('load', addButtons); } })();