扩展功能

2024/12/28 15:36

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/522088/1512255/%E6%89%A9%E5%B1%95%E5%8A%9F%E8%83%BD.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        扩展功能
// @namespace   None
// @license MIT
// @grant       GM_xmlhttpRequest
// @grant       none
// @version     1.56
// @author      -
// @description 2024/12/28 15:36
// ==/UserScript==

const chunkArray = function (array, chunkSize) {
    let result = [];
    for (let i = 0; i < array.length; i += chunkSize) {
        result.push(array.slice(i, i + chunkSize));
    }
    return result;
}
const decodeData = function (data){
    return $.map(Object.keys(data),function(val){return `${val}=${data[val]? data[val]:''}`}).join("&");
}

var single = $(`
<div class="in-screen-single">
  <div class="title">扩展内容:</div>
  <div class="item">
    <a href="javascript:" class="showOnlyWarehouse">仅显示仓库清单中</a>
  </div>
</div>
`);
single.find(".item > a").on("click",function(){
    if($(this).hasClass("in-active")){
        $(this).removeClass("in-active");
    }else{
        $(this).addClass("in-active");
    }
});



single.find("a.showOnlyWarehouse").on("click",async function(){
    $("#loading").show();
    if(!$(this).hasClass("in-active")){
        $("tr.content").show();
        $(".showOnlyWarehousefingerPrint").remove();
        $("#loading").hide();
        return
    }
    var url = "https://www.dianxiaomi.com/warehouseProduct/pageList.htm";
    var skus_box = [];
    switch($("#platformNam").val()){
        case "smt":
            skus_box = $("tr.content tr[data-productid]").map(function(){
                return $(this).find("td:eq(0) span.limingcentUrlpic:eq(1)")
            });
            break;
        case "shopify":
            skus_box = $("tr.content table.in-table-in .limingcentUrlpic")
            break;
        case "aliChoice":
            skus_box = $("tr.content td.skuTdInfo span.limingcentUrlpic")
            break;
        default:
            return;
    }
    var skus = skus_box.map(function (){return $.trim($(this).text())}).toArray();
    const taskList = $.map(chunkArray(skus,200), function(searchSkus){
        return new Promise((resolve)=>
            GM_xmlhttpRequest({
                url:url,
                method:"POST",
                data:decodeData({
                    zoneType: 0,
                    pageNo: 1,
                    pageSize: 300,
                    searchType: 1,
                    searchValue: searchSkus.join(),
                    productSearchType: 1,
                    warehouseId: '6162134',
                    orderBy: 1,
                    orderByVal: 1
                }),
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                onload:function(res){
                    resolve($(res.responseText))
                }
            })
        );
    });
    var warehouseMap = {};
    var book = await Promise.all(taskList);
    for(var page of book){
        page.closest("#goodsContent").find("tr.content").map(function (){
            var sku = $.trim($(this).find(".productSku").text());
            warehouseMap[sku] = +$.trim($(this).find(`tr[data-id] td:eq(4)`).text());
        });
    }
    window.warehouseMap = warehouseMap;
    $("tr.content").hide();
    for(var sku_box of skus_box){
        var tr_box = sku_box.closest("tr.content");
        if (Object.keys(warehouseMap).includes(sku_box.text())){
            var sku = $.trim(sku_box.text())
            if (warehouseMap[sku] >= 4) {
                var tag = $(`<button class="button btn-blue mLeft5 showOnlyWarehousefingerPrint" style="height: 21px;line-height: 0.75">充裕(${warehouseMap[sku]})</button>`);
            }else if (warehouseMap[sku] >= 2){
                var tag = $(`<button class="button btn-orange mLeft5 showOnlyWarehousefingerPrint" style="height: 21px;line-height: 0.75">紧张(${warehouseMap[sku]})</button>`);
            }else{
                var tag = $(`<button class="button btn-delete mLeft5 showOnlyWarehousefingerPrint" style="height: 21px;line-height: 0.75">危险(${warehouseMap[sku]})</button>`);
            }
            sku_box.after(tag);
            if(tr_box.is(":hidden")){
                tr_box.show();
            }
        }
    }
    $("#loading").hide();

});