淘宝天猫商品选项计算单价

点击sku后自动计算单价,并填充到sku中

  1. // ==UserScript==
  2. // @name 淘宝天猫商品选项计算单价
  3. // @namespace leizingyiu.net
  4. // @version 2022.09.27.1
  5. // @description 点击sku后自动计算单价,并填充到sku中
  6. // @icon https://img.alicdn.com/favicon.ico
  7. // @author Leizingyiu
  8. // @include *://item.taobao.com/*
  9. // @include *://detail.tmall.com/*
  10. // @run-at document-idle
  11. // @grant none
  12. // @license GNU AGPLv3
  13. // ==/UserScript==
  14.  
  15.  
  16.  
  17. /* setting */
  18. var specialPriceSelector = ['#J_PromoPriceNum.tb-rmb-num'
  19. , '#J_PromoPrice.tm-promo-cur > dd > div.tm-promo-price > span.tm-price'].join(','),
  20. // 淘宝优惠价: #J_PromoPriceNum.tb-rmb-num
  21. // tm 优惠价: #J_PromoPrice.tm-promo-cur > dd > div.tm-promo-price > span.tm-price
  22.  
  23. originalPriceSelector = [' #J_StrPrice > em.tb-rmb-num ',
  24. ' #J_StrPriceModBox > dd > span.tm-price'].join(' , '),
  25.  
  26. // 淘宝原价: #J_StrPrice > em.tb-rmb-num
  27. // tm 原价: #J_StrPriceModBox > dd > span.tm-price
  28.  
  29. skuSelector = '.tb-prop dd ul li a',
  30.  
  31. words = "包 条 个 袋 杯 枚 颗 罐 公斤 斤 两 盒 桶"
  32. .split(/\s{1,}/)
  33. .filter((a) => Boolean(a)),
  34. wordsReg = new RegExp(
  35. "\\d[\\d\\.]*s*(" +
  36. (words.map((word) => `(${word})`).join("|") + ")\\s*(\\*\\d{1,})*"), 'g'
  37. ),
  38. textReplaceReg = /(\([^\)]*\))|(\[[^\]]*\])|(「[^」]*」)|(([^)]*))/g,
  39. priceReg = /\d[\d.]*\s*(?=元)/,
  40. gramReg =
  41. /\d[\d.]*\s*(([千克]{1,})|(((kg)|(KG)|(Kg)|(g)|(G)){1,}))\s*(\*\d{1,})*/,
  42. volReg = /\d[\d.]*\s*(([毫升]{1,})|((L)|(ml)|(ML){1,}))\s*(\*\d{1,})*/;
  43.  
  44.  
  45.  
  46. loadingWaitTime = 1000;
  47. activateGapTime = 500;
  48.  
  49. /* style */
  50. let style = document.createElement('style');
  51. style.innerText = `
  52. .tb-prop li a:after , #detail .tb-key .tb-prop li:after {
  53. content: attr(price);
  54. display:content;
  55. white-space: break-spaces;
  56. line-height: 1.5em;
  57. word-break: break-word;
  58.  
  59.  
  60. }
  61. .tb-prop li a , #detail .tb-key .tb-prop li {white-space:break-all;}
  62. `;
  63. document.body.appendChild(style);
  64.  
  65. /* main function */
  66. function fn() {
  67. let that = this;
  68. setTimeout(
  69. function () {
  70.  
  71. let priceBox = document.querySelector(specialPriceSelector) ? document.querySelector(specialPriceSelector) : document.querySelector(originalPriceSelector);
  72. if (!priceBox) { console.error('很抱歉,找不到价格。请反馈本页面链接,谢谢。') }
  73. let price = priceBox.innerText;
  74.  
  75.  
  76. let unitprice = unitPrice(that.innerText, price);
  77. that.setAttribute('price', '¥' + price + ' | ' + unitprice);
  78. // that.removeEventListener('click', fn);
  79. }, loadingWaitTime
  80. );
  81. }
  82.  
  83. /* addEventListener */
  84. [...document.querySelectorAll(skuSelector)].map(li => {
  85. console.log(li);
  86. li.addEventListener('click', fn);
  87. });
  88.  
  89. // /* activate */
  90. // [...document.querySelectorAll(skuSelector)].map((li, idx) => {
  91. // setTimeout(
  92. // () => {
  93. // li.click()
  94. // },
  95. // idx * loadingWaitTime + activateGapTime
  96. // );
  97. // });
  98.  
  99.  
  100.  
  101.  
  102.  
  103. function unitPrice(text, price) {
  104. text = text.replace(textReplaceReg, "") || '',
  105. price = price || 0;
  106. if (text == '') { return false; }
  107.  
  108. var gram = text.match(gramReg),
  109. vol = text.match(volReg);
  110.  
  111.  
  112.  
  113. var otherUnit = text.match(wordsReg);
  114. var unit = "",
  115. num = 0,
  116. priceText = "",
  117. priceKg,
  118. priceL,
  119. priceU;
  120. if (price == null || (gram == null && vol == null && otherUnit == null)) {
  121. priceText = "--";
  122. } else {
  123. price = Number(price);
  124.  
  125.  
  126. if (gram != null) {
  127. gram = Number(
  128. eval(gram[0].replace(/[克gG]/g, "").replace(/[kK千]/, "*1000"))
  129. );
  130. priceKg = (price / gram) * 1000;
  131. priceText += priceKg.toFixed(2) + "/kg";
  132. }
  133. if (vol != null) {
  134. vol = Number(
  135. eval(vol[0].replace(/[升lL]/g, "").replace(/[毫mM]/, "/1000"))
  136. );
  137. priceL = price / vol;
  138. priceText = (gram != null ? " | " : "") + priceL.toFixed(2) + "/L";
  139. }
  140.  
  141. console.log(price, text, gram, vol, priceText, otherUnit, wordsReg);
  142.  
  143. if (otherUnit != null) {
  144.  
  145. otherUnit.map(un => {
  146. num = Number(un.match(/\d*/));
  147. unit = un.replace(/\d*/, "");
  148. priceU = price / num;
  149. priceText += (priceText == '' ? '' : " | ") + priceU.toFixed(2) + "/" + unit;
  150. if (unit == "斤") {
  151. priceKg = (priceU * 2).toFixed(2)
  152. priceText += " | " + priceKg + "/kg";
  153. }
  154. if (unit == "两") {
  155. priceKg = (priceU * 20).toFixed(2);
  156. priceText += " | " + priceKg + "/kg";
  157. }
  158. });
  159. }
  160.  
  161.  
  162. if (priceText == "") {
  163. priceText += "___";
  164. }
  165. }
  166. return priceText;
  167. }
  168.  

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址