// ==UserScript== // @name 京东自营过滤 // @version 0.1.0.1 // @icon https://www.jd.com/favicon.ico // @description 在京东商品列表和搜索结果页面增加【自营】【非自营】过滤选项 // @author You! // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // @include *://list.jd.com/list.html?* // @include *://search.jd.com/search?* // @run-at document-end // @namespace https://greasyfork.org/zh-CN/scripts/33729-京东自营过滤 // @downloadURL none // ==/UserScript== (function() { 'use strict'; //扫描页面的间隔频率时间 var timerFreq = 500; //var goodsJdKey = 'jd_' + location.pathname.split(/[\/\.]/)[1] + '_' + location.search.split(/[\?&]/)[1].replace(/[=,]/g, '_') + '_goodsJd'; //var goods3rdKey = 'jd_' + location.pathname.split(/[\/\.]/)[1] + '_' + location.search.split(/[\?&]/)[1].replace(/[=,]/g, '_') + '_goods3rd'; //上面两行替换下面两行可以对每个分类列表和搜索的关键词独立保存过滤器设置 var goodsJdKey = 'jd_goodsJd'; var goods3rdKey = 'jd_goods3rd'; uiInit(); loadAllGoods(); //向页面添加自营过滤选项 function uiInit() { var uiPos = $('.f-feature ul'); if (uiPos.length > 0) { var goodsJdChecked = GM_getValue(goodsJdKey) === undefined ? 'class="selected"' : ''; var goods3rdChecked = GM_getValue(goods3rdKey) === undefined ? 'class="selected"' : ''; uiPos.first().prepend($( '
  • 自营
  • '+ '
  • 非自营
  • ' )); $('#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() { if (parseInt($('#J_resCount').text().replace('+','')) > 30) { if (unsafeWindow.SEARCH !== undefined && unsafeWindow.SEARCH.scroll !== undefined) { unsafeWindow.SEARCH.scroll(); waitAllGoods(); } else setTimeout(loadAllGoods, timerFreq); } else forceLoadLazyImgs(); } //等待所有商品加载完成 function waitAllGoods() { if ($('li.gl-item').length == 30) setTimeout(waitAllGoods, timerFreq); else forceLoadLazyImgs(); } //取消图片延迟加载 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'); }); //加载过滤器设置 loadSettings(); } //加载过滤器设置 function loadSettings() { if (GM_getValue(goodsJdKey) !== undefined) toggleGoodsJd(false); if (GM_getValue(goods3rdKey) !== undefined) toggleGoods3rd(false); } })();