MEST Modeling Helper

Modeling Helper

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

// ==UserScript==
// @name         MEST Modeling Helper
// @namespace    joyings.com.cn
// @version      1.5.6
// @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();
    updateIndexBtn();
    semiCellEvent();
    addObserverIfDesiredNodeAvailable();
    window.loaded = false;
    window.loadedBtn = false;
    window.loadedSemi = false;
    window.autofill = 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);
            window.setTimeout(updateIndexBtn, 500);
            window.setTimeout(semiCellEvent, 500);
            composeObserver.disconnect();
            addObserverIfDesiredNodeAvailable();
        });
        composeObserver.observe(composeBox, config);
    }

    // 选择自己
    async function semiCellEvent() {
        if (!$('button:contains(" 选择自己 ")')[0]) {
            window.loadedSemi = false;
            await sleep(500);
            semiCellEvent();
            return;
        }
        if (window.loadedSemi) {
            return;
        }
        window.loadedSemi = true;
        $('button:contains(" 选择自己 ")')[0].addEventListener('click', async function (e) {
            await sleep(100);
            dbck();
        });
        await sleep(200);
        while (!$('button:contains(" 选择 ")', $('button:contains(" 选择自己 ")')[0].parentElement.parentElement)) {
            await sleep(100);
        }
        for (let btn of $('button:contains(" 选择 ")', $('button:contains(" 选择自己 ")')[0].parentElement.parentElement)) {
            btn.addEventListener('click', async function (e) {
                await sleep(500);
                dbck();
                await sleep(500);
                dbck();
            });
        }
        semiCellEvent();
    }

    // 添加按钮
    async function updateIndexBtn() {
        if (!$('button:contains(" 新增一行 ")')[0]) {
            window.loadedBtn = false;
            await sleep(500);
            updateIndexBtn();
            return;
        }
        if (window.loadedBtn) {
            return;
        }
        window.loadedBtn = true;
        let btnRow = $('button:contains(" 新增一行 ")')[0].parentElement;
        var btn = document.createElement('button');
        btn.setAttribute('title', '一键更新所有序号');
        btn.setAttribute('id', 'oneKeyButton');
        btn.setAttribute('type', 'button');
        btn.onclick = updateIndex;
        btn.appendChild(document.createTextNode('⇩更新序号'));

        var btn2 = document.createElement('button');
        btn2.setAttribute('title', '重新设置自动填充');
        btn2.setAttribute('id', 'reloadBtn');
        btn2.setAttribute('type', 'button');
        btn2.onclick = dbck;
        btn2.appendChild(document.createTextNode('⟳重载自动填充'));

        var btn3 = document.createElement('button');
        btn3.setAttribute('title', '一键设置工艺');
        btn3.setAttribute('id', 'updateProc');
        btn3.setAttribute('type', 'button');
        btn3.onclick = updateProcess;
        btn3.appendChild(document.createTextNode('⇩一键设置工艺'));

        btnRow.appendChild(btn);
        btnRow.appendChild(btn3);
        btnRow.appendChild(btn2);
    }

    // 一键更新序号
    async function updateIndex() {
        if (!document.querySelectorAll('[class="edit-cell"]')[0]) {
            await sleep(500);
            updateIndex();
            return;
        }
        let bodyRows = document.querySelectorAll('[class="edit-cell"]')[0].closest('table').lastChild.rows;
        const procIndex = $('th:contains(" 上级序号 ")')[0].cellIndex;

        for (let i of bodyRows) {
            let indexCell = i.cells.item(procIndex);
            if (i.cells.item(3).firstChild.firstChild.firstChild.textContent == '始工序') {
                continue;
            }
            indexCell.firstChild.firstChild.firstChild.click();
            await sleep(50);
            indexCell.querySelector('[class="el-icon-setting"]').closest('a').click();
            await sleep(100);
            $('button:contains(" 选择 ")')[i.rowIndex].click();
            await sleep(50);
        }
    }

    // 一键设置工艺
    async function updateProcess() {
        if (!document.querySelectorAll('[class="edit-cell"]')[0]) {
            await sleep(500);
            updateProcess();
            return;
        }
        let bodyRows = document.querySelectorAll('[class="edit-cell"]')[0].closest('table').lastChild.rows;
        const processIndex = $('th:contains(" 工序名称 ")')[0].cellIndex;
        const semiIndex = $('th:contains(" 半成品/成品名称 ")')[0].cellIndex;
        window.autofill = true;
        for (let i of bodyRows) {
            let name = bodyRows.item(i.rowIndex).cells.item(semiIndex).firstChild.firstChild.firstChild.textContent;

            let processName = getProcName(name);
            await sleep(50);
            i.cells.item(processIndex).firstChild.firstChild.firstChild.click()
            await sleep(50);
            i.querySelector('[placeholder="工序选择"]').closest('div').querySelector('button').click();
            await sleep(150);
            document.querySelectorAll('[placeholder="工序名称"]')[0].value = processName;
            await sleep(50);
            document.querySelectorAll('[placeholder="工序名称"]')[0].dispatchEvent(new Event('input', {
                bubbles: true
            }));
            await sleep(50);
            $('button:contains(" 搜索 ")')[0].click();
            if (!document.getElementById('semiNameLabel')) {
                let label1 = document.createElement('label');
                label1.classList.add("el-form-item__label");
                label1.setAttribute('id', 'semiNameLabel');
                label1.appendChild(document.createTextNode(name));
                $('button:contains(" 搜索 ")')[0].parentElement.appendChild(label1);
            }
            await sleep(150);
            // 自动选择第一个工序名称
            while (!$('td:contains("GX")')[0]) {
                await sleep(100);
            }
            if ($('button:contains(" 选择 ")', $('td:contains("GX")')[0].parentElement.parentElement).length == 1) {
                $('button:contains(" 选择 ")', $('td:contains("GX")')[0].parentElement)[0].click();
                await sleep(200);
            } else {
                while ($('td:contains("GX")')[0]) {
                    await sleep(200);
                }
            }
        }
        window.autofill = false;
    }

    // 新增一行自动填写
    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);
        const alias = document.querySelector('[placeholder="别名"]').closest('textarea').value;
        const semiIndex = $('th:contains(" 半成品/成品名称 ")')[0].cellIndex;
        const materialIndex = $('th:contains(" 所需物料集 ")')[0].cellIndex;
        const procIndex = $('th:contains(" 上级序号 ")')[0].cellIndex;
        const processIndex = $('th:contains(" 工序名称 ")')[0].cellIndex;

        if ($('div:contains(" 暂未设置工艺 ")')[0]) {
            $('button:contains(" 设置工艺版本 ")')[0].addEventListener('click', async function (e) {
                await sleep(150);
                $('button:contains(" 新增工艺版本 ")')[0].click();
                await sleep(150);
                if (!$('button:contains(" 选择 ")')[0]) {
                    document.querySelectorAll('[placeholder="输入工艺版本名称"]')[0].value = "default";
                    document.querySelectorAll('[placeholder="输入工艺版本名称"]')[0].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.firstChild.click();
            await sleep(100);
            $('button:contains(" 选择 ")')[bodyRows.length - 1].click();
            await sleep(100);

            // 设置物料
            let prevSemi = "";
            if (bodyRows.length > 1) {
                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);
            if (!$('button:contains(" 移除 ")')[0]) {
                $('button:contains("新增行")')[0].click();
                await sleep(100);
            }
            document.querySelector('[placeholder="物料选择"]').closest('div').querySelector('button').click();
            await sleep(100);
            document.querySelectorAll('[placeholder="存货名称"]')[1].value = prevSemi;
            document.querySelectorAll('[placeholder="存货名称"]')[1].dispatchEvent(new Event('input', {
                bubbles: true
            }));
            await sleep(50);
            $('button:contains(" 搜索 ")')[0].click();
            //$('button:contains(" 选择 ")')[0].click();
            await sleep(150);
            dbck();
        });
        await sleep(150);
        dbck();
    }

    // 整个表格自动填写
    async function dbck() {
        if (!document.querySelectorAll('[class="edit-cell"]')[0]) {
            await sleep(500);
            dbck();
            return;
        }
        let bodyRows = document.querySelectorAll('[class="edit-cell"]')[0].closest('table').lastChild.rows;
        const alias = document.querySelector('[placeholder="别名"]').closest('textarea').value;
        const semiIndex = $('th:contains(" 半成品/成品名称 ")')[0].cellIndex;
        const processIndex = $('th:contains(" 工序名称 ")')[0].cellIndex;
        const materialIndex = $('th:contains(" 所需物料集 ")')[0].cellIndex;
        const procIndex = $('th:contains(" 上级序号 ")')[0].cellIndex;

        // 设置上级序号
        for (let i of bodyRows) {
            let indexCell = i.cells.item(procIndex);
            indexCell.addEventListener('auxclick', async function (e) {
                indexCell.querySelector('[class="el-icon-setting"]').closest('a').click();
                await sleep(100);
                $('button:contains(" 选择 ")')[i.rowIndex].click();
            });
        }

        // 半成品
        for (let i of bodyRows) {
            let semiCell = i.cells.item(semiIndex);
            semiCell.addEventListener('click', async function (e) {
                i.querySelector('[placeholder="输出半成品"]').closest('div').querySelector('button').click();
                await sleep(150);
                document.querySelectorAll('[placeholder="存货别名"]')[1].value = alias;
                await sleep(50);
                document.querySelectorAll('[placeholder="存货别名"]')[1].dispatchEvent(new Event('input', {
                    bubbles: true
                }));
                await sleep(50);
                $('button:contains(" 搜索 ")')[0].click();
            });
        }

        // 物料集
        for (let i of bodyRows) {
            let prevSemi = '';
            if (i.rowIndex > 0) {
                prevSemi = bodyRows.item(i.rowIndex - 1).cells.item(semiIndex).firstChild.firstChild.firstChild.textContent;
            }
            let matCell = i.cells.item(materialIndex);
            matCell.addEventListener('click', async function (e) {
                matCell.firstChild.firstChild.click();
                await sleep(150);
                if (!$('button:contains(" 移除 ")')[0]) {
                    $('button:contains("新增行")')[0].click();
                    await sleep(100);
                }
                i.querySelector('[placeholder="物料选择"]').closest('div').querySelector('button').click();
                await sleep(100);
                document.querySelectorAll('[placeholder="存货名称"]')[1].value = prevSemi;
                document.querySelectorAll('[placeholder="存货名称"]')[1].dispatchEvent(new Event('input', {
                    bubbles: true
                }));
                await sleep(50);
                $('button:contains(" 搜索 ")')[0].click();
                //$('button:contains(" 选择 ")')[0].click();
            });
        }

        // 工序
        for (let i of bodyRows) {
            let lastProcessCell = i.cells.item(processIndex);
            lastProcessCell.addEventListener('click', async function (e) {
                if (!window.autofill) {
                    let name = bodyRows.item(i.rowIndex).cells.item(semiIndex).firstChild.firstChild.firstChild.textContent;
                    let processName = getProcName(name);
                    i.querySelector('[placeholder="工序选择"]').closest('div').querySelector('button').click();
                    await sleep(150);
                    document.querySelectorAll('[placeholder="工序名称"]')[0].value = processName;
                    await sleep(50);
                    document.querySelectorAll('[placeholder="工序名称"]')[0].dispatchEvent(new Event('input', {
                        bubbles: true
                    }));
                    await sleep(50);
                    $('button:contains(" 搜索 ")')[0].click();
                    if (!document.getElementById('semiNameLabel')) {
                        let label1 = document.createElement('label');
                        label1.classList.add("el-form-item__label");
                        label1.setAttribute('id', 'semiNameLabel');
                        label1.appendChild(document.createTextNode(name));
                        $('button:contains(" 搜索 ")')[0].parentElement.appendChild(label1);
                    }
                    await sleep(150);
                    // 自动选择第一个工序名称
                    while (!$('td:contains("GX")')[0]) {
                        await sleep(100);
                    }
                    if ($('button:contains(" 选择 ")', $('td:contains("GX")')[0].parentElement.parentElement).length == 1) {
                        $('button:contains(" 选择 ")', $('td:contains("GX")')[0].parentElement)[0].click();
                    }
                }
            });
        }
    }

    function getProcName(name) {
        var processName = '';
        if (name.includes('打磨抛光')) {
            processName = '打磨抛光';
        } else if (name.includes('拉伸')) {
            processName = '拉伸';
        } else if (name.includes('飞边')) {
            processName = '飞边';
        } else if (name.includes('打磨')) {
            processName = '打磨';
        } else if (name.includes('烧焊')) {
            processName = '满焊';
        } else if ((new RegExp('.*冲(\\W|\\w)*孔.*')).test(name)) {
            processName = '冲孔';
        } else if (name.includes('验收')) {
            processName = '验收';
        } else if (name.includes('剪板')) {
            processName = '剪板';
        } else if (name.includes('开料')) {
            processName = '开料';
        } else if (name.includes('电镀')) {
            processName = '电镀';
        } else if (name.includes('电泳')) {
            processName = '电泳';
        } else if (name.includes('喷粉')) {
            processName = '喷粉';
        } else if (name.includes('喷涂')) {
            processName = '喷粉';
        } else if (name.includes('折弯')) {
            processName = '折弯';
        } else if (name.includes('注塑')) {
            processName = '注塑';
        } else if (name.includes('成型')) {
            processName = '成型';
        } else if (name.includes('弯线')) {
            processName = '弯线';
        } else if (name.includes('碰焊')) {
            processName = '碰焊';
        } else if (name.includes('抛光')) {
            processName = '抛光';
        } else if (name.includes('锯')) {
            processName = '锯';
        } else if (name.includes('冲')) {
            processName = '冲';
        } else if (name.includes('弯')) {
            processName = '弯';
        } else if (name.includes('压')) {
            processName = '压';
        } else if (name.includes('焊')) {
            processName = '焊';
        } else if (name.includes('激光')) {
            processName = '激光';
        } else if (name.includes('产品组装')) {
            processName = '产品组装';
        } else if (name.includes('铆')) {
            processName = '铆';
        } else if (name.includes('攻牙')) {
            processName = '攻牙';
        } else if (name.includes('锣')) {
            processName = '锣';
        } else if (name.includes('压印')) {
            processName = '压印';
        } else if (name.includes('调')) {
            processName = '调';
        } else if ((new RegExp('.*镀(\\W|\\w)')).test(name)) {
            processName = '镀';
        } else if (name.includes('清洗')) {
            processName = '清洗';
        } else {
            processName = '';
        }
        return processName;
    }
})();