Keep Focus and Display Regex Match

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

  1. // ==UserScript==
  2. // @name Keep Focus and Display Regex Match
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Keep focus on a specific text field, clear its content, and display regex matches
  6. // @author You
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12.  
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17.  
  18.  
  19. if (!document.title.includes("Item Inventory by Locn")) {
  20. return;
  21. }
  22.  
  23.  
  24.  
  25. // Create a container to display the matching text
  26. const container = document.createElement('div');
  27. container.style.position = 'fixed';
  28. container.style.top = '50%';
  29. container.style.left = '50%';
  30. container.style.transform = 'translate(-50%, -50%)';
  31. container.style.background = 'red'; // Set background to red
  32. container.style.color = 'white'; // Set text color to white
  33. container.style.zIndex = '9999';
  34. container.style.padding = '10px';
  35. container.style.whiteSpace = 'pre'; // Preserve whitespace and newlines
  36. //container.style.fontWeight = 'bold'; // Make text bold
  37. container.style.textAlign = 'center'; // Center-align text
  38. container.id = 'regexMatchContainer';
  39. document.body.appendChild(container);
  40.  
  41.  
  42.  
  43. // Regular expressions to match the patterns
  44. const regex1 = /AS[A-Z0-9]-[A-Z0-9]{2}-[A-Z0-9]{2}-[A-Z0-9]{3}/g;
  45. const regex2 = /TI/g; // New regex for "TI"
  46.  
  47.  
  48.  
  49. // Function to keep focus on the text field and clear its content
  50. function keepFocus() {
  51. const element = document.querySelector('input[type="text"]');
  52. if (element && document.activeElement !== element) {
  53. element.focus();
  54. element.value = ''; // Clear the content
  55. }
  56. }
  57.  
  58.  
  59.  
  60. // Function to find and display matching text
  61. function displayMatch() {
  62. // Clear previous matches
  63. document.getElementById('regexMatchContainer').innerText = '';
  64.  
  65.  
  66.  
  67. const bodyText = document.body.innerText;
  68. const matches1 = bodyText.match(regex1) || [];
  69. const matches2 = bodyText.match(regex2) || [];
  70.  
  71.  
  72.  
  73. let combinedMatches = matches1;
  74.  
  75.  
  76.  
  77. // Include "TI" matches only if their count is more than 1
  78. if (matches2.length > 2) {
  79. combinedMatches = combinedMatches.concat(matches2);
  80. }
  81.  
  82.  
  83.  
  84. // Remove duplicates from the combined matches
  85. const allMatches = Array.from(new Set(combinedMatches));
  86.  
  87.  
  88.  
  89. // If matches are found, display them
  90. if (allMatches.length) {
  91. document.getElementById('regexMatchContainer').innerText = allMatches.join('\n');
  92. }
  93. }
  94.  
  95.  
  96.  
  97. // Run the keepFocus and displayMatch functions every 500 milliseconds
  98. setInterval(() => {
  99. keepFocus();
  100. displayMatch();
  101. }, 500);
  102.  
  103.  
  104.  
  105. })();