// ==UserScript==
// @name 一号店单价助手
// @namespace http://greasyfork.org/
// @version 1.01
// @description 一号店(yhd.com)单价助手,补全了商品列表中有些商品没标单价的信息,帮助你在1号店(yihaodian)找到最划算的商品
// @author Yarmu
// @match *://list.yhd.com/*
// @match *://search.yhd.com/*
// @grant GM_setValue
// @grant GM_getValue
// @supportURL http://greasyfork.org/zh-CN/users/41708-yarmu
// @downloadURL https://update.greasyfork.icu/scripts/30690/%E4%B8%80%E5%8F%B7%E5%BA%97%E5%8D%95%E4%BB%B7%E5%8A%A9%E6%89%8B.user.js
// @updateURL https://update.greasyfork.icu/scripts/30690/%E4%B8%80%E5%8F%B7%E5%BA%97%E5%8D%95%E4%BB%B7%E5%8A%A9%E6%89%8B.meta.js
// ==/UserScript==
(function() {
var enabled = GM_getValue('yhd_price_tip_enabled', true);
addListPriceButton(enabled);
if (enabled) {
addPriceTipListener('.proPrice', addListPriceTip, 1000);
}
})();
function addPriceTipListener(tag, func, time) {
var eachCallFunc = function() {
$(tag).each(function() {
if (!$(this).attr('priceTip')) {
$(this).attr('priceTip', '1');
func.call(this);
}
});
};
eachCallFunc();
if (time) {
setInterval(eachCallFunc, time);
}
}
function addListPriceTip() {
var infoItem = $(this).find('.num');
if (infoItem.length === 0) infoItem = $(this).find('.price');
var price = infoItem.attr('yhdprice');
console.log(price);
if (!price) return;
var capacity = infoItem.attr('diapernum');
var unitType = infoItem.attr('productunit');
var unitItem = $(this).find('.unit_price');
console.log([price, capacity, unitType, unitItem.length]);
if (capacity && unitType && unitItem.length) return;
var titleItem = $(this).parent().find('.proName a');
if(titleItem.length === 0) titleItem = $(this).parent().find('.title a');
var title = titleItem.text().trim();
price = parseFloat(price);
capacity = parseFloat(capacity);
var unit;
if (!isNaN(capacity) && capacity > 0 && unitType && unitItem.length === 0) {
if (unitType == '4') {
unitType = 'L';
capacity /= 1000;
price /= capacity;
} else {
unitType = '500g';
capacity /= 500;
price /= capacity;
}
unit = {
price: Math.round(price * 100) / 100,
capacity: Math.round(capacity * 10000) / 10000,
unit: unitType
};
} else {
unit = getUnit(title, price);
if (unit === null) return;
}
var htm = '(¥' + unit.price + '/' + unit.unit + ')';
if (unit.tip) {
title = '( 助手估重: ' + unit.capacity + unit.unit + ' = ' + unit.tip + ' )\n' + title;
titleItem.attr('title', title);
}
if (!unitItem.length) {
unitItem = $('');
$(this).append(unitItem);
}
unitItem.html(htm);
}
function addListPriceButton(isEnabled) {
var button = $('#priceTipButton');
if (button.length > 0) return;
button = $('单价助手');
$('.sort_b').append(button);
button.click(function() {
GM_setValue('yhd_price_tip_enabled', !isEnabled);
$(this).attr('class', (!isEnabled? 'cur': ''));
location.reload();
});
}
function getUnit(title, price) {
if (!title) return null;
if (price <= 0) return null;
//处理包含:和送的情况
if (title.match(/:|[))】]送/)){
var titles = title.split(/:|[))】]送/);
for (var t=0; t 3 && match[pos.pCount]) {
var multiple = match[pos.pCount].match(/\d+/g);
if (multiple) for (var i=0; i cap) {
tip = ''; cap = 0; count = 0;
} else {
continue;
}
}
un = unit;
tip += match[0] + ' ';
cap += capacity;
++count;
}
}
//处理数量在其他位置的情况
if (count == 1 && pos.i !== 0) {
var regZhn = '两一二三四五六七八九十';
var regMul = '(?:[^*x×满\\d])\\s*(\\d+|['+regZhn+'])['+regQuant+'](?!\\d+折|松)';
reg = new RegExp(regMul, 'i');
match = reg.exec(title);
if (match) {
var mul = regZhn.indexOf(match[1]);
if (mul == -1) mul = parseInt(match[1]);
else if (mul === 0) mul = 2;
if (lastMul != mul) {
cap *= mul;
tip += match[0].substr(1, match[0].length-1).trim();
}
}
}
if (cap > 0) {
var unitPrice = parseFloat(price) / cap;
//如果单价>2000元或<0.5元,太大或太小不显示
if (unitPrice > 2000 || unitPrice < 0.5) return null;
else return {
capacity: Math.round(cap * 10000) / 10000,
unit: un,
price: Math.round(unitPrice * 100) / 100,
tip: tip.trim()
};
} else return null;
}