Marvel's Spider Man
releases on September 26, 2025!

Preorder now on CardKingdom Preorder now on TcgPlayer

Marvel's Spider Man
releases on September 26, 2025!

Preorder now on CardKingdom Preorder now on TcgPlayer
32 total results       Previous Page 2 of 2
You must login or register to post a new reply
Posts [ 31 to 32 of 32 ]
Trade score 0 (100%)
Members
Registered: 18-Jan-2018 22:29
Posts: 2
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);[/i][/card][/card][/card][/1][/0-9][/2][/0-9][/1][/row][/row][/row][/row][/URGBWC][/URGBWC][/i][/1][/i][/i][/];

// Iterate through rows to get mana cost totals for each card
var x ]
Trade score 284 (99%)
Members
Registered: 10-Sep-2012 19:20
Posts: 402
20-May-2019 23:35 (Last edited: 20-May-2019 23:35)
32
LucasVon wrote: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);[/i][/card][/card][/card][/1][/0-9][/2][/0-9][/1][/row][/row][/row][/row][/URGBWC][/URGBWC][/i][/1][/i][/i][/];

// Iterate through rows to get mana cost totals for each card
var x ]

Awesome, thanks!
Posts [ 31 to 32 of 32 ]
32 total results       Previous Page 2 of 2
You must login or register to post a new reply