Greasy Fork

Greasy Fork is available in English.

京东秒杀价格过滤

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

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         京东秒杀价格过滤
// @version      0.2.3.6
// @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==

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

(function() {
    //扫描页面的间隔频率时间
    var timerFreq = 1000;
    //向页面右下角的浮动工具栏尾部追加价格过滤部件
    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, timerFreq);
    };
    //对页面执行过滤排序操作
    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, timerFreq);
    };
    //对页面内的一个具体分类执行过滤排序操作
    var filterGoods = function(goodsList) {
        var goods = goodsList.find('li.seckill_mod_goods');
        if (goods.length > 0) {
            var niceGoods = [];
            goods.each(function(idx){
                //按商品名过滤
                if (typeof(filterPName) === 'function') {
                    var pname = getProductName(this);
                    if (!filterPName(pname, location.search.split(/[?&=]/)[2])) return;
                }
                //按价格过滤
                var price = getPrice(this);
                if (price.now < priceMin || (0 < priceMax && priceMin <= priceMax && priceMax < price.now)) return;
                //标注优惠力度
                var priceOff = price.pre - price.now;
                var discount = price.now / price.pre;
                if (discount <= 0.1) {
                    $(this).find('div.seckill_mod_goods_info').attr('title', '1折以下(优惠'+priceOff+')').css('background-color', 'gold'); //1折以下
                } else if (discount <= 0.3) {
                    $(this).find('div.seckill_mod_goods_info').attr('title', '3折以下(优惠'+priceOff+')').css('background-color', 'springgreen'); //3折以下
                } else if (discount <= 0.5) {
                    $(this).find('div.seckill_mod_goods_info').attr('title', '5折以下(优惠'+priceOff+')').css('background-color', '#BBFFEE'); //5折以下
                } else if (discount <= 0.75) {
                    $(this).find('div.seckill_mod_goods_info').attr('title', '75折以下(优惠'+priceOff+')').css('background-color', '#CCEEFF'); //75折以下
                } else if ( priceOff >= 90) {
                    $(this).find('div.seckill_mod_goods_info').attr('title', '优惠超过90('+priceOff+')').css('background-color', '#FFEEEE'); //优惠超过90
                }
                //命中
                niceGoods.push(this);
            });
            //排序
            if (niceGoods.length > 0) {
                niceGoods.sort(function(g1, g2){
                    return getPrice(g1).now - getPrice(g2).now; //<-------------------------------------------------如果要降序,修改这里
                });
                //取消延迟加载
                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, timerFreq);
    var priceFilter = setInterval(doFilter, timerFreq);
})();

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*$/.test(str)) return 0; //非浮点数字串
    return parseFloat(str);
}