// ==UserScript== // @name 二手车摆渡人268V汽车之家车型采集*全自动版 // @namespace http://tampermonkey.net/ // @version 1.63 // @description 按照图片样式进行段落排列采集,自动复制并退出 // @author YanXiaofei Assistant // @match *://www.autohome.com.cn/spec/* // @grant GM_setClipboard // @grant GM_addStyle // ==/UserScript== (function() { 'use strict'; // 1. 样式 (居中对话框) GM_addStyle(` #custom-dialog-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 99999; display: flex; justify-content: center; align-items: center; } #custom-dialog { background: #fff; padding: 25px; border-radius: 12px; width: 320px; text-align: center; box-shadow: 0 5px 20px rgba(0,0,0,0.3); } #collect-btn { background: #007bff; color: #fff; border: none; padding: 12px 25px; border-radius: 6px; cursor: pointer; font-size: 16px; font-weight: bold; width: 100%; } #collect-btn:hover { background: #0056b3; } .data-preview-box { text-align: left; font-size: 14px; margin: 15px 0; color: #333; background: #f8f9fa; padding: 12px; border-radius: 8px; line-height: 1.6; white-space: pre-wrap; /* 保持换行显示 */ } .auto-status { color: #28a745; font-weight: bold; margin-top: 10px; font-size: 14px; } `); // 2. 深度采集逻辑 function deepCollect() { const urlMatch = window.location.pathname.match(/\/spec\/(\d+)/); const pageId = urlMatch ? urlMatch[1] : 'ID未识别'; let brand = '', series = '', model = ''; // 获取面包屑内容 const breadTexts = Array.from(document.querySelectorAll('.breadnav a, .tw-text-hover, .breadnav span')) .map(el => el.innerText.trim()) .filter(txt => txt && !['首页', '选车', '>'].includes(txt)); if (breadTexts.length >= 2) { brand = breadTexts[0]; series = breadTexts[1]; } // 获取车型 const allSpans = Array.from(document.querySelectorAll('span, h1, .subnav-title-name')); for (let el of allSpans) { const txt = el.innerText.trim(); if (txt.includes('款') && txt.length > 5 && txt !== brand && txt !== series) { model = txt; break; } } // 兜底方案 if (!brand || !series) { const title = document.title.split(' ')[0]; if (title) brand = title.substring(0, 2); } return { brand, series, model, pageId }; } // 3. 弹出对话框并执行自动化逻辑 function initApp() { if (document.getElementById('custom-dialog-overlay')) return; const overlay = document.createElement('div'); overlay.id = 'custom-dialog-overlay'; overlay.innerHTML = `
`; document.body.appendChild(overlay); const preview = document.getElementById('data-preview'); const btn = document.getElementById('collect-btn'); const autoMsg = document.getElementById('auto-msg'); let hasAutoCopied = false; const timer = setInterval(() => { const data = deepCollect(); if (data.brand && data.series && data.model) { // 按照图片样式:分行排列 const formattedResult = `${data.brand}\n${data.series}\n${data.model}\n${data.pageId}`; // 更新预览(显示分行效果) preview.innerText = formattedResult; if (!hasAutoCopied) { hasAutoCopied = true; GM_setClipboard(formattedResult); autoMsg.innerText = '✅ 已按照段落格式自动复制!'; btn.innerText = '复制成功'; btn.style.background = '#28a745'; setTimeout(() => { clearInterval(timer); overlay.remove(); }, 20000); } } }, 500); btn.onclick = () => { const data = deepCollect(); const formattedResult = `${data.brand}\n${data.series}\n${data.model}\n${data.pageId}`; GM_setClipboard(formattedResult); overlay.remove(); }; document.getElementById('close-dialog').onclick = () => { clearInterval(timer); overlay.remove(); }; } if (document.readyState === 'complete') { initApp(); } else { window.addEventListener('load', initApp); } })();