MEST Modeling Helper

Modeling Helper

目前為 2022-10-10 提交的版本,檢視 最新版本

// ==UserScript==
// @name         MEST Modeling Helper
// @namespace    joyings.com.cn
// @version      0.4.1
// @description  Modeling Helper
// @author       zmz125000
// @match        http://*/mest/*
// @icon         http://www.google.com/s2/favicons?domain=openwrt.org
// @grant        none
// @license      MIT
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js
// @run-at       document-end
// ==/UserScript==

(function () {
    'use strict';

    // Your code here...
    // addClickActions();
    addObserverIfDesiredNodeAvailable();
    window.loaded = false;

    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    function addObserverIfDesiredNodeAvailable() {
        var composeBox = document.querySelectorAll('[class="el-popup-parent--hidden"]')[0];
        if (!composeBox) {
            //The node we need does not exist yet.
            //Wait 500ms and try again
            window.setTimeout(addObserverIfDesiredNodeAvailable, 500);
            return;
        }
        var config = {
            attributes: true,
        };
        var composeObserver = new MutationObserver(function () {
            window.setTimeout(addClickActions, 500);
            composeObserver.disconnect();
            addObserverIfDesiredNodeAvailable();
        });
        composeObserver.observe(composeBox, config);
    }

    async function addClickActions() {
        if (!$('button:contains(" 新增一行 ")')[0]) {
            window.loaded = false;
            await sleep(500);
            addClickActions();
            return
        }
        if (window.loaded) {
            return
        }
        window.loaded = true;

        await sleep(250);
        var semiIndex = $('th:contains(" 半成品/成品名称 ")')[0].cellIndex;
        var materialIndex = $('th:contains(" 所需物料集 ")')[0].cellIndex;
        var procIndex = $('th:contains(" 上级序号 ")')[0].cellIndex;

        $('button:contains(" 设置工艺版本 ")')[0].addEventListener('click', async function (e) {
            await sleep(150);
            $('button:contains(" 新增工艺版本 ")')[0].click();
            await sleep(150);
            document.querySelectorAll('[placeholder="输入工艺版本名称"]')[1].value = "default";
            document.querySelectorAll('[placeholder="输入工艺版本名称"]')[1].dispatchEvent(new Event('input', {
                bubbles: true
            }));
            await sleep(50);
            $('button:contains(" 选择 ")')[0].click();
        })

        $('button:contains(" 新增一行 ")')[0].addEventListener('click', async function (e) {
            let bodyRows = document.querySelectorAll('[class="edit-cell"]')[0].closest('table').lastChild.rows;
            // 设置上级序号
            let lastIndexCell = bodyRows.item(bodyRows.length - 1).cells.item(procIndex);
            lastIndexCell.firstChild.firstChild.firstChild.click();
            await sleep(50);
            // lastIndexCell.firstChild.firstChild.firstChild.lastChild.click();
            lastIndexCell.firstChild.firstChild.firstChild.firstChild.click()
            await sleep(150);
            $('button:contains(" 选择 ")')[bodyRows.length - 1].click();
            await sleep(150);
            // 设置物料
            let prevSemi = bodyRows.item(bodyRows.length - 2).cells.item(semiIndex).firstChild.firstChild.firstChild.textContent;
            let lastMatCell = bodyRows.item(bodyRows.length - 1).cells.item(materialIndex);
            lastMatCell.firstChild.firstChild.click();
            await sleep(150);
            $('button:contains("新增行")')[0].click();

            await sleep(150);
            document.querySelector('[placeholder="物料选择"]').closest('div').querySelector('button').click();
            await sleep(150);
            document.querySelectorAll('[placeholder="存货名称"]')[1].value = prevSemi;
            document.querySelectorAll('[placeholder="存货名称"]')[1].dispatchEvent(new Event('input', {
                bubbles: true
            }));
            await sleep(350);
            $('button:contains(" 搜索 ")')[0].click();
            //$('button:contains(" 选择 ")')[0].click();
        })
    }
})();