1

(31 replies, posted in Site Discussion)

Hello, I edited the script a bit more to place the average CMC under the mana curve chart and make it look more like it's part of the website. Also commented out the console log and did some general tidying up of the formatting.

// ==UserScript==
// @name        deckboxCMC
// @match       https://deckbox.org/sets/*
// @description Calculates CMC in deckbox sets
// @version     0.6.1
// ==/UserScript==

// Function to append a new DOM element after another
function insertAfter(newNode, referenceNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

// Initialize array and hash to hold mana costs
var manaCosts = {};
var totalCosts = [];

// Iterate through rows to get mana cost totals for each card
var x = document.getElementsByClassName("mtg_mana");
//console.log("mtg_mana element count: " + x.length);


for (i = 0; i < x.length; i++) {
    var re = /mtg_mana_[0-9URGBWCX]{1,2}/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);
    //console.log("Card element looks like this: " + document.getElementById(row).innerHTML);
    var cardName = document.getElementById(row).innerHTML.match(/(?:a class="simple" href="https:\/\/deckbox\.org\/mtg\/.*" target="_blank">)(.*)(?:<\/a>)/)[1];
    //console.log("Card name is " + cardName);
    var stripped = x[i].outerHTML.match(re).toString();
    //console.log("This its stripped mana name: " + stripped);
    var strippedMore = stripped.replace("mtg_mana_", "");
    //console.log("Mana cost indicated: " + strippedMore);
    var converted = strippedMore.replace(/[URGBWC]([URGBWC])?/, "1");
    converted = converted.replace(/X/, "0");

    if (!manaCosts[row]) {
        //console.log("Adding new item to manaCosts hash for card " + cardName);
        //console.log("Found mana cost " + converted + " for card " + cardName);
        manaCosts[row] = Number(converted);
    } else {
        manaCosts[row] += Number(converted);
        //console.log("Found mana cost " + converted + " for card " + cardName);
    }
    //console.log("Total mana cost for " + cardName + " is " + manaCosts[row]);
}

// Summary of card costs by table row
//console.log("Here's what we found in the main deck: ");
for (var card in manaCosts) {
    // use hasOwnProperty to filter out keys from the Object.prototype
    if (manaCosts.hasOwnProperty(card)) {
        cardName = document.getElementById(card).innerHTML.match(/(?:a class="simple" href="https:\/\/deckbox\.org\/mtg\/.*" target="_blank">)(.*)(?:<\/a>)/)[1];
        if (document.getElementById(card).innerHTML.indexOf("wishlist") !== -1) {
            //console.log("Deck list has a wishlist.  I think this is YOUR deck.");
            var cardCount = document.getElementById(card).innerHTML.match(/(?:<td class="card_count deck_count.*>(\n)?)([0-9]{1,2})(?:(\n)?<\/td>\n<script>)/);
            cardCount = Number(cardCount[2]);
            //console.log("Raw cardCount is " + cardCount);
            //console.log("I think there are " + cardCount + " copies of " + cardName);
        } else {
            //console.log("Deck list DOESN'T have a wishlist.  I think this is someone elses deck.");
            cardCount = document.getElementById(card).innerHTML.match(/(?:<td class="card_count">)([0-9]*)(?:<\/td>\n<td class="card_name">)/)[1];
            //console.log("I think there are " + cardCount + " copies of " + cardName);
        }
        if (document.getElementById(card).innerHTML.indexOf("main") !== -1) {
            //console.log(cardCount + " copies of " + cardName + " (" + manaCosts[card] + ")");
            for (var i = 0; i < cardCount; i++) {
                //console.log("Adding mana cost " + manaCosts[card] + " for card " + cardName + " to the total.");
                totalCosts.push(manaCosts[card]);
            }
        } else {
            //console.log("Card " + cardName + " with value of " + card + " is not in main deck.  Will NOT use to calculate CMC average.");
        }
    }
}

// Get average
var total = 0;
//console.log("Found " + totalCosts.length + " cards in the main deck.");
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);

// Create element with average CMC
var ElCMC = document.createElement('div');
ElCMC.id = 'avg_cmc';
ElCMC.style.textAlign = 'center';
ElCMC.style.fontSize = '10px';
ElCMC.style.color = '#484848';
ElCMC.textContent = "Average CMC: " + avg.toFixed(2);

// Insert the element after the mana curve chart
var ref = document.querySelector('#chart_curve');
insertAfter(ElCMC, ref);

2

(23 replies, posted in Site Discussion)

Just gonna pop my support for this in here. Trying to make the switch from Delver Lens to Deckbox to be able to manage my collection on a browser and I'm really missing the ability to create lists for cards in different boxes, or really, just any tagging system at all that lets me track where a specific card is.