Skip to content

Instantly share code, notes, and snippets.

@Garbee
Last active July 23, 2017 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garbee/5c1da3b351e12cc1e43f42b2a7c12f8c to your computer and use it in GitHub Desktop.
Save Garbee/5c1da3b351e12cc1e43f42b2a7c12f8c to your computer and use it in GitHub Desktop.
var getData = function(name) {
const selectors = new Map();
selectors.set('name', 'nameRow');
selectors.set('manaCost', 'manaRow');
selectors.set('convertedManaCost', 'cmcRow');
selectors.set('rulesText', 'textRow');
selectors.set('flavorText', 'flavorRow');
selectors.set('powerAndToughness', 'ptRow');
selectors.set('rarity', 'rarityRow');
selectors.set('artist', 'artistRow');
selectors.set('number', 'numberRow')
return function() {
const data = new Map();
const output = {};
let nameNodes = Array.from(document.querySelectorAll(`[id$="${selectors.get('name')}"] .value`));
let index = 0;
nameNodes.forEach((node, count) => {
if (node.textContent.trim() === name.trim()) {
index = count;
}
});
const dataContainer = document.querySelectorAll('.rightCol')[index];
let types = dataContainer.querySelector('[id$="typeRow"] .value').textContent.trim().split('—').map(text => text.trim());
output['type'] = types;
selectors.forEach((value, key) => {
const get = dataContainer.querySelector(`[id$="${value}"] .value`);
if (get === null) {
return;
}
if (key === 'manaCost') {
const textCost = Array.from(
get.querySelectorAll('img')
).map(node => node.alt.trim());
output[key] = textCost;
return;
}
if (key === 'powerAndToughness' &&
! types.includes('Planeswalker')) {
const PowTou = get.textContent.trim().split('/');
output['power'] = PowTou[0];
output['toughness'] = PowTou[1];
return;
}
if (key === 'powerAndToughness' &&
types.includes('Planeswalker')) {
output['loyalty'] = get.textContent.trim();
return;
}
if (key === 'rulesText') {
output[key] = [];
get.querySelectorAll('.cardtextbox').forEach(textBox => {
textBox.querySelectorAll('img').forEach(image => {
const text = image.alt;
image.outerHTML = `{${text}}`;
});
const pushText = textBox.textContent.trim();
if (pushText !== '') {
output[key].push(textBox.textContent.trim());
}
});
return;
}
output[key] = get.textContent.trim();
});
return output;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment