// ==UserScript== // @name 京东图书定价查询助手(手动防封版) // @namespace http://tampermonkey.net/ // @version 2.9-Manual // @description 手动触发查询,防封 // @author M // @match *://t.jd.com/* // @match *://guanzhu.jd.com/* // @match *://item.jd.com/* // @grant GM_xmlhttpRequest // @connect item.m.jd.com // ==/UserScript== (function() { 'use strict'; const CONFIG = { FOLLOW: { card: '.item-inner', price: '.p-price' }, ITEM: { anchor: '.product-price-panel' } }; let lastItemSku = null; function getPageType() { if (window.location.host.includes('item.jd.com')) return 'ITEM'; return 'FOLLOW'; } const currentPage = getPageType(); console.log(`🚀 京东图书定价查询助手(手动防封版) 启动!请点击 [🔍查定价] 按钮获取价格。`); function fetchPrice(sku, btn, targetContainer) { btn.innerText = "⏳查询中..."; btn.disabled = true; btn.style.cursor = "not-allowed"; btn.style.background = "#999"; GM_xmlhttpRequest({ method: "GET", url: `https://item.m.jd.com/product/${sku}.html`, timeout: 6000, onload: function(response) { if (response.finalUrl && response.finalUrl.includes('risk_handler')) { btn.innerText = "🚫被拦截(稍后再试)"; btn.style.background = "#666"; return; } try { const html = response.responseText; let marketPrice = null; let currentPrice = null; let mMatch = html.match(/"linePrice"\s*:\s*\{[^}]*?"price"\s*:\s*["'](\d+\.\d{1,2})["']/i); if (mMatch) marketPrice = parseFloat(mMatch[1]); let pMatch = html.match(/"jdPrice"\s*:\s*["'](\d+\.\d{1,2})["']/i); if (pMatch) currentPrice = parseFloat(pMatch[1]); if (marketPrice) { const discount = (currentPrice && marketPrice > 0) ? ((currentPrice / marketPrice) * 10).toFixed(2) : null; injectDisplay(targetContainer, marketPrice, discount, sku, btn); } else { btn.innerText = "⚠️无定价(通常为套装)"; btn.style.background = "#f0ad4e"; } } catch (e) { btn.innerText = "❌解析失败"; btn.style.background = "#d9534f"; } }, onerror: function() { btn.innerText = "❌网络超时(重试)"; btn.disabled = false; btn.style.cursor = "pointer"; btn.style.background = "#e4393c"; } }); } function injectDisplay(container, mPrice, discount, sku, btn) { if (document.querySelector(`.gemini-tag-${sku}`)) return; const isDetailPage = (currentPage === 'ITEM'); const wrap = document.createElement('span'); wrap.className = `gemini-tag-${sku}`; wrap.style.cssText = isDetailPage ? "display:inline-block; margin-left:10px; color:#999; font-size:13px;" : "display:inline-block; margin-left:6px; color:#999; font-size:12px;"; wrap.innerHTML = `定价:¥${mPrice}${discount ? ` [${discount}折]` : ''}`; if (btn && btn.parentNode) { btn.parentNode.replaceChild(wrap, btn); } else { if (isDetailPage) { const anchor = document.querySelector('.product-price-gray-line-through') || document.querySelector('.product-price--gray-line-through') || container; if(anchor && anchor.parentNode) anchor.parentNode.appendChild(wrap); } else { container.appendChild(wrap); } } } function createButton(sku, container) { const btn = document.createElement('button'); btn.className = `gemini-btn-${sku}`; btn.innerText = "🔍查定价"; btn.style.cssText = "display:inline-block; margin-left:8px; padding:2px 8px; font-size:12px; color:#fff; background:#e4393c; border:none; border-radius:12px; cursor:pointer; outline:none; transition:all 0.2s;"; btn.onclick = function(e) { e.preventDefault(); e.stopPropagation(); fetchPrice(sku, btn, container); }; return btn; } function scan() { if (currentPage === 'ITEM') { const skuMatch = window.location.href.match(/\/(\d+)\.html/); if (skuMatch) { const sku = skuMatch[1]; if (lastItemSku && lastItemSku !== sku) { document.querySelectorAll('[class^="gemini-tag-"], [class^="gemini-btn-"]').forEach(el => el.remove()); } lastItemSku = sku; if (!document.querySelector(`.gemini-tag-${sku}`) && !document.querySelector(`.gemini-btn-${sku}`)) { const anchor = document.querySelector(CONFIG.ITEM.anchor); if (anchor) { const btn = createButton(sku, anchor); // 兼容新老灰色类名 const grayPrice = document.querySelector('.product-price-gray-line-through') || document.querySelector('.product-price--gray-line-through'); if (grayPrice && grayPrice.parentNode) { grayPrice.parentNode.insertBefore(btn, grayPrice.nextSibling); } else if (anchor.parentNode) { // 🌟 越狱修复:直接插入到父容器的末尾,模仿原 V2.9 暴力绕过 overflow 限制 anchor.parentNode.appendChild(btn); } } } } } else { document.querySelectorAll(CONFIG.FOLLOW.card).forEach(card => { const link = card.querySelector('a[href*="jd.com"]'); const priceArea = card.querySelector(CONFIG.FOLLOW.price); if (link && priceArea) { const match = link.href.match(/\/(\d{6,15})/); if (match) { const sku = match[1]; if (!card.querySelector(`.gemini-btn-${sku}`) && !card.querySelector(`.gemini-tag-${sku}`)) { const btn = createButton(sku, priceArea); priceArea.appendChild(btn); } } } }); } } setInterval(scan, 1500); })();