Amazon 购买数量高亮

[EN] Highlights purchase quantity on Amazon | [CN] 高亮显示Amazon商品购买数量

目前为 2025-04-09 提交的版本。查看 最新版本

// ==UserScript==
// @name         Amazon 购买数量高亮
// @namespace    https://greasyfork.org/zh-CN/users/your-profile-id
// @version      1.0.2
// @description  [EN] Highlights purchase quantity on Amazon | [CN] 高亮显示Amazon商品购买数量
// @author       再见梵高 <[email protected]>
// @license      MIT
// @match          *://www.amazon.com/*
// @match          *://www.amazon.ca/*
// @match          *://www.amazon.co.uk/*
// @match          *://www.amazon.de/*
// @match          *://www.amazon.fr/*
// @match          *://www.amazon.it/*
// @match          *://www.amazon.es/*
// @icon         https://www.amazon.com/favicon.ico
// @grant        GM_addStyle
// @supportURL   mailto:[email protected]
// ==/UserScript==

(function() {
    'use strict';

    // 配置参数
    const CONFIG = {
        highlightClass: 'amz-pq-highlight',
        styleRules: {
            color: '#FF4444 !important',
            fontSize: '18px !important',
            fontWeight: 'bolder !important',
            textShadow: '0 0 3px rgba(255,68,68,0.4) !important',
            animation: 'pulse 1s ease-in-out infinite'
        },
        textPatterns: [
            /(\d+,?)+\s?\+?\s?(bought|purchased|sold)/i,
            /multiple\s+times/i,
            /past\s+(month|week)/i
        ]
    };

    // 添加动态样式
    GM_addStyle(`
        .${CONFIG.highlightClass} {
            color: ${CONFIG.styleRules.color};
            font-size: ${CONFIG.styleRules.fontSize};
            font-weight: ${CONFIG.styleRules.fontWeight};
            text-shadow: ${CONFIG.styleRules.textShadow};
            animation: ${CONFIG.styleRules.animation};
        }

        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }

        /* 覆盖亚马逊默认颜色 */
        .a-color-secondary.${CONFIG.highlightClass} {
            color: ${CONFIG.styleRules.color} !important;
        }
    `);

    // 目标元素检测
    function checkElement(el) {
        return CONFIG.textPatterns.some(pattern => 
            pattern.test(el.textContent) && 
            el.classList.contains('a-color-secondary') &&
            el.closest('.a-row.a-size-base')
        );
    }

    // 应用高亮样式
    function applyHighlight() {
        document.querySelectorAll('span.a-color-secondary').forEach(span => {
            if (checkElement(span) && !span.classList.contains(CONFIG.highlightClass)) {
                // 保留原始结构应用样式
                const clone = span.cloneNode(true);
                clone.classList.add(CONFIG.highlightClass);
                span.parentNode.replaceChild(clone, span);
            }
        });
    }

    // 初始化执行
    applyHighlight();

    // 优化后的MutationObserver配置
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.addedNodes.length) {
                applyHighlight();
            }
        });
    });

    // 监听商品信息容器
    const containers = [
        '#dp-container',
        '#centerCol',
        '#mainContent',
        '#ppd'
    ].map(sel => document.querySelector(sel));

    containers.forEach(container => {
        if (container) {
            observer.observe(container, {
                childList: true,
                subtree: true,
                attributes: false,
                characterData: true
            });
        }
    });

    // 处理SPA页面切换
    document.addEventListener('page:change', applyHighlight);
    window.addEventListener('spa-navigate', applyHighlight);
})();