yiwaima / ERP 波次自动全选

// ==UserScript==
// @name         ERP 波次自动全选
// @namespace    https://openuserjs.org/users/yiwaima
// @version      1.1
// @description  在ERP波次创建页面自动处理URL参数并选中全选复选框和仓库名称
// @author       yiwaima
// @match        https://*.erp321.com/app/wms/Wave/CreateOffPick.aspx*
// @icon         https://files.erp321.com/img/platIcon/ziShen.png
// @grant        none
// @license      MIT
// @homepageURL  https://openuserjs.org/scripts/yiwaima/ERP_波次自动全选
// @supportURL   https://github.com/yiwaima/erp-wave-auto-select-all/issues
// @updateURL    https://openuserjs.org/meta/yiwaima/ERP_波次自动全选.meta.js
// @downloadURL  https://openuserjs.org/install/yiwaima/ERP_波次自动全选.user.js
// ==/UserScript==

(function() {
    'use strict';

    // 从cookie中获取值的函数
    function getCookie(name) {
        const value = `; ${document.cookie}`;
        const parts = value.split(`; ${name}=`);
        if (parts.length === 2) return parts.pop().split(';').shift();
        return null;
    }

    // 检测URL参数并处理跳转
    function checkAndRedirect() {
        const currentUrl = new URL(window.location.href);
        const searchParams = currentUrl.searchParams;

        // 检查是否包含必需的参数
        if (!searchParams.has('owner_co_id') || !searchParams.has('authorize_co_id')) {
            // 从cookie中提取u_co_id
            const uCoId = getCookie('u_co_id');
            if (uCoId) {
                // 构建新的URL
                const newUrl = `https://ww.erp321.com/app/wms/Wave/CreateOffPick.aspx?owner_co_id=${uCoId}&authorize_co_id=${uCoId}`;
                console.log(`检测到URL参数不完整,自动跳转到:${newUrl}`);
                window.location.href = newUrl;
            } else {
                console.error('无法从cookie中获取u_co_id,无法构建完整URL');
            }
        }
    }

    // 自动选择仓库名称
    function autoSelectWarehouse() {
        // 查找仓库名称元素
        const warehouseElement = document.evaluate('//*[@id="warehouse_10_name"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

        if (warehouseElement) {
            // 点击仓库名称元素
            warehouseElement.click();
            console.log('已自动选择仓库名称');

            // 触发必要的事件
            warehouseElement.dispatchEvent(new Event('change', { bubbles: true }));
            warehouseElement.dispatchEvent(new Event('click', { bubbles: true }));
        }
    }

    // 自动选择全选复选框的函数
    function autoSelectAll() {
        // 查找目标复选框
        const checkbox = document.evaluate('//*[@id="cbx_all"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

        if (checkbox && checkbox.type === 'checkbox' && !checkbox.checked) {
            checkbox.checked = true;
            console.log('已自动选中全选复选框');

            // 触发必要的事件,确保选中状态生效
            checkbox.dispatchEvent(new Event('change', { bubbles: true }));
            checkbox.dispatchEvent(new Event('click', { bubbles: true }));
        }
    }

    // 主函数,统一执行所有操作
    function executeAllOperations() {
        autoSelectWarehouse();
        autoSelectAll();
    }

    // 首先检查URL并处理跳转
    checkAndRedirect();

    // 页面加载完成后执行所有操作
    window.addEventListener('load', executeAllOperations);

    // 使用MutationObserver监听DOM变化,确保动态加载的内容也能被处理
    const observer = new MutationObserver(executeAllOperations);
    observer.observe(document.body, { childList: true, subtree: true });

    // 初始执行一次,确保页面加载时就处理
    executeAllOperations();
})();