京东秒杀价格过滤

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

当前为 2017-09-26 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         京东秒杀价格过滤
// @version      0.2.1.3
// @icon         https://www.jd.com/favicon.ico
// @description  按价格过滤并排序(价格升序)秒杀商品列表
// @author       You!
// @grant        GM_setValue
// @grant        GM_getValue
// @match        *://miaosha.jd.com/category.html?cate_id=*
// @run-at       document-end
// @namespace    http://tampermonkey.net/
// ==/UserScript==

(function() {
    //向页面右下角的浮动工具栏尾部追加价格过滤部件
    var uiInit = function() {
        clearInterval(priceUI);
        var uiPos = $('#sk_mod_er');
        if (uiPos.length > 0) {
            uiPos.append($(
                '<div class="sk_mod_er_mobile" style="color:white;padding:7px;float:left;text-align:left">'+
                '<nobr>价格范围:<a id="fltReload" href style="color:white;font-weight:700" title="将会重新加载页面">刷新</a></nobr><br>'+
                '<input id="fltPriceMin" type="number" style="width:80px;margin-top:3px" value="'+GM_getValue('jd_miaosha_price_min')+'"/><br/>'+
                '<input id="fltPriceMax" type="number" style="width:80px;margin-top:5px" value="'+GM_getValue('jd_miaosha_price_max')+'"/><br/>'+
                '</div>'
            ));
            $('#fltReload').first().click(function(){
                priceMin = getFloat($('#fltPriceMin').first().val());
                priceMax = getFloat($('#fltPriceMax').first().val());
                setTimeout(function() {
                    GM_setValue("jd_miaosha_price_min", priceMin);
                    GM_setValue("jd_miaosha_price_max", priceMax);
                }, 0);
            });
        } else priceUI = setInterval(uiInit, 1000);
    };
    //对页面执行过滤排序操作
    var doFilter = function() {
        clearInterval(priceFilter);
        var count = 0;
        //count += filterGoods($('div.skwrap')); //正在抢购
        //count += filterGoods($('div.moregoods')); //更多好货

        count += filterGoods($('div.catinfo_seckillnow')); //当天正在秒杀的商品
        count += filterGoods($('div.catinfo_startsoon')); //明日秒杀的商品
        if (count === 0) priceFilter = setInterval(doFilter, 1000);
    };
    //对页面内的一个具体分类执行过滤排序操作
    var filterGoods = function(goodsList) {
        var goods = goodsList.find('li.seckill_mod_goods');
        if (goods.length > 0) {
            var niceGoods = [];
            goods.each(function(idx){
                //按商品名过滤
                if ('undefined' !== typeof(filterPName) && filterPName !== null) {
                    var pname = getProductName(this);
                    if (!filterPName(pname)) return;
                }
                //按价格过滤
                var price = getPrice(this);
                if (price < priceMin || priceMax < price) return;
                //命中
                niceGoods.push(this);
            });
            //排序
            if (niceGoods.length > 0) {
                niceGoods.sort(function(g1, g2){
                    return getPrice(g1) - getPrice(g2); //<-------------------------------------------------如果要降序,修改这里
                });
                //取消延迟加载
                var lazyImgs = $(niceGoods).find('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');
                });
            } else niceGoods = $('<center><h2><br/>该分类下所有商品的价格都不符合过滤条件。<br/></h2></center');
            //替换原内容
            $(goods[0].closest('ul')).empty().append(niceGoods);
        }
        return goods.length;
    };

    if (!location.href.match('miaosha.jd.com')) return;

    var priceMin = GM_getValue('jd_miaosha_price_min');
    var priceMax = GM_getValue('jd_miaosha_price_max');

    var priceUI = setInterval(uiInit, 1000);
    var priceFilter = setInterval(doFilter, 1000);
})();

function getProductName(elm) {
    return $(elm).find('.seckill_mod_goods_title').first().text().trim();
}

function getPrice(elm) {
    return getFloat($(elm).find('i.seckill_mod_goods_price_now').first().text());
}

function getFloat(str) {
    str = str.replace('¥','').replace('¥','').trim();
    if (!/^\d+\.?\d*$/.test(str)) return 0; //非浮点数字串
    return parseFloat(str);
}