shopline店铺结算页首屏数据获取

shopline店铺结算页首屏数据获取对比v1

目前為 2025-06-19 提交的版本,檢視 最新版本

// ==UserScript==
// @name         shopline店铺结算页首屏数据获取
// @namespace    http://tampermonkey.net/
// @version      1.0.2
// @description  shopline店铺结算页首屏数据获取对比v1
// @author       skyhuang
// @match        https://*.myshopline.com/*/checkouts/*
// @match        https://*.myshoplinestg.com/*/checkouts/*
// @license      AGPL License
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    
    function toString(sv, tv) {
    if (sv === undefined || sv === null) {
        sv = "null"
    }
    if (tv === undefined || tv === null) {
        tv = "null"
    }
    if (sv instanceof Object) {
        sv = JSON.stringify(sv)
    }
    if (tv instanceof Object) {
        tv = JSON.stringify(tv)
    }
    return `${sv} ----> ${tv}`
}

function compareList(sourceList, targetList) {
    let resultList = []
    for (let i = 0; i < sourceList.length; i++) {
        let sourceIdxValue = sourceList[i]
        let targetIdxValue = i >=targetList.length ? undefined : targetList[i]
        if ((sourceIdxValue === undefined || sourceIdxValue === null) && (targetIdxValue === undefined || targetIdxValue === null)) {
            // 应该不会有这个场景
            continue
        }
        if ((sourceIdxValue === undefined || sourceIdxValue === null) || (targetIdxValue === undefined || targetIdxValue === null)) {
            resultList[i] = toString(sourceIdxValue, targetIdxValue)
            continue
        }
        if (sourceIdxValue instanceof Object && targetIdxValue instanceof Object) {
            resultList[i] = compareObject(sourceIdxValue, targetIdxValue)
            continue
        }
        if (sourceIdxValue === targetIdxValue) {
            resultList[i] = null
            continue
        }
        resultList[i] = toString(sourceIdxValue, targetIdxValue)
    }
    let flag = false;
    for (let v of resultList) {
        if (v !== undefined && v !== null) {
            if (typeof v === 'object' && !Array.isArray(v) && Object.keys(v).length === 0) {
                // 判断是否是空对象{}
            } else {
                flag = true
                break
            }
        }
    }
    return flag ? resultList : []
}

function compareObject(sourceObj, targetObj) {
    let resultObj = {}
    Object.keys(sourceObj).forEach(key => {
        let sourceObjValue = sourceObj[key]
        let targetObjValue = targetObj[key]
        if ((sourceObjValue === undefined || sourceObjValue === null) && (targetObjValue === undefined || targetObjValue === null)) {
            return
        }
        if ((sourceObjValue === undefined || sourceObjValue === null) || (targetObjValue === undefined || targetObjValue === null)) {
            resultObj[key] = toString(sourceObjValue, targetObjValue)
            return
        }
        if (sourceObjValue instanceof Array && targetObjValue instanceof Array) {
            let subListResult = compareList(sourceObjValue, targetObjValue)
            if (subListResult.length > 0) {
                resultObj[key] = subListResult
            }
            return
        }
        if (sourceObjValue instanceof Object && targetObjValue instanceof Object) {
            let subObjResult = compareObject(sourceObjValue, targetObjValue)
            if (Object.keys(subObjResult).length !== 0) {
                resultObj[key] = subObjResult
            }
            return
        }
        // 额外判断下string能不能转object、array
        if (typeof sourceObjValue === "string" && typeof targetObjValue === "string") {
            if (sourceObjValue.startsWith("{") && targetObjValue.startsWith("{")) {
                try {
                    let sourceString2Object = JSON.parse(sourceObjValue)
                    let targetString2Object = JSON.parse(targetObjValue)
                    let subObjResult = compareObject(sourceString2Object, targetString2Object)
                    if (Object.keys(subObjResult).length !== 0) {
                        resultObj[key] = subObjResult
                    }
                    return
                } catch (err) {
                    // 转对象失败继续往下执行
                    console.log(err)
                }
            }

            if (sourceObjValue.startsWith("[{") && targetObjValue.startsWith("[{")) {
                try {
                    let sourceString2List = JSON.parse(sourceObjValue)
                    let targetString2List = JSON.parse(targetObjValue)
                    let subListResult = compareList(sourceString2List, targetString2List)
                    if (subListResult.length > 0) {
                        resultObj[key] = subListResult
                    }
                    return
                } catch (err) {
                    console.log(err)
                }
            }
        }
        if (sourceObjValue === targetObjValue) {
            return
        }
        resultObj[key] = toString(sourceObjValue, targetObjValue)
    })
    return resultObj
}
    // 方法1:直接访问全局变量
    function capturePreloadState() {
        // 确保变量存在
        if (typeof window.__PRELOAD_STATE__ !== 'undefined') {
            const data = window.__PRELOAD_STATE__;
            const tradeCheckout = data['tradeCheckout'];
            const checkout = data['checkout'];
            console.log("捕获的结算页首屏checkout数据:", tradeCheckout);
            //理论上chenckout里面的数据和tradeCheckout数据是一样的,所以这里还是自动对比一下
            const result = compareList(tradeCheckout,checkout);
            console.log("对比结果1: (tips:数组对象打印出来的null或{}是表示对比通过!!!", result);
            
        } else {
            console.warn("__PRELOAD_STATE__ 命令无法执行!");
        }
    }

    // 启动捕获逻辑
    if (document.readyState === 'complete') {
        capturePreloadState(); // 页面已加载完成
    } else {
        window.addEventListener('load', () => {
            capturePreloadState(); // 等待页面完全加载
        });
    }
})();