Keep Focus and Display Regex Match

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

 

 

})();