CWO

Automatically runs the CWO export process when JDE CWO is opened

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         CWO
// @namespace    http://tampermonkey.net/
// @version      4.1
// @description  Automatically runs the CWO export process when JDE CWO is opened
// @match        https://jdepd.vline.com.au/jde/E1Menu.maf*
// @icon         https://static.vecteezy.com/system/resources/previews/009/628/419/non_2x/cwo-logo-cwo-letter-cwo-letter-logo-design-initials-cwo-logo-linked-with-circle-and-uppercase-monogram-logo-cwo-typography-for-technology-business-and-real-estate-brand-vector.jpg
// @grant        none
// @license      MIT
// @run-at       document-idle
// ==/UserScript==

(function () {
    'use strict';

    // Only run on CWO menu
    if (!window.location.href.includes("CWO")) {
        return;
    }

    console.log("🚀 CWO Auto Export Started");

    const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

    async function waitForElement(selector, matchFn = null, timeout = 30000) {
        const start = Date.now();

        while (Date.now() - start < timeout) {

            // Main document
            let elements = Array.from(document.querySelectorAll(selector));
            let found = matchFn ? elements.find(matchFn) : elements[0];

            if (found) {
                return found;
            }

            // Search iframes
            const iframes = document.querySelectorAll("iframe");

            for (const iframe of iframes) {
                try {
                    const doc = iframe.contentDocument || iframe.contentWindow.document;
                    if (!doc) continue;

                    let iframeElements = Array.from(doc.querySelectorAll(selector));
                    let iframeFound = matchFn ? iframeElements.find(matchFn) : iframeElements[0];

                    if (iframeFound) {
                        return iframeFound;
                    }
                } catch (e) {
                    // Ignore cross-origin frames
                }
            }

            await delay(500);
        }

        console.log("Timed out waiting for:", selector);
        return null;
    }

    function fullClick(element) {
        if (!element) return;

        ['mousedown', 'mouseup', 'click'].forEach(eventType => {
            element.dispatchEvent(
                new MouseEvent(eventType, {
                    bubbles: true,
                    cancelable: true,
                    view: window
                })
            );
        });
    }

    async function runScript() {

        // Give JDE time to initialise
        await delay(5000);

        try {

            //------------------------------------------
            // STEP 1
            //------------------------------------------

            console.log("Step 1");

            const cwoItem = await waitForElement(
                ".listText",
                el => el.textContent.trim() === "CWO"
            );

            if (cwoItem) {
                fullClick(cwoItem);
                console.log("Clicked CWO listText");
            } else {
                console.warn("CWO menu item not found");
            }

            await delay(3000);

            //------------------------------------------
            // STEP 2
            //------------------------------------------

            console.log("Step 2");

            const dropdown = await waitForElement("select#gtab0_1");

            if (dropdown) {

                const option = Array.from(dropdown.options).find(
                    o => o.text.trim() === "Show All Columns"
                );

                if (option) {
                    dropdown.value = option.value;
                    dropdown.dispatchEvent(new Event("change", { bubbles: true }));
                    console.log("Dropdown selected");
                }
            }

            await delay(2000);

            //------------------------------------------
            // STEP 3
            //------------------------------------------

            console.log("Step 3");

            const lastPage = await waitForElement("img#jdehtmlGridLast0_1");

            if (lastPage) {
                const link = lastPage.closest("a");
                fullClick(link || lastPage);
            }

            await delay(3000);

            //------------------------------------------
            // STEP 4
            //------------------------------------------

            console.log("Step 4");

            const exportButton = await waitForElement("img#jdehtmlExportData0_1");

            if (exportButton) {
                exportButton.click();
            }

            await delay(3000);

            //------------------------------------------
            // STEP 5
            //------------------------------------------

            console.log("Step 5");

            const fileType = await waitForElement("input#expImpFileType6");

            if (fileType) {
                fileType.click();
            }

            await delay(2000);

            //------------------------------------------
            // STEP 6
            //------------------------------------------

            console.log("Step 6");

            const loadButton = await waitForElement("input#loadValue");

            if (loadButton) {
                loadButton.click();
            }

            console.log("🎉 CWO Export Complete");

        } catch (e) {
            console.error(e);
        }
    }

    // Wait until page has finished loading
    window.addEventListener("load", () => {
        setTimeout(runScript, 2000);
    });

})();