Created
April 12, 2015 02:06
-
-
Save joncasey/5fee9c88f08c78a771aa to your computer and use it in GitHub Desktop.
Convert Games with Gold list to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://en.wikipedia.org/wiki/List_of_Games_with_Gold_games | |
// http://www.tekrevue.com/complete-xbox-games-with-gold-list-details/ | |
var tables = document.querySelectorAll('table.wikitable') | |
var data = { | |
xbox360: tabular(tables[0]), | |
xboxOne: tabular(tables[1]) | |
} | |
var prettyData = JSON.stringify(data, 0, 2) | |
save(prettyData) | |
function tabular(table) { | |
var data = { cols: {}, rows: [] } | |
var each = Array.prototype.forEach | |
var keys = {} | |
each.call(table.tHead.rows[0].cells, function (cell, cellIndex) { | |
var key = cell.textContent | |
if (key) { | |
keys[key] = cellIndex | |
} | |
}) | |
each.call(table.tBodies[0].rows, function (row, rowIndex) { | |
var raw = [] | |
each.call(row.cells, function (cell, cellIndex) { | |
var clone = cell.cloneNode(true) | |
each.call(clone.querySelectorAll('.sortkey'), function (el) { | |
el.parentNode.removeChild(el) | |
}) | |
raw[cellIndex] = clone.textContent | |
}) | |
data.rows.push(raw) | |
}) | |
data.cols = keys | |
return data | |
} | |
function save(data, filename) { | |
var blob = new Blob([data], {type:'text/json'}) | |
var e = document.createEvent('MouseEvents') | |
var a = document.createElement('a') | |
a.download = filename || 'console.json' | |
a.href = window.URL.createObjectURL(blob) | |
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':') | |
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null) | |
a.dispatchEvent(e) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment