京东自营过滤

在京东商品列表和搜索结果页面增加【自营】【非自营】过滤选项。京东的商品分类列表页和搜索结果列表页过滤器中的【京东配送】和【自营】是不一样的,【京东配送】会包含部分第三方商家的商品,只是由京东承担物流运输而已,这些第三方商品很多都没有“上午下单下午就到”的快捷,所以有必要专门针对【自营】和【非自营】过滤商品列表!但是需要注意的是,有些【自营】商品也是由【厂商配送】的,这种商品可以通过同时选中【自营】和【京东配送】过滤掉。

Verze ze dne 14. 10. 2017. Zobrazit nejnovější verzi.

// ==UserScript==
// @name         京东自营过滤
// @version      0.2.0.8
// @icon         https://www.jd.com/favicon.ico
// @description  在京东商品列表和搜索结果页面增加【自营】【非自营】过滤选项。京东的商品分类列表页和搜索结果列表页过滤器中的【京东配送】和【自营】是不一样的,【京东配送】会包含部分第三方商家的商品,只是由京东承担物流运输而已,这些第三方商品很多都没有“上午下单下午就到”的快捷,所以有必要专门针对【自营】和【非自营】过滤商品列表!但是需要注意的是,有些【自营】商品也是由【厂商配送】的,这种商品可以通过同时选中【自营】和【京东配送】过滤掉。
// @author       You!
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_addStyle
// @include      *://list.jd.com/list.html?*
// @include      *://search.jd.com/search?*
// @include      *://search.jd.com/Search?*
// @run-at       document-end
// @namespace    https://greasyfork.org/zh-CN/scripts/33729-京东自营过滤
// ==/UserScript==

(function() {
    'use strict';

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

    //var goodsJdKey = 'jd_' + location.pathname.split(/[\/\.]/)[1].toLowerCase() + '_' + location.search.split(/[\?&]/)[1].replace(/[=,]/g, '_').toLowerCase() + '_goodsJd';
    //var goods3rdKey = 'jd_' + location.pathname.split(/[\/\.]/)[1].toLowerCase() + '_' + location.search.split(/[\?&]/)[1].replace(/[=,]/g, '_').toLowerCase() + '_goods3rd';
    //上面两行替换下面两行可以对每个商品分类列表和搜索关键词独立保存过滤器设置
    var goodsJdKey = 'jd_goodsJd';
    var goods3rdKey = 'jd_goods3rd';

    var currentPathname = location.pathname.toLowerCase();
    runMain();

    //页面入口点
    function runMain() {
        switch(currentPathname) {
            case '/search':
                if ($('span.ns-icon').length == 1) return; //搜索没有结果
        }

        uiInit();
        loadAllGoods();
    }

    //向页面添加自营过滤选项
    function uiInit() {
        var uiPos = $('.f-feature ul');
        if (uiPos.length > 0) {
            switch(currentPathname) {
                case '/search':
                    uiPos.find('li a[onclick]').each(function() {
                        var tagA = $(this);
                        var oldOnClick = this.onclick;
                        tagA.click(function() {
                            oldOnClick();
                            var waitAjaxLoadGoodsList = function() {
                                if (unsafeWindow.SEARCH.loading) setTimeout(waitAjaxLoadGoodsList, timerFreq);
                                else runMain();
                            };
                            setTimeout(waitAjaxLoadGoodsList, timerFreq);
                        });
                        tagA.removeAttr('onclick');
                    });

                    var hookSearchSort = function() {
                        var sortA = $('#J_filter div.f-sort a[onclick]');
                        if (unsafeWindow.SEARCH !== undefined && unsafeWindow.SEARCH.sort_html !== undefined) {
                            var oldSortHtml = unsafeWindow.SEARCH.sort_html;
                            unsafeWindow.SEARCH.sort_html = function(C) {
                                oldSortHtml(C);
                                $('#J_filter div.f-sort a[onclick]').each(function() {
                                    var tagA = $(this);
                                    var oldOnClick = this.onclick;
                                    tagA.click(function() {
                                        oldOnClick();
                                        var waitAjaxLoadGoodsList = function() {
                                            if (unsafeWindow.SEARCH.loading) setTimeout(waitAjaxLoadGoodsList, timerFreq);
                                            else loadAllGoods();
                                        };
                                        setTimeout(waitAjaxLoadGoodsList, timerFreq);
                                    });
                                    tagA.removeAttr('onclick');
                                });
                            };
                        }
                        else setTimeout(hookSearchSort, timerFreq);
                    };
                    hookSearchSort();
                    break;
            }

            var goodsJdChecked = GM_getValue(goodsJdKey) === undefined ? 'class="selected"' : '';
            var goods3rdChecked = GM_getValue(goods3rdKey) === undefined ? 'class="selected"' : '';
            GM_addStyle('.goodsCount{font-size:10px;color:#fff;background-color:#e23a3a;border-radius:3px;padding:0px 3px;margin:0 5px 0 2px}');
            uiPos.first().prepend($(
                '<li><a '+goodsJdChecked+' href="javascript:;" id="goodsJd"><i/>自营<span class="goodsCount" id="goodsJdCount">0</span></a></li>'+
                '<li><a '+goods3rdChecked+' href="javascript:;" id="goods3rd"><i/>非自营<span class="goodsCount" id="goods3rdCount">0</span></a></li>'
            ));

            $('#goodsJd').first().click(function() {
                setTimeout(function() {
                    var checked = $('#goodsJd').toggleClass('selected').attr('class').length > 0;
                    toggleGoodsJd(checked);
                    //保存设置
                    if (!checked) GM_setValue(goodsJdKey, checked); else GM_deleteValue(goodsJdKey);
                }, 0);
            });
            $('#goods3rd').first().click(function() {
                setTimeout(function() {
                    var checked = $('#goods3rd').toggleClass('selected').attr('class').length > 0;
                    toggleGoods3rd(checked);
                    //保存设置
                    if (!checked) GM_setValue(goods3rdKey, checked); else GM_deleteValue(goods3rdKey);
                }, 0);
            });
        } else setTimeout(uiInit, timerFreq);
    }

    //切换过滤自营/非自营商品
    function toggleGoodsJd(checked) {
        $('li.gl-item:has(i[data-tips="京东自营,品质保障"])').css('display', checked ? '' : 'none');
    }
    function toggleGoods3rd(checked) {
        $('li.gl-item:not(:has(i[data-tips="京东自营,品质保障"]))').css('display', checked ? '' : 'none');
    }

    //加载所有商品
    function loadAllGoods() {
        switch(currentPathname) {
            case '/list.html':
                processGoodsList();
                break;

            case '/search':
                if (unsafeWindow.SEARCH !== undefined && unsafeWindow.SEARCH.scroll !== undefined) {
                    unsafeWindow.SEARCH.scroll();
                    setTimeout(waitAllGoods, timerFreq);
                }
                else setTimeout(loadAllGoods, timerFreq);
                break;
        }
    }
    //等待所有商品加载完成
    function waitAllGoods() {
        if (unsafeWindow.SEARCH.loading) setTimeout(waitAllGoods, timerFreq);
        else processGoodsList();
    }
    //收尾处理
    function processGoodsList() {
        //计算商品数量
        setCount();
        //加载过滤器设置
        loadSettings();
        //取消图片延迟加载
        forceLoadLazyImgs();
    }
    //计算商品数量
    function setCount() {
        $('#goodsJdCount').text($('li.gl-item:has(i[data-tips="京东自营,品质保障"])').length);
        $('#goods3rdCount').text($('li.gl-item:not(:has(i[data-tips="京东自营,品质保障"]))').length);
    }
    //加载过滤器设置
    function loadSettings() {
        if (GM_getValue(goodsJdKey) !== undefined) toggleGoodsJd(false);
        if (GM_getValue(goods3rdKey) !== undefined) toggleGoods3rd(false);
    }
    //取消图片延迟加载
    function forceLoadLazyImgs() {
        var lazyImgs = $('ul.gl-warp 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');
        });
    }
})();