-
Notifications
You must be signed in to change notification settings - Fork 1
/
puca-export.js
99 lines (75 loc) · 2.54 KB
/
puca-export.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(function ($) {
alert('Ready to export.\n'
+ 'This includes loading all your haves which may take some time.\n'
+ 'A download button will appear in the top left corenr of the screen when done.');
var conditionMap = {
'NM/M': 'Near Mint',
'SP': 'Slightly Played',
'MP': 'Moderately Played',
'HP': 'Heavily Played'
};
var tradableMap = {
true: 'HAVE',
false: 'NOT FOR TRADE',
};
var quote = function (s) {
return '"' + s + '"';
};
var cards = [];
var collectCards = function () {
var $cards = $('.item.clear');
for (var i = 0; i < $cards.length; i++) {
var $card = $($cards[i]);
var card = {
cardName: $card.find('a[data-card_id]').text(),
setName: $card.find('.set select option:selected').text(),
isTradable: tradableMap[$card.find('.trade .btn-tab').hasClass('active-button-2')],
isFoil: $card.find('.foil select option:selected').val(),
language: $card.find('.language select option:selected').text(),
condition: conditionMap[$card.find('.condition select option:selected').text()]
};
if (card.cardName && card.setName) {
cards.push(card);
}
}
$cards.remove();
};
var doExport = function () {
console.log('exporting');
var groups = _.groupBy(cards, function (c) { return JSON.stringify(c); });
var csv = [['Count', 'Name', 'Expansion', 'Condition', 'Language', 'Foil', 'PucaID', 'Status'].join(',') + '\n'];
for (var key in groups) {
var group = groups[key];
var card = group[0];
var quantity = group.length;
group.length = 0; // a bit of cleanup
var row = [quantity, quote(card.cardName), quote(card.setName), card.condition, card.language, card.isFoil, '', card.isTradable];
csv.push(row.join(',') + '\n');
}
var blob = new Blob(csv, { type: 'text/csv' });
$('<a/>', {
href: URL.createObjectURL(blob),
class: 'btn',
download: 'Pucatrade Haves ' + (new Date()) + '.csv'
}).css({
position: 'fixed',
left: '20px',
top: '20px',
'z-index': 100
}).text('Download .CSV').appendTo($('body'));
};
var loadOrExport = function () {
collectCards();
var $more = $("a:contains('LOAD MORE')");
if ($more.length == 1) {
// no more load button
doExport();
return;
}
console.log('expanding page');
var evt = new Event("click", { "bubbles": true, "cancelable": true });
$more[0].dispatchEvent(evt);
setTimeout(loadOrExport, 2000);
};
loadOrExport();
})($);