Greasy Fork

店小秘助手

统计当前订单件数 & 针对区域定价的报关金额进行批量填充

目前为 2023-12-05 提交的版本。查看 最新版本

// ==UserScript==
// @name         店小秘助手
// @namespace    https://greasyfork.org/zh-CN/scripts/462551
// @version      1.1
// @description  统计当前订单件数 & 针对区域定价的报关金额进行批量填充
// @author       Huang
// @match        https://www.dianxiaomi.com/order/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=dianxiaomi.com
// @grant        GM_addStyle
// @license MIT
// ==/UserScript==

(function () {
    // 模拟input
    function tEvent (b, a) {
        if (b) {
            window.newhtmlevents = window.newhtmlevents || document.createEvent("HTMLEvents");
            newhtmlevents.initEvent(a, true, true);
            return b.dispatchEvent(newhtmlevents)
        }
    }


    let finalDeclareValues
    // 批量报关时先将值取出
    $(document).off('click', `a[onclick="showBatchCustoms();"]`).on('click', function () {
        let t = []
        $(`input[name='packageId']`).each(function () {
            if ($(this).prop('checked')) {
                let eleList = $(this).closest('tr').next().children().eq(0).find('tr')
                eleList.each((i, e) => t.push($(e).find("p:contains('USD')").text().replace(/[^0-9.]/g, "")))
                finalDeclareValues = t
            }
        })
    })

    // 填充价格
    function fillValues () {
        $(`input[name="declaredValues"]`).each(function (index) {
            let ele = $(this).get(0)
            tEvent(ele, 'click')
            tEvent(ele, 'input')
            ele.value = finalDeclareValues[index]
            tEvent(ele, "keyup")
            tEvent(ele, "change")
            tEvent(ele, "blur")
        })
    }


    // 统计订单件数
    function summary () {
        let titleRows = document.querySelectorAll('.goodsId')

        const len = titleRows.length
        if (len == 0) return

        for (let titleRow of titleRows) {
            let contentRow = titleRow.nextElementSibling
            let numBoxes = contentRow.querySelectorAll('[class^="circularSpan"]')

            let sum = 0
            for (let numBox of numBoxes) {
                let num = parseInt(numBox.textContent)
                sum += num
            }

            titleRow.insertAdjacentHTML("beforeend", `<h3 class='total-num'>${sum}件</h3>`)
        }
    }

    jQuery.ajaxPrefilter(function (options, originalOptions, jqXHR) {
        const { url } = options
        const keywords = ['list.htm', 'splitList.htm', 'mergeList.htm', 'searchPackage.htm']
        for (const curURL of keywords) {
            jqXHR.done(function (data) {
                if (!data) return
                if (url.includes(curURL)) setTimeout(summary, 50)
                if (url.includes('showBatchCustoms.htm')) setTimeout(fillValues, 50)
            })
        }
    })

    let css = `
    h3.total-num {
        color: red;
        position: absolute;
        left: 250px;
        top: 7px;
        font-size:14px;
    }
    #orderListTable tr.goodsId {
        position: relative;
    }
    `
    GM_addStyle(css)

})()