Skip to content

Instantly share code, notes, and snippets.

Created May 7, 2017 15:05
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 anonymous/95aebe6166baede137884d9a939e852e to your computer and use it in GitHub Desktop.
Save anonymous/95aebe6166baede137884d9a939e852e to your computer and use it in GitHub Desktop.
Convert order history from a popular Swiss ticket shop to CSV
// 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