Greasy Fork is available in English.
在京东商品列表和搜索结果页面增加【自营】【非自营】过滤选项。京东商品分类列表页和搜索结果列表页的过滤器中【京东配送】和【自营】是不一样的,【京东配送】会包含一些第三方商家的商品,只是由京东承担物流运输而已,这些第三方商品很多都没有“上午下单下午就到”的快捷,所以有必要专门针对【自营】和【非自营】过滤商品列表!
当前为
// ==UserScript==
// @name 京东自营过滤
// @version 0.2.0.2
// @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?*
// @run-at document-end
// @namespace http://greasyfork.icu/zh-CN/scripts/33729-京东自营过滤
// ==/UserScript==
(function() {
'use strict';
//扫描页面的间隔频率时间
var timerFreq = 333;
//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';
runMain();
//页面入口点
function runMain() {
uiInit();
loadAllGoods();
}
//向页面添加自营过滤选项
function uiInit() {
var uiPos = $('.f-feature ul');
if (uiPos.length > 0) {
if (location.pathname.toLowerCase() === '/search') {
uiPos.find('li a').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, 0);
});
tagA.removeAttr('onclick');
});
}
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() {
if (unsafeWindow.SEARCH !== undefined && unsafeWindow.SEARCH.scroll !== undefined) {
unsafeWindow.SEARCH.scroll();
setTimeout(waitAllGoods, timerFreq);
}
else setTimeout(loadAllGoods, timerFreq);
}
//等待所有商品加载完成
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');
});
}
})();