Greasy Fork is available in English.

CWO

Automatically runs the CWO export process when JDE CWO is opened

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
    });

})();