Greasy Fork is available in English.

【省钱购物助手】-- 领取淘宝天猫优惠券

专注于寻找淘宝天猫商品优惠券,在商品列表和商品详情页显示优惠券,简洁干净无广告,如果商品有优惠券会在商品详情页价格下方出现优惠券二维码,使用淘宝App扫码领券购物更省钱,如果没有表示该商品没有优惠券

// ==UserScript==
// @name         【省钱购物助手】-- 领取淘宝天猫优惠券
// @namespace    https://mmys.club
// @version      0.3
// @description  专注于寻找淘宝天猫商品优惠券,在商品列表和商品详情页显示优惠券,简洁干净无广告,如果商品有优惠券会在商品详情页价格下方出现优惠券二维码,使用淘宝App扫码领券购物更省钱,如果没有表示该商品没有优惠券
// @author       simples
// @match        *://*.taobao.com/*
// @match        *://*.tmall.com/*
// @match        *://*.tmall.hk/*
// @match        *://*.liangxinyao.com/*
// @exclude       *://login.taobao.com/*
// @exclude       *://pages.tmall.com/*
// @require      https://cdn.staticfile.org/jquery/1.12.4/jquery.min.js
// @require      https://cdn.jsdelivr.net/npm/davidshimjs-qrcodejs@0.0.2/qrcode.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    var style = document.createElement('link');
    style.href = 'https://mmys.club/youhuiquan.css';
    style.rel = 'stylesheet';
    style.type = 'text/css';
    document.getElementsByTagName('head').item(0).appendChild(style);
    
    var obj = {};
    obj.initSearchHtml = function (selectorList) {
        setInterval(function () {
            selectorList.forEach(function (selector) {
                obj.initSearchItemSelector(selector);
            });
        }, 3000);
    };

    obj.initSearchEvent = function () {
        $(document).on("click", ".tb-cool-box-area", function () {
            var $this = $(this);
            if ($this.hasClass("tb-cool-box-wait")) {
                obj.basicQueryItem(this);
            } else if ($this.hasClass("tb-cool-box-info-translucent")) {
                $this.removeClass("tb-cool-box-info-translucent");
            } else {
                $this.addClass("tb-cool-box-info-translucent");
            }
        });
    };

    obj.basicQuery = function () {
        setInterval(function () {
            $(".tb-cool-box-wait").each(function () {
                obj.basicQueryItem(this);
            });
        }, 3000);
    };
    
    obj.initSearchItemSelector = function (selector) {
        $(selector).each(function () {
            obj.initSearchItem(this);
        });
    };

    obj.initSearchItem = function (selector) {
        var $this = $(selector);
        if ($this.hasClass("tb-cool-box-already")) {
            return;
        } else {
            $this.addClass("tb-cool-box-already")
        }

        var nid = $this.attr("data-id");
        if (!obj.isVailidItemId(nid)) {
            nid = $this.attr("data-itemid");
        }

        if (!obj.isVailidItemId(nid)) {
            if ($this.attr("href")) {
                nid = location.protocol + $this.attr("href");
            } else {
                var $a = $this.find("a");
                if (!$a.length) {
                    return;
                }

                nid = $a.attr("data-nid");
                if (!obj.isVailidItemId(nid)) {
                    if ($a.hasClass("j_ReceiveCoupon") && $a.length > 1) {
                        nid = location.protocol + $($a[1]).attr("href");
                    } else {
                        nid = location.protocol + $a.attr("href");
                    }
                }
            }
        }

        if (obj.isValidNid(nid)) {
            obj.appenBasicQueryHtml($this, nid);
        }
    };

    obj.appenBasicQueryHtml = function (selector, nid) {
        selector.append('<div class="tb-cool-box-area tb-cool-box-wait" data-nid="' + nid + '"><a class="tb-cool-box-info tb-cool-box-info-default" title="点击查询">待查询</a></div>');
    };

    obj.basicQueryItem = function (selector) {
        var $this = $(selector);
        $this.removeClass("tb-cool-box-wait");

        var nid = $this.attr("data-nid");
        $.get('https://mmys.club:9010/productDetail/'+nid,function(data) {
            if (data.coupon_amount) {
                obj.showBasicQueryFind($this, data.coupon_amount);
            } else {
                obj.showBasicQueryEmpty($this);
            }
        })
    };

    obj.showBasicQueryFind = function (selector, couponMoney) {
        selector.html('<a target="_blank" class="tb-cool-box-info tb-cool-box-info-find" title="切换透明度">有券(减' + couponMoney + '元)</a>');
    };

    obj.showBasicQueryEmpty = function (selector) {
        selector.addClass("tb-cool-box-info-translucent");
        selector.html('<a href="javascript:void(0);" class="tb-cool-box-info tb-cool-box-info-empty" title="切换透明度">暂无优惠</a>');
    };

    obj.isDetailPageTaoBao = function (url) {
        if (url.indexOf("//item.taobao.com/item.htm") > 0 || url.indexOf("//detail.tmall.com/item.htm") > 0 || url.indexOf("//chaoshi.detail.tmall.com/item.htm") > 0 || url.indexOf("//detail.tmall.hk/hk/item.htm") > 0) {
            return true;
        } else {
            return false;
        }
    };

    obj.isVailidItemId = function (itemId) {
        if (!itemId) {
            return false;
        }

        var itemIdInt = parseInt(itemId);
        if (itemIdInt == itemId && itemId > 10000) {
            return true;
        }
        else {
            return false;
        }
    };

    obj.isValidNid = function (nid) {
        if (!nid) {
            return false;
        }
        else if (nid.indexOf('http') >= 0) {
            if (obj.isDetailPageTaoBao(nid) || nid.indexOf("//detail.ju.taobao.com/home.htm") > 0) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return true;
        }
    };

    if (obj.isDetailPageTaoBao(location.href)) {
        var params = location.search.split('?')[1].split('&');
        for (var index in params) {
            if (params[index].split('=')[0] == 'id') {
                var productId = params[index].split('=')[1];
                break;
            }
        }
        $.get('https://mmys.club:9010/productDetail/'+productId,function(data) {
            if (data.coupon_amount) {
                var couponArea = '<div class="coupon-wrap"><div class="coupon"><div class="coupon-info"><div class="coupon-desc" style="font-weight: bold;">优惠券 ' + data.coupon_amount + '元<br /><br /></div><div>使用淘宝app扫描二维码<br />领取优惠券</div></div>'+
                    '<div id="coupon-get" style="padding:10px;"></div></div></div>';
                if (location.href.indexOf('//detail.tmall') != -1) {
                    $('.tm-fcs-panel').after(couponArea);
                } else {
                    $('ul.tb-meta').after(couponArea);
                }
                setTimeout(function() {
                    var qrcode = new QRCode("coupon-get", {
                        text: data.coupon_click_url,
                        width: 128,
                        height: 128,
                        colorDark : "#000000",
                        colorLight : "#ffffff",
                        correctLevel : QRCode.CorrectLevel.H
                    });
                    qrcode.makeCode(data.coupon_click_url);

                }, 500)
            }
        })
    } else {
        var selectorList = [];
        var url = location.href;
        if (url.indexOf("//s.taobao.com/search") > 0 || url.indexOf("//s.taobao.com/list") > 0) {
            selectorList.push(".items .item");
        }
        else if (url.indexOf("//list.tmall.com/search_product.htm") > 0) {
            selectorList.push(".product");
            selectorList.push(".chaoshi-recommend-list .chaoshi-recommend-item");
        }
        else if (url.indexOf("//list.tmall.hk/search_product.htm") > 0) {
            selectorList.push("#J_ItemList .product");
        } else if (document.getElementById('J_ShopSearchResult')) {
            selectorList.push("#J_ShopSearchResult .item");
        }
        if (selectorList && selectorList.length > 0) {
            obj.initSearchHtml(selectorList);
            obj.initSearchEvent();
            obj.basicQuery();
        }
    }
    
})();