zitot

aoe2.net regex Filter 0.3

Nov 5th, 2021 (edited)
682
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.3
  5. // @description  second searchbox with OR regex, lowercase and capital doesn't matter so don't use /i. parses gametype and lobby name. Usage: a|b returns anything with a or b. Note that Regular Search takes precedence, so if you have no results, clear the first search and try again. The page will refresh every 3 seconds.
  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.  
  14. // ==/UserScript==
  15. (function() {
  16.     'use strict';
  17.     setTimeout(main_Func, 400); // Wait 400 milliseconds after page load before starting the script
  18. })();
  19.  
  20. // TODO: do not refresh if tab is not focused
  21. // TODO: Alerts for iOS users through Spontit app if game is available
  22. // TODO: Allow users to set refresh frequency
  23. // TODO: Allow user to pause refresh
  24.  
  25. var searchCount = 0;
  26. function SearchIt() {
  27.     var TABLE = document.querySelector("table#aoe2de-lobbies-table");
  28.     var ROWS = TABLE.rows;
  29.     var NEWSEARCHBOX = document.getElementById("regex-search");
  30.     // note: I should just make a new fragment and then set its display to ""
  31.     // setting each row's display one at a time will cause lots of page reflow
  32.     // that's what i did with my hp business outlet filter
  33.  
  34.     for (let i = ROWS.length - 1; i >= 1; i--) {
  35.         let row = ROWS[i];
  36.         let cell1 = row.cells[1];
  37.         let cell3 = row.cells[3];
  38.  
  39.         let ALLCAPS1 = cell1.innerHTML.toUpperCase();
  40.         let ALLCAPS3 = cell3.innerHTML.toUpperCase();
  41.         let search1 = ALLCAPS1.search(NEWSEARCHBOX.value.toUpperCase());
  42.         let search2 = ALLCAPS3.search(NEWSEARCHBOX.value.toUpperCase());
  43.  
  44.         // string.search returns -1 if it fails to find a match
  45.         if(search1 === -1 && search2 === -1) {
  46.             //hideRow(row);
  47.             row.style.display = "none";
  48.         } else {
  49.             //showRow(row);
  50.             row.style.display = "";
  51.         }
  52.     }
  53.     console.log("SearchCount = " + (searchCount++) + "\n");
  54. }
  55.  
  56. function main_Func(){
  57.     // Cell 0 is ignorable
  58.     // Cell 1 is Type (Random Map, Scenario, ..)
  59.     // Cell 2 is ignorable
  60.     // Cell 3 is Lobby Name
  61.     // Cell 4 is Location (Coastal, Arabia, ..)
  62.  
  63.     // Custom Input with OR implemented...
  64.     //document.querySelector("table#aoe2de-lobbies-table").rows[1].cells[3].innerHTML.search("CBA|Michi")
  65.     // REGEX SEARCH
  66.  
  67.  
  68.     var OLDSEARCHBOX = document.querySelector("div label input.form-control.form-control-sm");
  69.     var SEARCHNODE = document.querySelector("div#aoe2de-lobbies-table_filter label");
  70.     if (!OLDSEARCHBOX) {
  71.         console.log("OLDSEARCHBOX is null, timeout for 1 second and try again");
  72.         setTimeout(main_Func, 1000);
  73.         return;
  74.     } else {
  75.         if (!SEARCHNODE) {
  76.             console.log("OLDSEARCHBOX is not null, but SEARCHNODE is null.\ntimeout for 1 second and try again");
  77.             setTimeout(main_Func, 1000);
  78.             return;
  79.         } else {
  80.             console.log("OLDSEARCHBOX is not null and SEARCHNODE is not null.");
  81.             //console.log("Proceed to disable old search box and create new search box.");
  82.             console.log("Proceed to create new search box.");
  83.             console.log("Value of OLDSEARCHBOX = " + OLDSEARCHBOX.value + "\nProceed to clear input value");
  84.             OLDSEARCHBOX.value = "";
  85.             console.log("Value of OLDSEARCHBOX = " + OLDSEARCHBOX.value + "");
  86.  
  87.             // Old search box is absolute
  88.             // OLDSEARCHBOX.disabled= "true";
  89.             // SEARCHNODE.removeChild(OLDSEARCHBOX);
  90.             OLDSEARCHBOX.placeholder = "Default Search";
  91.             var NEWSEARCHBOX = document.createElement("input");
  92.             NEWSEARCHBOX.id = 'regex-search';
  93.             NEWSEARCHBOX.setAttribute("type", "text");
  94.             NEWSEARCHBOX.onkeyup = SearchIt;
  95.             NEWSEARCHBOX.placeholder="Enhanced: Use | for OR";
  96.             SEARCHNODE.appendChild(NEWSEARCHBOX);
  97.             var INTERVAL_TIME = 3000;  // Set to 3 seconds
  98.             setInterval(SearchIt, INTERVAL_TIME);
  99.  
  100.         }
  101.     }
  102. }
  103.  
Add Comment
Please, Sign In to add comment