Greasy Fork

店小秘统计当前订单件数

自己用着方便,看谁也有这需求就凑合用吧!

目前为 2023-03-28 提交的版本。查看 最新版本

// ==UserScript==
// @name         店小秘统计当前订单件数
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  自己用着方便,看谁也有这需求就凑合用吧!
// @author       Huang
// @match        https://www.dianxiaomi.com/order/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=dianxiaomi.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    function addStyle(){
        const style = document.createElement('style');
        style.textContent = `
          h3.total-num {
            color: red;
            position: absolute;
            left: 200px;
            top: 8px;
            font-size:14px;
          }
        `;
        document.head.appendChild(style);
    }

    function main(){
        var tbody = document.querySelector(".xianshishujudate");
        if(!tbody) return;

        var trs = tbody.children;
        var itemList=[];

        for (var i = 0; i < trs.length; i++) {
            if (trs[i].tagName === "TR") {
                itemList.push(trs[i])
            }
        }

        if(itemList.length == 0)return;
        const len = itemList.length;


        for (let i = 0; i < itemList.length; i++) {
            if (i%2 === 0) continue;
            let v = itemList[i];
            if(v.hasAttribute('cur-total'))return;

            let curNum = 0;
            var numberBox = v.querySelectorAll(".p0-imp .pairProInfo");
            numberBox.forEach(item=>{

                const secondTd = item.querySelector("td:nth-of-type(2)");
                const span = secondTd.querySelector("p:first-of-type span");
                curNum+=Number(span.innerText)
                v.setAttribute('cur-total',curNum);
            })
        }

        for (let i = 0; i < itemList.length; i++) {
            if (i%2 != 0) continue;
            let v = itemList[i];

            v.style.position="relative";
            var newElement = document.createElement("h3");
            var num = itemList[i+1].getAttribute('cur-total');

            newElement.textContent = `${num}件`
            newElement.classList.add('total-num');
            v.appendChild(newElement);
        }
    }

    function waitForElement() {
        var target = document.querySelector("#orderListTable");
        if (!target) return;

        observer.disconnect();
        main();
    }


    var observer = new MutationObserver(waitForElement);
    observer.observe(document.body, { childList: true, subtree: true });

    window.addEventListener("load",()=>{
        waitForElement();
        addStyle()
    } );



    // 节流函数
    function throttle(fn, delay) {
        let timer = null;
        return function() {
            if (!timer) {
                timer = setTimeout(() => {
                    fn.apply(this, arguments);
                    timer = null;
                }, delay);
            }
        }
    }

    // 不知道怎么在切tab时也监听变化绑定事件,暂且绑到scroll吧
    window.addEventListener('scroll', throttle(() => {
        main();
    }, 500));
})();