forked from philipmulcahy/azad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject.js
96 lines (88 loc) · 3.1 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
/* Copyright(c) 2016 Philip Mulcahy. */
/* global window */
/* jshint strict: true, esversion: 6 */
const amazon_order_history_inject = (function() {
'use strict';
const request_scheduler = amazon_order_history_request_scheduler.create();
function getYears() {
if(typeof(getYears.years) === 'undefined') {
console.log('getYears() needs to do something');
const snapshot = amazon_order_history_util.findMultipleNodeValues(
'//select[@name=\"orderFilter\"]/option[@value]',
document,
document);
getYears.years = snapshot.map( elem => {
return elem.textContent
.replace('en', '') // amazon.fr
.replace('nel', '') // amazon.it
.trim();
}).filter( (element, index, array) => {
return(/^\d+$/).test(element);
}).filter( year => (year >= '2004') );
}
console.log('getYears() returning ', getYears.years);
return getYears.years;
}
function fetchAndShowOrders(years) {
amazon_order_history_order.getOrdersByYear(
years,
request_scheduler,
getYears()[0]
).then(
orderPromises => {
let beautiful = true;
if (orderPromises.length >= 500) {
beautiful = false;
alert('500 or more orders found. That\'s a lot! We\'ll start you off with a plain table to make display faster. You can click the blue "datatable" button to restore sorting, filtering etc.');
}
amazon_order_history_table.displayOrders(orderPromises, beautiful);
return document.querySelector('[id=\"order_table\"]');
}
);
}
function addYearButtons() {
console.log('addYearButtons starting');
const years = getYears();
if(years.length > 0) {
amazon_order_history_util.addButton(
'All years',
() => {
fetchAndShowOrders(years);
}
);
} else {
console.log('addYearButtons no years found');
}
years.forEach( year => {
amazon_order_history_util.addButton(
[year],
() => {
fetchAndShowOrders([year]);
}
);
});
}
function addClearCacheButton() {
amazon_order_history_util.addButton(
'clear cache',
() => {
request_scheduler.clearCache();
}
);
}
function addInfoPoints() {
const progress = document.createElement('div');
progress.setAttribute('id', 'order_reporter_progress');
progress.setAttribute('class', 'order_reporter_progress');
progress.setAttribute(
'style', 'position:absolute; top:0; right:0; color:orange; padding:0.2em; font-size:75%');
document.body.insertBefore(
progress,
document.body.firstChild
);
}
console.log('Amazon Order History Reporter starting');
addYearButtons();
addClearCacheButton();
addInfoPoints();
})();