Barcode Modifier General

Automatically modifies barcode input on bgmdolly.gminvent.fr

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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

(function () {
    'use strict';

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

    function simulateCarriageReturn(inputField) {
        inputField.value += "\r";
        inputField.dispatchEvent(new Event('input', { bubbles: true }));
        inputField.dispatchEvent(new Event('change', { bubbles: true }));
        console.log("Injected raw carriage return and dispatched input/change events.");
    }

    document.addEventListener("keydown", function (event) {
        const inputFields = document.querySelectorAll("input[id$='input']");
        const inputField = Array.from(inputFields).find(field => field === event.target);

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

        console.log(`Key pressed: "${event.key}" on field ID: ${inputField.id}`);

        if (event.key === "Tab") {
            event.preventDefault();
            event.stopImmediatePropagation();

            let barcode = inputField.value.trim();
            console.log("Original barcode:", barcode);

            let modifiedBarcode = null;

            // Case 1: Starts with "la", "re", "ch", "cm" followed by digits
            const match = barcode.match(/^(la|re|ch|cm)(\d+)$/i);
            if (match) {
                const prefix = match[1];
                const digits = match[2];
                const totalLength = 10;
                const paddedDigits = digits.padStart(totalLength - prefix.length, '0');
                modifiedBarcode = "SEMLG" + prefix + paddedDigits;
                console.log("Matched prefix case → transformed to:", modifiedBarcode);
            }

            // Case 2: Starts with "A "
            else if (barcode.startsWith("A ")) {
                modifiedBarcode = "cmdb0a000" + barcode.substring(2);
                console.log("Matched 'A ' case → transformed to:", modifiedBarcode);
            }

            if (modifiedBarcode !== null) {
                inputField.value = modifiedBarcode;
                inputField.dispatchEvent(new Event('input', { bubbles: true }));
                inputField.dispatchEvent(new Event('change', { bubbles: true }));
                console.log("Modified barcode applied and events dispatched.");
            } else {
                console.log("No matching pattern. No transformation applied.");
            }

            simulateCarriageReturn(inputField);
        }
    });
})();