MediaWiki:Navbox.js: Difference between revisions

Made most console log messages conditional on an enable/disable variable to be used for debugging
No edit summary
(Made most console log messages conditional on an enable/disable variable to be used for debugging)
 
Line 6: Line 6:
*/
*/
console.log("Navbox JS Version 8.27.22");
console.log("Navbox JS Version 8.27.22");
//Change boolean to enable/disable console.log statements (debugging)
const enableConsoleLogs = true;


var shipArr = []; //Array of ship objects -- see "function Ship(...)" for constructor & properties
var shipArr = []; //Array of ship objects -- see "function Ship(...)" for constructor & properties
Line 15: Line 18:
//Array of valid category-based types (classes/events)
//Array of valid category-based types (classes/events)
const types = ["AI","Fighters","Hidden Event","Halloween 2016","Halloween 2017","Christmas 2017","Fourth of July 2018","Halloween 2018","Christmas 2018","Fourth of July 2019","Retro Ship Event 2019","Halloween 2019","Christmas 2019","Promotion","April Fools 2020","Fourth of July 2020","Retro Ship Event 2020","Halloween 2020","Black Friday 2020","Christmas 2020","Fourth of July 2021","Retro Ship Event 2021","Halloween 2021","Christmas 2021","Retro Ship Event 2022","Super Capital","Advanced","Miner","Freighter","Frigate","Destroyer","Cruiser","Battlecruiser","Battleship","Dreadnought","Carrier","Admin"];
const types = ["AI","Fighters","Hidden Event","Halloween 2016","Halloween 2017","Christmas 2017","Fourth of July 2018","Halloween 2018","Christmas 2018","Fourth of July 2019","Retro Ship Event 2019","Halloween 2019","Christmas 2019","Promotion","April Fools 2020","Fourth of July 2020","Retro Ship Event 2020","Halloween 2020","Black Friday 2020","Christmas 2020","Fourth of July 2021","Retro Ship Event 2021","Halloween 2021","Christmas 2021","Retro Ship Event 2022","Super Capital","Advanced","Miner","Freighter","Frigate","Destroyer","Cruiser","Battlecruiser","Battleship","Dreadnought","Carrier","Admin"];
//Array of pages to be removed from shipArr -- all non-ship pages; first list wiill sort generically for anything that contains the given text. Second list will only eliminate text exact matches
//Array of pages to be removed from shipArr -- all non-ship pages; first list will sort generically for anything that contains the given text. Second list will only eliminate text exact matches
const generalUnwantedNamesTypes = ["Stronghold","AI Fighters","Pirates","Boss Spawn Time","Build Menu","Aliens","Ships","Tier List","All Ship","INVALID","Category"];
const generalUnwantedNamesTypes = ["Stronghold","AI Fighters","Pirates","Boss Spawn Time","Build Menu","Aliens","Ships","Tier List","All Ship","INVALID","Category"];
const exactUnwantedNamesTypes = ["U.N.E","Kneall"];
const exactUnwantedNamesTypes = ["U.N.E","Kneall"];
Line 46: Line 49:
//Called to start executing all other functions
//Called to start executing all other functions
function startNavboxFill(){
function startNavboxFill(){
console.log("Checking if navbox fill can run...");
if(enableConsoleLogs) console.log("Checking if navbox fill can run...");
if(canRun){
if(canRun){
console.log("Can run; Navbox fill started...");
if(enableConsoleLogs) console.log("Can run; Navbox fill started...");
canRun = false;
canRun = false;
if(flag === 0) updateButtons();
if(flag === 0) updateButtons();
Line 85: Line 88:
else if(response.status == 404){
else if(response.status == 404){
shipArr[idx].name += " (INVALID - 404)";
shipArr[idx].name += " (INVALID - 404)";
             console.log("Warning: Invalid entry at "+idx+" (Error 404 -- Invalid URL)");
             if(enableConsoleLogs) console.log("Warning: Invalid entry at "+idx+" (Error 404 -- Invalid URL)");
response.text().then(rawTxt => {
response.text().then(rawTxt => {
var page = parser.parseFromString(rawTxt, 'text/html');
var page = parser.parseFromString(rawTxt, 'text/html');
Line 124: Line 127:
         returnPage(shipArr[i].name,fillPriceType,i);
         returnPage(shipArr[i].name,fillPriceType,i);
     }
     }
     console.log("ALL SHIP PAGE FETCH REQUESTS SENT");
     if(enableConsoleLogs) console.log("ALL SHIP PAGE FETCH REQUESTS SENT");
}
}
//fill values in shipArr
//fill values in shipArr
Line 145: Line 148:
if(c === undefined) p = "N/A--"+name;
if(c === undefined) p = "N/A--"+name;
else{
else{
console.log(shipArr[idx].name + ": Missing or invalid price parameter; reverting to credit cost: " + c.innerText);
if(enableConsoleLogs) console.log(shipArr[idx].name + ": Missing or invalid price parameter; reverting to credit cost: " + c.innerText);
p = c.innerText;
p = c.innerText;
}
}
}
}
else{
else{
console.log(shipArr[idx].name + ": Missing or invalid price parameter; reverting to materials cost: " + e.innerText);
if(enableConsoleLogs) console.log(shipArr[idx].name + ": Missing or invalid price parameter; reverting to materials cost: " + e.innerText);
p = e.innerText;
p = e.innerText;
}
}
Line 194: Line 197:
count++;
count++;
     if(count == shipArr.length){
     if(count == shipArr.length){
       console.log("SHIPARR ELEMENTS FILLED");
       if(enableConsoleLogs) console.log("SHIPARR ELEMENTS FILLED");
       cleanArr();
       cleanArr();
     }
     }
     else console.log(count+" of "+shipArr.length+"|"+shipArr[idx].name+" (Index "+idx+")");
     else if(enableConsoleLogs) console.log(count+" of "+shipArr.length+"|"+shipArr[idx].name+" (Index "+idx+")");
}
}
//sub-function called by cleanArr to remove values from shipArr
//sub-function called by cleanArr to remove values from shipArr
function remove(idx){
function remove(idx){
console.log("REMOVED ITEM AT INDEX "+idx+": "+shipArr[idx].name+"|"+shipArr[idx].price+"|"+shipArr[idx].type);
if(enableConsoleLogs) console.log("REMOVED ITEM AT INDEX "+idx+": "+shipArr[idx].name+"|"+shipArr[idx].price+"|"+shipArr[idx].type);
shipArr.splice(idx,1);
shipArr.splice(idx,1);
}
}
Line 210: Line 213:
         count = shipArr.length;
         count = shipArr.length;
     }
     }
