CWO

Automatically runs the CWO export process when JDE CWO is opened

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==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);
    });

})();