zitot

AOE2.net REGEX Filter 0.2

Nov 3rd, 2021 (edited)
1,284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         AOE2.net REGEX Search
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.2
  5. // @description  replaces searchbox, parses gametype and lobby names only atm. usage: michi|cba for cba or michi. BUG: this script is bugged - if text was typed into the old searchbox in the previous session, aoe2.net will keep it in the next sessions. I tried setting the value of the old input to "" and that failed to fix it so clear it yourself before enabling this script.
  6. // @author       xThomas
  7. // @match        https://aoe2.net/
  8. // @icon         https://www.google.com/s2/favicons?domain=aoe2.net
  9. // @grant        none
  10. // @run-at       document-end
  11.  
  12.  
  13. // ==/UserScript==
  14. (function() {
  15.     'use strict';
  16.     setTimeout(main_Func, 400); // Wait 400 milliseconds after page load before starting the script
  17. })();
  18.  
  19. var searchCount = 0;
  20. function SearchIt() {
  21.     var TABLE = document.querySelector("table#aoe2de-lobbies-table");
  22.     var ROWS = TABLE.rows;
  23.     var NEWSEARCHBOX = document.getElementById("regex-search");
  24.     // note: I should just make a new fragment and then set its display to ""
  25.     // setting each row's display one at a time will cause lots of page reflow
  26.     // that's what i did with my hp business outlet filter
  27.  
  28.     for (let i = ROWS.length - 1; i >= 1; i--) {
  29.         let row = ROWS[i];
  30.         let cell1 = row.cells[1];
  31.         let cell3 = row.cells[3];
  32.  
  33.         let ALLCAPS1 = cell1.innerHTML.toUpperCase();
  34.         let ALLCAPS3 = cell3.innerHTML.toUpperCase();
  35.         let search1 = ALLCAPS1.search(NEWSEARCHBOX.value.toUpperCase());
  36.         let search2 = ALLCAPS3.search(NEWSEARCHBOX.value.toUpperCase());
  37.  
  38.         // string.search returns -1 if it fails to find a match
  39.         if(search1 === -1 && search2 === -1) {
  40.             //hideRow(row);
  41.             row.style.display = "none";
  42.         } else {
  43.             //showRow(row);
  44.             row.style.display = "";
  45.         }
  46.     }
  47.     console.log("SearchCount = " + (searchCount++) + "\n");
  48. }
  49.  
  50. function main_Func(){
  51.     // Cell 0 is ignorable
  52.     // Cell 1 is Type (Random Map, Scenario, ..)
  53.     // Cell 2 is ignorable
  54.     // Cell 3 is Lobby Name
  55.     // Cell 4 is Location (Coastal, Arabia, ..)
  56.  
  57.     // Custom Input with OR implemented...
  58.     //document.querySelector("table#aoe2de-lobbies-table").rows[1].cells[3].innerHTML.search("CBA|Michi")
  59.     // REGEX SEARCH
  60.  
  61.  
  62.     var OLDSEARCHBOX = document.querySelector("div label input.form-control.form-control-sm");
  63.     var SEARCHNODE = document.querySelector("div#aoe2de-lobbies-table_filter label");
  64.     if (!OLDSEARCHBOX) {
  65.         console.log("OLDSEARCHBOX is null, timeout for 1 second and try again");
  66.         setTimeout(main_Func, 1000);
  67.         return;
  68.     } else {
  69.         if (!SEARCHNODE) {
  70.             console.log("OLDSEARCHBOX is not null, but SEARCHNODE is null.\ntimeout for 1 second and try again");
  71.             setTimeout(main_Func, 1000);
  72.             return;
  73.         } else {
  74.             console.log("OLDSEARCHBOX is not null and SEARCHNODE is not null.\nProceed to disable old search box and create new search box.");
  75.             console.log("Value of OLDSEARCHBOX = " + OLDSEARCHBOX.value + "\nProceed to clear input value");
  76.             OLDSEARCHBOX.value = "";
  77.             console.log("Value of OLDSEARCHBOX = " + OLDSEARCHBOX.value + "");
  78.  
  79.             OLDSEARCHBOX.disabled= "true";
  80.             SEARCHNODE.removeChild(OLDSEARCHBOX);
  81.             var NEWSEARCHBOX = document.createElement("input");
  82.             NEWSEARCHBOX.id = 'regex-search';
  83.             NEWSEARCHBOX.setAttribute("type", "text");
  84.             NEWSEARCHBOX.onkeyup = SearchIt;
  85.             SEARCHNODE.appendChild(NEWSEARCHBOX);
  86.             var INTERVAL_TIME = 3000;  // Set to 3 seconds
  87.             setInterval(SearchIt, INTERVAL_TIME);
  88.  
  89.         }
  90.     }
  91. }
  92.  
Add Comment
Please, Sign In to add comment