Created
May 7, 2017 15:05
-
-
Save anonymous/95aebe6166baede137884d9a939e852e to your computer and use it in GitHub Desktop.
Convert order history from a popular Swiss ticket shop to CSV
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
// Go to your favorite national trainline, access /ticketshop/b2c/dossierListe.do | |
// Run this code in your console to get a CSV formatted version of the history | |
// See how much you're spending on public transport! | |
(function () { | |
// JSON to CSV converter by Zach Hunter http://www.zachhunter.com/2011/06/json-to-csv/ | |
function ConvertToCSV(objArray) { | |
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray; | |
var str = ''; | |
for (var i = 0; i < array.length; i++) { | |
var line = ''; | |
for (var index in array[i]) { | |
if (line != '') line += ',' | |
line += array[i][index]; | |
} | |
str += line + '\r\n'; | |
} | |
return str; | |
} | |
myOrders = []; | |
jQuery('#orders').find('tr.even,tr.odd').each(function() { | |
var tds = jQuery(this).find('td'); | |
var info = jQuery(this).next().find('ul li').html(); | |
info = info.replace(/,/g,' '); | |
info = info.replace(/<br>/g,','); | |
myOrders.push({ | |
date: tds[1].innerText.trim(), | |
amount: parseFloat(tds[3].innerText), | |
info: info.trim() | |
}); | |
}); | |
window.prompt("Copy to clipboard: Ctrl+C, Enter", ConvertToCSV(myOrders)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment