Keep Focus and Display Regex Match

Keep focus on a specific text field, clear its content, and display regex matches

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

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

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         Keep Focus and Display Regex Match
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Keep focus on a specific text field, clear its content, and display regex matches
// @author       You
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

 

 

(function() {
    'use strict';

 

 

    if (!document.title.includes("Item Inventory by Locn")) {
        return;
    }

 

 

    // Create a container to display the matching text
    const container = document.createElement('div');
    container.style.position = 'fixed';
    container.style.top = '50%';
    container.style.left = '50%';
    container.style.transform = 'translate(-50%, -50%)';
    container.style.background = 'red';  // Set background to red
    container.style.color = 'white';  // Set text color to white
    container.style.zIndex = '9999';
    container.style.padding = '10px';
    container.style.whiteSpace = 'pre';  // Preserve whitespace and newlines
    //container.style.fontWeight = 'bold'; // Make text bold
    container.style.textAlign = 'center'; // Center-align text
    container.id = 'regexMatchContainer';
    document.body.appendChild(container);

 

 

    // Regular expressions to match the patterns
    const regex1 = /AS[A-Z0-9]-[A-Z0-9]{2}-[A-Z0-9]{2}-[A-Z0-9]{3}/g;
    const regex2 = /TI/g;  // New regex for "TI"

 

 

    // Function to keep focus on the text field and clear its content
    function keepFocus() {
        const element = document.querySelector('input[type="text"]');
        if (element && document.activeElement !== element) {
            element.focus();
            element.value = '';  // Clear the content
        }
    }

 

 

// Function to find and display matching text
function displayMatch() {
    // Clear previous matches
    document.getElementById('regexMatchContainer').innerText = '';

 

 

    const bodyText = document.body.innerText;
    const matches1 = bodyText.match(regex1) || [];
    const matches2 = bodyText.match(regex2) || [];

 

 

    let combinedMatches = matches1;

 

 

    // Include "TI" matches only if their count is more than 1
    if (matches2.length > 2) {
        combinedMatches = combinedMatches.concat(matches2);
    }

 

 

    // Remove duplicates from the combined matches
    const allMatches = Array.from(new Set(combinedMatches));

 

 

    // If matches are found, display them
    if (allMatches.length) {
        document.getElementById('regexMatchContainer').innerText = allMatches.join('\n');
    }
}

 

 

    // Run the keepFocus and displayMatch functions every 500 milliseconds
    setInterval(() => {
        keepFocus();
        displayMatch();
    }, 500);

 

 

})();