京东秒杀价格过滤

按价格过滤并排序(价格升序)秒杀商品列表

当前为 2017-10-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         京东秒杀价格过滤
// @version      0.3.0.10
// @icon         https://www.jd.com/favicon.ico
// @description  按价格过滤并排序(价格升序)秒杀商品列表
// @author       You!
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @include      *://miaosha.jd.com/pinpai.html?brand_id=*
// @include      *://miaosha.jd.com/brand.html?brand_id=*
// @include      *://miaosha.jd.com/category.html?cate_id=*
// @run-at       document-end
// @namespace    https://gf.qytechs.cn/zh-CN/scripts/33454-京东秒杀价格过滤
// ==/UserScript==

(function() {
    'use strict';

    /**********************************************************/
    //如果要过滤商品名,启用该函数,但是需要自己实现过滤逻辑:
    //    pname 传入商品名
    //    catId 传入商品分类编号,编号代表的意义请自行对照京东秒杀的页面地址
    //    命中返回 true, 否侧返回 false
    /*在行首插入单行注释(//)以启用该商品名过滤函数*/
    function filterPName(pname, cat, catId) {
        switch(cat) {
            case 'pinpai': //品牌秒杀
                break;

            case 'brand': //品类秒杀
                break;

            case 'category': //分类秒杀
                if (catId == 34) { //潮流鞋靴
                    if (!pname.match('男')) return false;
                }
                break;
        }
        return true;
    }
    /**********************************************************/

    //扫描页面的间隔频率时间
    var timerFreq = 1000;

    var currentPageKey = 'jd_miaosha_' + location.pathname.split(/[\/\.]/)[1].toLowerCase() + '_' + location.search.split('&')[0].split('=')[1];
    var priceInfo = loadPagePrice(currentPageKey);

    uiInit();
    doFilter();

    //向页面右下角的浮动工具栏尾部追加价格过滤部件
    function uiInit() {
        var uiPos = $('#sk_mod_er ul');
        if (uiPos.length > 0) {
            var checked = priceInfo.checked ? ' checked="true"' : '';
            uiPos.append($(
                '<li class="sk_mod_er_mobile" style="color:white;padding:7px;float:left;text-align:left">'+
                '<nobr>价格范围:<a id="fltReload" href="javascript:;" style="color:white;font-weight:700">刷新</a></nobr><br>'+
                '<input id="fltPriceMin" type="number" style="width:80px;margin-top:3px" value="'+priceInfo.min+'"/><br/>'+
                '<input id="fltPriceMax" type="number" style="width:80px;margin-top:5px" value="'+priceInfo.max+'"/><br/>'+
                '<nobr><input id="fltPriceOne" type="checkbox" style="margin:6px -3px 0px 0px"'+checked+'"/><label for="fltPriceOne" style="float:right;margin-top:2px">使用独立设置</label></nobr><br/>'+
                '</li>'
            ));
            var onPriceChange = function() {
                setTimeout(function() {
                    saveCurrentPagePrice();
                }, 0);
            };
            $('#fltReload').first().click(function() {
                setTimeout(function() {
                    saveCurrentPagePrice();
                    doFilter();
                }, 0);
            });
            $('#fltPriceMin').first().change(onPriceChange);
            $('#fltPriceMax').first().change(onPriceChange);
            $('#fltPriceOne').first().click(function() {
                var oldChecked = !this.checked;
                setTimeout(function() {
                    saveCurrentPagePrice(oldChecked);
                    $('#fltPriceMin').attr('value', priceInfo.min);
                    $('#fltPriceMax').attr('value', priceInfo.max);
                }, 0);
            });
        } else setTimeout(uiInit, timerFreq);
    }

    //对页面执行过滤排序操作
    function doFilter() {
        var count = 0;
        switch(location.pathname.toLowerCase()) {
            case '/': //秒杀首页
                //count += filterGoods($('div.skwrap')); //正在抢购
                //count += filterGoods($('div.moregoods')); //更多好货
                break;

            case '/pinpai.html': //品牌秒杀
            case '/brand.html': //品类秒杀
                count += filterGoods($('div.bprd'));
                break;

            case '/category.html': //分类秒杀
                count += filterGoods($('div.catinfo_seckillnow')); //当天正在秒杀的商品
                count += filterGoods($('div.catinfo_startsoon')); //明日秒杀的商品
                break;
        }
        if (count === 0) setTimeout(doFilter, timerFreq);
    }

    //对页面内的一个具体分类执行过滤排序操作
    function filterGoods(goodsList) {
        var goods = goodsList.find('li.seckill_mod_goods');
        if (goods.length > 0) {
            var groups = groupObjs(goods, function(k, o) {
                o = $(o);
                //记录原始索引位置
                if (o.attr('idx') === undefined) o.attr('idx', k);
                //按商品名过滤
                if (typeof(filterPName) === 'function') {
                    var pname = getProductName(o);
                    if (!filterPName(pname, priceInfo.pageCat, priceInfo.pageCatId)) return false;
                }
                //按价格过滤
                var price = getPrice(o);
                if (price.now < priceInfo.min || (0 < priceInfo.max && priceInfo.min <= priceInfo.max && priceInfo.max < price.now)) return false;
                //标注优惠力度
                var priceOff = price.pre - price.now;
                var discount = price.now / price.pre;
                if (discount <= 0.1) {
                    o.find('div.seckill_mod_goods_info').attr('title', '1折以下(优惠'+priceOff+')').css('background-color', 'gold'); //1折以下
                } else if (discount <= 0.3) {
                    o.find('div.seckill_mod_goods_info').attr('title', '3折以下(优惠'+priceOff+')').css('background-color', 'springgreen'); //3折以下
                } else if (discount <= 0.5) {
                    o.find('div.seckill_mod_goods_info').attr('title', '5折以下(优惠'+priceOff+')').css('background-color', '#BBFFEE'); //5折以下
                } else if (discount <= 0.75) {
                    o.find('div.seckill_mod_goods_info').attr('title', '75折以下(优惠'+priceOff+')').css('background-color', '#CCEEFF'); //75折以下
                } else if (priceOff >= 90) {
                    o.find('div.seckill_mod_goods_info').attr('title', '优惠超过90('+priceOff+')').css('background-color', '#FFEEEE'); //优惠超过90
                } else {
                    o.find('div.seckill_mod_goods_info').removeAttr('title').css('background-color', '');
                }
                //命中
                return true;
            });

            //排序
            var niceGoods = groups[true];
            if (niceGoods.length > 0) {
                niceGoods.sort(function(g1, g2){
                    return getPrice(g1).now - getPrice(g2).now || //<---------------------------------------如果要降序,修改这里
                        $(g1).attr('idx') - $(g2).attr('idx');
                });
                //取消图片延迟加载
                forceLoadLazyImgs(niceGoods);
            } else niceGoods = $('<center><h2><br/>该分类下所有商品的价格都不符合过滤条件。<br/></h2></center');

            //替换原内容
            $(niceGoods).css('display', '');
            $(groups[false]).css('display', 'none');
            $(goods[0].closest('ul')).prepend($(niceGoods).remove());
        }
        return goods.length;
    }

    //取消图片延迟加载
    function forceLoadLazyImgs(elms) {
        var lazyImgs = $(elms).find('img[data-lazy-img][data-lazy-img!="done"]');
        if (lazyImgs.length > 0) lazyImgs.each(function(idx){
            var img = $(this);
            this.src = img.attr('data-lazy-img');
            img.removeAttr('data-lazy-img');
        });
    }

    //加载/保存页面价格设置
    function loadPagePrice(pageKey) {
        var g_min = GM_getValue('jd_miaosha_price_min');
        var g_max = GM_getValue('jd_miaosha_price_max');
        if (g_min === undefined) g_min = 0;
        if (g_max === undefined) g_max = 0;

        var p_min = GM_getValue(pageKey + '_price_min');
        var p_max = GM_getValue(pageKey + '_price_max');
        if (p_min === undefined) p_min = 0;
        if (p_max === undefined) p_max = 0;

        var checked = GM_getValue(pageKey);
        var min = checked ? p_min : g_min;
        var max = checked ? p_max : g_max;

        var parts = pageKey.split('_');
        return {
            pageKey: pageKey,
            pageCat: parts[parts.length - 2],
            pageCatId: parts[parts.length - 1],
            checked: checked,
            min: min,
            max: max,
            p_min: p_min,
            p_max: p_max,
            g_min: g_min,
            g_max: g_max,
        };
    }
    function saveCurrentPagePrice(oldChecked) {
        var min = getFloat($('#fltPriceMin').first().val());
        var max = getFloat($('#fltPriceMax').first().val());
        var keyMin, keyMax;

        //更新
        priceInfo.checked = $('#fltPriceOne').first().is(':checked');
        if (oldChecked === undefined) oldChecked = priceInfo.checked;
        if (oldChecked) {
            priceInfo.p_min = min;
            priceInfo.p_max = max;
            keyMin = currentPageKey + '_price_min';
            keyMax = currentPageKey + '_price_max';
        }
        else {
            priceInfo.g_min = min;
            priceInfo.g_max = max;
            keyMin = 'jd_miaosha_price_min';
            keyMax = 'jd_miaosha_price_max';
        }
        if (priceInfo.checked) {
            priceInfo.min = priceInfo.p_min;
            priceInfo.max = priceInfo.p_max;
        }
        else {
            priceInfo.min = priceInfo.g_min;
            priceInfo.max = priceInfo.g_max;
        }

        //保存
        if (min === 0) GM_deleteValue(keyMin); else GM_setValue(keyMin, min);
        if (max === 0) GM_deleteValue(keyMax); else GM_setValue(keyMax, max);
        if (priceInfo.checked || priceInfo.p_min > 0 || priceInfo.p_max > 0)
            GM_setValue(currentPageKey, priceInfo.checked);
        else GM_deleteValue(currentPageKey);
    }

    //获取商品名
    function getProductName(elm) {
        return $(elm).find('.seckill_mod_goods_title').first().text().trim();
    }
    //获取商品价格
    function getPrice(elm) {
        return {
            now: getFloat($(elm).find('.seckill_mod_goods_price_now').first().text()),
            pre: getFloat($(elm).find('.seckill_mod_goods_price_pre').first().text())
        };
    }
    //字串转浮点数
    function getFloat(str) {
        str = str.replace('¥','').replace('¥','').trim();
        if (!/^-?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?$/.test(str)) return 0; //非浮点数字串
        return parseFloat(str);
    }
})();

function groupObjs(objs, callback) {
    if (objs instanceof jQuery) objs = $.makeArray(objs);
    var ret = {};
    for(var i=0; i<objs.length; ++i){
        var obj = objs[i];
        var key = callback(i, obj);
        if(!(key in ret)) ret[key] = [];
        ret[key].push(obj);
    }
    return ret;
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址