// ==UserScript== // @name deckboxCMC // @match https://deckbox.org/sets/* // @description Calculates CMC in deckbox sets // @version 0.3 // ==/UserScript== var manaCosts = {}; var totalCosts = []; var x = document.getElementsByClassName("mtg_mana"); var i; for (i = 0; i < x.length; i++) { var re = /mtg_mana_[0-9URGBWX]/i; // console.log("This is a mana cost item: " + x[i].outerHTML); var row = x[i].parentElement.parentElement.id; // console.log("Parent element is " + row); var stripped = x[i].outerHTML.match(re).toString(); // console.log("This its stripped mana name: " + stripped); var strippedMore = stripped.slice(-1); //console.log("Mana cost indicated: " + strippedMore); var converted = strippedMore.replace(/[URGBW]/, "1"); var converted = converted.replace(/X/, "0"); if (!manaCosts[row]) { // console.log("Adding new item to manaCosts hash for row " + row); // console.log("Found mana cost " + converted + " for row " + row); manaCosts[row] = Number(converted); } else { manaCosts[row] += Number(converted); // console.log("Found mana cost " + converted + " for row " + row); } console.log("Total mana cost for " + row + " is " + manaCosts[row]); } // Summary of card costs by table row console.log("Here's a hash of mana costs: "); for (var card in manaCosts) { // use hasOwnProperty to filter out keys from the Object.prototype if (manaCosts.hasOwnProperty(card)) { if (card.indexOf("main") !== -1) { console.log("Card " + card + " with cost " + manaCosts[card] + " is in main deck. Will use to calculate CMC average."); totalCosts.push(manaCosts[card]); } else { console.log("Card " + card + " with cost " + manaCosts[card] + " is not in main deck. Will NOT use to calculate CMC average."); } } } var total = 0; console.log("Found " + totalCosts.length + " costs to work with"); for(var i = 0; i < totalCosts.length; i++) { // console.log("Adding mana cost number " + i + " to the total."); total += totalCosts[i]; // console.log("Total is now: " + total); } console.log("Dividing total mana cost of " + total + " by " + totalCosts.length + " total cards to find the average."); var avg = total / totalCosts.length; console.log("Agerage CMC: " + avg); var box = document.createElement( 'div' ); box.id = 'myAlertBox'; box.style.background = 'white'; box.style.border = '2px solid red'; box.style.padding = '4px'; box.style.position = 'absolute'; box.style.top = '8px'; box.style.left = '8px'; box.style.maxWidth = '400px'; box.textContent = "Converted mana cost: " + avg; document.body.appendChild( box );