Greasy Fork

Greasy Fork is available in English.

淘宝天猫优惠查询助手

优惠查询助手

当前为 2024-11-26 提交的版本,查看 最新版本

// ==UserScript==
// @name         淘宝天猫优惠查询助手
// @namespace   https://www.ishtq.com
// @icon         https://ae01.alicdn.com/kf/Uc280ab6a71d84b01866aa3546d65bcf9c.png
// @version      5.0
// @description  优惠查询助手
// @author       淘宝天猫优惠查询助手
// @match        *://*.tmall.com/*
// @match        *://*.taobao.com/*
// @include      http*://chaoshi.tmall.com/*
// @include      http*://detail.tmall.hk/*
// @include      http*://chaoshi.detail.tmall.com/*
// @antifeature       referral-link 应GreasyFork代码规范要求:含有优惠券查询功能的脚本必须添加此提示!感谢大家的理解...
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // 检测商品详情页标题并返回商品名称
    function getProductName() {
        // 针对天猫商品详情页
        let productNameElement = document.querySelector('.mainTitle--O1XCl8e2, .tb-main-title'); // 兼容天猫和淘宝的标题
        if (productNameElement) {
            return productNameElement.innerText.trim();
        }
        return null;
    }

    // 创建悬浮按钮
    function createFloatingButton() {
        const button = document.createElement('button');
        button.innerText = '查券';
        button.style.position = 'fixed';
        button.style.bottom = '50px';
        button.style.right = '50px';
        button.style.zIndex = '9999';
        button.style.width = '60px';
        button.style.height = '60px';
        button.style.backgroundColor = '#ff5000';
        button.style.color = '#fff';
        button.style.border = 'none';
        button.style.borderRadius = '50%';
        button.style.cursor = 'pointer';
        button.style.boxShadow = '0 4px 8px rgba(0, 0, 0, 0.2)';
        button.style.fontSize = '14px';
        button.style.fontWeight = 'bold';
        button.style.lineHeight = '60px';
        button.style.textAlign = 'center';
        button.style.transition = 'all 0.3s ease';

        // 鼠标悬停样式
        button.addEventListener('mouseover', () => {
            button.style.backgroundColor = '#ff7800';
        });
        button.addEventListener('mouseout', () => {
            button.style.backgroundColor = '#ff5000';
        });

        // 按钮点击事件
        button.addEventListener('click', () => {
            const productName = getProductName();
            if (!productName) {
                alert('未能获取商品名称,请确保当前页面为商品详情页!');
                return;
            }
            const queryUrl = `https://tyfnr.yhzu.cn/?r=/l&kw=${encodeURIComponent(productName)}&origin_id=&sort=0`;
            window.open(queryUrl, '_blank');
        });

        document.body.appendChild(button);
    }

    // 动态监听 DOM 加载
    function observeProductName() {
        const observer = new MutationObserver((mutationsList, observer) => {
            const productName = getProductName();
            if (productName) {
                console.log('商品名称已找到:', productName);
                observer.disconnect(); // 找到商品名称后停止观察
                createFloatingButton(); // 创建按钮
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true,
        });
    }

    // 初始化脚本
    function init() {
        const productName = getProductName();
        if (productName) {
            console.log('商品名称:', productName);
            createFloatingButton();
        } else {
            console.log('未找到商品名称,开始监听 DOM 变化...');
            observeProductName();
        }
    }

    // 启动脚本
    init();
})();