console.log("STARTING DUPLICATE/INVALID FILTER");
if(enableConsoleLogs) console.log("STARTING DUPLICATE/INVALID FILTER");
var x = shipArr.length;
var x = shipArr.length;
for(j = shipArr.length-1; j >= 0; j--){
for(j = shipArr.length-1; j >= 0; j--){
Line 234: Line 237:
restartFill();
restartFill();
     }  //if array is empty after clean, remove invalid array and retry XML calls; only retry once to prevent infinite loops
     }  //if array is empty after clean, remove invalid array and retry XML calls; only retry once to prevent infinite loops
console.log("Number of removed items: "+(x-shipArr.length));
if(enableConsoleLogs) console.log("Number of removed items: "+(x-shipArr.length));
     console.log("Final array size: "+shipArr.length+"|"+count);
     if(enableConsoleLogs) console.log("Final array size: "+shipArr.length+"|"+count);
if(count < 400) getPriceType();
if(count < 400) getPriceType();
else if(count > 400){
else if(count > 400){
console.log("Cleaning up prices...");
if(enableConsoleLogs) console.log("Cleaning up prices...");
//clean/format price for sorting
//clean/format price for sorting
for(i = 0; i < shipArr.length; i++){
for(i = 0; i < shipArr.length; i++){
Line 256: Line 259:
if(canRestart && canRun){
if(canRestart && canRun){
canRestart = false;
canRestart = false;
console.log("Invalid array or force refresh");
if(enableConsoleLogs) console.log("Invalid array or force refresh");
shipArr = [];
shipArr = [];
localStorage.removeItem("storedArr");
localStorage.removeItem("storedArr");
Line 360: Line 363:
           if(tempShipArr.some(function(obj,idx){ return obj.type == tempShipArr[i].type && i != idx; })) document.getElementById("Nav"+tempShipArr[i].type).innerHTML +=" • ";
           if(tempShipArr.some(function(obj,idx){ return obj.type == tempShipArr[i].type && i != idx; })) document.getElementById("Nav"+tempShipArr[i].type).innerHTML +=" • ";
         }
         }
         else console.log("Ship not added: "+tempShipArr[i].name+". Invalid or missing category: "+tempShipArr[i].type);
         else if(enableConsoleLogs) console.log("Ship not added: "+tempShipArr[i].name+". Invalid or missing category: "+tempShipArr[i].type);
         tempShipArr.splice(i,1);
         tempShipArr.splice(i,1);
     }
     }
Line 368: Line 371:
canRun = true;
canRun = true;
canRestart = true;
canRestart = true;
     console.log("SHIPARR OUTPUT COMPLETE");
     if(enableConsoleLogs) console.log("SHIPARR OUTPUT COMPLETE");
}
}
//Add eventListeners to all <span> buttons in the array; update button innerHTML/text for different states of "flag"
//Add eventListeners to all <span> buttons in the array; update button innerHTML/text for different states of "flag"