什么值得买 自动计算单价

在什么值得买搜索页面,自动计算单价

目前為 2022-04-13 提交的版本,檢視 最新版本

// ==UserScript==
// @name        什么值得买 自动计算单价
// @namespace   leizingyiu.net
// @version     2022-04-13
// @description 在什么值得买搜索页面,自动计算单价
// @icon        https://www.smzdm.com/favicon.ico
// @author      Leizingyiu
// @match       http*://search.smzdm.com/
// @grant       none
// @license     GNU AGPLv3 
// 
// ==/UserScript==

let words='包 条 个 袋 杯 枚 颗 罐 斤 两 盒 桶 只'.split(/\s{1,}/).filter(a=>Boolean(a));
let wordsReg=new RegExp('\\d[\\d\\.]*\s*('+(words.map(word=>`(${word})`).join('|')+')\\s*(\\*\\d{1,})*'));
//console.log(wordsReg);

[...document.querySelectorAll('h5.feed-block-title')].map(h5 => {
  
    let text = h5.innerText.replace(/(\([^\)]*\))|(\[[^\]]*\])|(「[^」]*」)|(([^)]*))/g, '');
  
    let price = text.match(/\d[\d.]*\s*(?=元)/);
    let gram = text.match(/\d[\d.]*\s*(([千克]{1,})|([kgKG]{1,}))\s*(\*\d{1,})*/);
    let vol = text.match(/\d[\d.]*\s*(([毫升]{1,})|([mlML]{1,}))\s*(\*\d{1,})*/);
    let otherUnit=text.match(wordsReg);
  
    //console.log(price,gram,vol,otherUnit);
  
    let unit='',num=0, priceText = '', priceKg, priceL,priceU;
  
    if (price == null || (gram == null && vol == null && otherUnit ==null)) {
        priceText = '--';
    } else {
        price = Number(price[0]);

        if (gram != null) {
            gram = Number(eval(gram[0].replace(/[克gG]/g, '').replace(/[k千]/, '*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';
          
        }
      if(otherUnit!=null){
            num=Number(otherUnit[0].match(/\d*/));
            unit=otherUnit[0].replace(/\d*/,'');
            priceU=price/num;
            priceText+=(gram!=null ||vol != null?' | ':'')+priceU.toFixed(2)+'/'+unit;
        
            if(unit=='斤'){
              priceText+=' | '+(priceU*2).toFixed(2) + '/kg';
            }
        
        if(unit=='两'){
              priceText+=' | '+(priceU*20).toFixed(2) + '/kg';
            }
        } 
      
      if (priceText==''){ 
            priceText += '___'; 
        }

    }
  
    let redBox = h5.querySelectorAll('a')[1];
    redBox.style.display = 'block';
    redBox.children[0].style.float = 'left';
  
    let span = document.createElement('span');
    span.innerText = priceText;
    span.style.float = 'right';
  
    redBox.appendChild(span);
});

QingJ © 2025

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