Auto Barcode Modifier

Automatically modifies barcode input on bgmdolly.gminvent.fr

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Auto Barcode Modifier
// @version      1.0
// @description  Automatically modifies barcode input on bgmdolly.gminvent.fr
// @match        *://bgmdolly.gminvent.fr/*
// @grant        none
// @namespace https://greasyfork.org/users/1448578
// ==/UserScript==

(function() {
    'use strict';

    console.log("Tampermonkey script loaded on bgmdolly.gminvent.fr");

    document.addEventListener("keydown", function(event) {
        // Select all input fields where ID ends with "input"
        const inputFields = document.querySelectorAll("input[id$='input']");

        // Find the active input field (the one where Enter was pressed)
        const inputField = Array.from(inputFields).find(field => field === event.target);

        if (!inputField) {
            console.log("Error: No matching input field found.");
            return;
        }

        // console.log("encontrado:", inputField.id);

        if (event.key === "Enter") {
            let barcode = inputField.value;
            console.log("Current barcode value:", barcode);

            if (barcode.startsWith("A ")) {
                console.log("Barcode starts with 'A ', modifying it...");

                event.preventDefault(); // **STOP the original Enter from executing**
                event.stopImmediatePropagation(); // **Ensures no other event listeners process Enter**

                // **Modify barcode (Remove "A " and add "cmdb0a000" as prefix)**
                let modifiedBarcode = "cmdb0a000" + barcode.substring(2);
                console.log("Modified barcode:", modifiedBarcode);

                // **Set the new modified barcode**
                inputField.value = modifiedBarcode;

                // **Force a new Enter keypress AFTER modification**
                setTimeout(() => {
                    let enterEvent = new KeyboardEvent("keydown", {
                        key: "Enter",
                        code: "Enter",
                        keyCode: 13,
                        which: 13,
                        bubbles: true
                    });
                    inputField.dispatchEvent(enterEvent); // **Simulated Enter**
                    console.log("Simulated Enter key press.");
                }, 10); // Small delay ensures value updates before new Enter
            } else {
                console.log("Barcode does not start with 'A ', proceeding with normal Enter.");
            }
        }
    });

})();