forked from philipmulcahy/azad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject.js
257 lines (240 loc) · 8.68 KB
/
inject.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* Copyright(c) 2016 Philip Mulcahy. */
/* global window */
/* global XPathResult */
/* jshint strict: true, esversion: 6 */
var amazon_order_history_inject = (function () {
"use strict";
function updateStatus(msg) {
document.getElementById("order_reporter_notification").textContent = msg;
}
class YearFetcher {
constructor(year) {
this.year = year;
this.expected_order_count = null;
this.order_found_callback = null;
this.query_string_templates = {
"www.amazon.de": "https://%(site)s/gp/css/order-history" +
"?opt=ab&digitalOrders=1" +
"&unifiedOrders=1" +
"&returnTo=" +
"&orderFilter=year-%(year)s" +
"&startIndex=%(startOrderPos)s" +
"&language=en_GB",
"www.amazon.co.uk": "https://%(site)s/gp/css/order-history" +
"?opt=ab&digitalOrders=1" +
"&unifiedOrders=1" +
"&returnTo=" +
"&orderFilter=year-%(year)s" +
"&startIndex=%(startOrderPos)s",
"smile.amazon.com": "https://%(site)s/gp/css/order-history" +
"?opt=ab&digitalOrders=1&" +
"&unifiedOrders=1" +
"&returnTo=" +
"&orderFilter=year-%(year)s" +
"&startIndex=%(startOrderPos)s",
"www.amazon.com": "https://%(site)s/gp/your-account/order-history" +
"?ie=UTF8" +
"&orderFilter=year-%(year)s" +
"&startIndex=%(startOrderPos)s" +
"&unifiedOrders=0"
};
/* Promise to array of Order Promise. */
this.orders = new Promise(
function (resolve, reject) {
var orders = [];
this.order_found_callback = function (order) {
orders.push(order);
order.then(
function (order) {
updateStatus("Fetching " + order.id);
}
);
if (orders.length === this.expected_order_count) {
resolve(orders);
}
};
this.sendGetOrderCount();
}.bind(this)
);
}
generateQueryString(startOrderPos) {
var template = this.query_string_templates[amazon_order_history_util.getSite()];
return sprintf(
template, {
site: amazon_order_history_util.getSite(),
year: this.year,
startOrderPos: startOrderPos
}
);
}
sendGetOrderCount() {
var req = new XMLHttpRequest();
var query = this.generateQueryString(0);
req.open("GET", query, true);
req.onload = this.receiveGetOrderCount.bind(this);
req.send();
}
receiveGetOrderCount(evt) {
var iorder;
var req;
var query;
var p = new DOMParser();
var d = p.parseFromString(evt.target.responseText, "text/html");
var countSpan = d.evaluate(
".//span[@class=\"num-orders\"]",
d,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE).singleNodeValue;
this.expected_order_count = parseInt(
countSpan.textContent.split(" ")[0], 10);
updateStatus(
"Found " + this.expected_order_count + " orders for " + this.year
);
this.unfetched_count = this.expected_order_count;
if (isNaN(this.unfetched_count)) {
updateStatus(
"Error: cannot find order count in " + countSpan.textContent
);
this.unfetched_count = 0;
}
// Request second and subsequent pages.
for (iorder = 10; iorder < this.expected_order_count; iorder += 10) {
req = new XMLHttpRequest();
query = this.generateQueryString(iorder);
req.open("GET", query, true);
req.onload = this.receiveOrdersPage.bind(this);
req.send();
}
// We already have the first page.
this.receiveOrdersPage(evt);
}
receiveOrdersPage(evt) {
var p = new DOMParser();
var d = p.parseFromString(evt.target.responseText, "text/html");
var orders;
var ordersElem;
var elem;
var i;
try {
ordersElem = d.getElementById("ordersContainer");
orders = d.evaluate(
".//*[contains(concat(\" \", " +
"normalize-space(@class), " +
"\" \"), " +
"\" order \")]",
ordersElem,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
);
} catch (err) {
updateStatus(
"Error: maybe you\"re not logged into " +
"https://" + amazon_order_history_util.getSite() + "/gp/css/order-history " +
err
);
return;
}
function makeOrderPromise(elem) {
return new Promise(
function (resolve, reject) {
resolve(amazon_order_history_order.create(elem));
}
);
}
for (i = 0; i !== orders.snapshotLength; i += 1) {
elem = orders.snapshotItem(i);
this.order_found_callback(makeOrderPromise(elem));
}
}
}
function getTextArrayFromXPathSnapshotResult(snapshots, func) {
func = defaultFor(
func,
function (elem) {
return elem.textContent.trim();
}
);
var results = [];
var i;
var elem;
for (i = 0; i !== snapshots.snapshotLength; i += 1) {
elem = snapshots.snapshotItem(i);
results[results.length] = func(elem);
}
return results;
}
function getYears() {
if (typeof (getYears.years) === "undefined") {
var yearElems = document.evaluate(
"//select[@name=\"orderFilter\"]/option[@value]",
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
);
getYears.years = getTextArrayFromXPathSnapshotResult(yearElems).filter(
function (element, index, array) {
return (/^\d+$/).test(element);
}
);
}
return getYears.years;
}
/* Returns array of Promise to array of Order Promises. */
function getOrdersByYear(years) {
// At return time we may not know how many orders there are, only
// how many years in which orders have been queried for.
return years.map(
function (year) {
return new YearFetcher(year).orders;
}
);
}
function fetchAndShowOrders(years) {
var orders = getOrdersByYear(years);
Promise.all(orders).then(
function (arrayOfArrayOfOrderPromise) {
var orderPromises = [].concat.apply(
[],
arrayOfArrayOfOrderPromise
);
amazon_order_history_table.displayOrders(orderPromises, true);
}
);
}
function addYearButtons() {
var notification = document.createElement("span");
notification.setAttribute("id", "order_reporter_notification");
document.body.insertBefore(
notification,
document.body.firstChild
);
var years = getYears();
if (years.length > 0) {
amazon_order_history_util.addButton(
"All years",
function () {
fetchAndShowOrders(years);
}
);
}
years.forEach(
function (year) {
amazon_order_history_util.addButton(
[year],
function () {
fetchAndShowOrders([year]);
}
);
}
);
}
function defaultFor(arg, val) {
return arg === undefined ? val : arg;
}
addYearButtons();
return {
addYearButtons: addYearButtons
};
})();