// ==UserScript== // @name 淘宝天猫商品选项计算单价 // @namespace leizingyiu.net // @version 2022.09.27 // @description 点击sku后自动计算单价,并填充到sku中 // @icon https://img.alicdn.com/favicon.ico // @author Leizingyiu // @include *://item.taobao.com/* // @include *://detail.tmall.com/* // @run-at document-idle // @grant none // @license GNU AGPLv3 // @downloadURL none // ==/UserScript== /* setting */ priceSelector = ['.tm-promo-cur .tm-price ', '#J_PromoPriceNum ', '#J_PromoPrice .tm-price','#J_StrPrice > em.tb-rmb-num'].join(','); skuSelector = '.tb-prop dd ul li a'; var words = "包 条 个 袋 杯 枚 颗 罐 公斤 斤 两 盒 桶" .split(/\s{1,}/) .filter((a) => Boolean(a)), wordsReg = new RegExp( "\\d[\\d\\.]*s*(" + (words.map((word) => `(${word})`).join("|") + ")\\s*(\\*\\d{1,})*"), 'g' ), textReplaceReg = /(\([^\)]*\))|(\[[^\]]*\])|(「[^」]*」)|(([^)]*))/g, priceReg = /\d[\d.]*\s*(?=元)/, gramReg = /\d[\d.]*\s*(([千克]{1,})|(((kg)|(KG)|(Kg)|(g)|(G)){1,}))\s*(\*\d{1,})*/, volReg = /\d[\d.]*\s*(([毫升]{1,})|((L)|(ml)|(ML){1,}))\s*(\*\d{1,})*/; loadingWaitTime = 1000; activateGapTime = 500; /* style */ let style = document.createElement('style'); style.innerText = ` .tb-prop li a:after , #detail .tb-key .tb-prop li:after { content: attr(price); display:content; white-space: break-spaces; line-height: 1.5em; word-break: break-word; } .tb-prop li a , #detail .tb-key .tb-prop li {white-space:break-all;} `; document.body.appendChild(style); /* main function */ function fn() { let that = this; setTimeout( function () { let price = document.querySelector(priceSelector).innerText; let unitprice = unitPrice(that.innerText, price); that.setAttribute('price', '¥' + price + ' | ' + unitprice); // that.removeEventListener('click', fn); }, loadingWaitTime ); } /* addEventListener */ [...document.querySelectorAll(skuSelector)].map(li => { console.log(li); li.addEventListener('click', fn); }); // /* activate */ // [...document.querySelectorAll(skuSelector)].map((li, idx) => { // setTimeout( // () => { // li.click() // }, // idx * loadingWaitTime + activateGapTime // ); // }); function unitPrice(text, price) { text = text.replace(textReplaceReg, "") || '', price = price || 0; if (text == '') { return false; } var gram = text.match(gramReg), vol = text.match(volReg); var otherUnit = text.match(wordsReg); var unit = "", num = 0, priceText = "", priceKg, priceL, priceU; if (price == null || (gram == null && vol == null && otherUnit == null)) { priceText = "--"; } else { price = Number(price); if (gram != null) { gram = Number( eval(gram[0].replace(/[克gG]/g, "").replace(/[kK千]/, "*1000")) ); priceKg = (price / gram) * 1000; priceText += priceKg.toFixed(2) + "/kg"; } if (vol != null) { vol = Number( eval(vol[0].replace(/[升lL]/g, "").replace(/[毫mM]/, "/1000")) ); priceL = price / vol; priceText = (gram != null ? " | " : "") + priceL.toFixed(2) + "/L"; } console.log(price, text, gram, vol, priceText, otherUnit, wordsReg); if (otherUnit != null) { otherUnit.map(un => { num = Number(un.match(/\d*/)); unit = un.replace(/\d*/, ""); priceU = price / num; priceText += (priceText == ''?'':" | " )+ priceU.toFixed(2) + "/" + unit; if (unit == "斤") { priceKg = (priceU * 2).toFixed(2) priceText += " | " + priceKg + "/kg"; } if (unit == "两") { priceKg = (priceU * 20).toFixed(2); priceText += " | " + priceKg + "/kg"; } }); } if (priceText == "") { priceText += "___"; } } return priceText; }