forked from philipmulcahy/azad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
112 lines (103 loc) · 3.19 KB
/
util.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
/* Copyright(c) 2016 Philip Mulcahy. */
/* jshint strict: true, esversion: 6 */
/* global XPathResult */
const amazon_order_history_util = (function(){
"use strict";
function getSite() {
var href = window.location.href;
var stem = /https:\/\/((www|smile)\.amazon\.[^\/]+)/.exec(href)[1];
return stem;
}
function getOrderDetailUrl(orderId) {
return "https://" + getSite() + "/gp/your-account/order-details/" +
"ref=oh_aui_or_o01_?ie=UTF8&orderID=" + orderId;
}
function getOrderPaymentUrl(orderId) {
return orderId.startsWith("D") ?
"https://" + getSite() + "/gp/digital/your-account/order-summary.html" +
"?ie=UTF8&orderID=" + orderId + "&print=1&" :
"https://" + getSite() + "/gp/css/summary/print.html" +
"/ref=oh_aui_ajax_pi?ie=UTF8&orderID=" + orderId;
}
function addButton(name, cb, style) {
var existing = document.querySelector('[button_name="' + name + '"]');
if ( existing !== null ) {
existing.parentNode.removeChild(existing);
}
var a = document.createElement("button");
if(typeof(style) === "undefined") {
style = "background-color:orange; color:white";
}
a.innerText = name;
a.setAttribute("style", style);
a.setAttribute("class", "order_reporter_button");
a.setAttribute("button_name", name);
a.onclick = cb;
document.body.insertBefore(
a,
document.body.firstChild
);
}
function removeButton(name) {
var elem = document.querySelector('[button_name="' + name + '"]');
if ( elem !== null ) {
elem.parentNode.removeChild(elem);
}
}
function findSingleNodeValue(xpath, doc, elem) {
return doc.evaluate(
xpath,
elem,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
}
function findMultipleNodeValues(xpath, doc, elem) {
var snapshot;
try {
snapshot = doc.evaluate(
xpath,
elem,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
);
} catch(err) {
log(
"Error: maybe you\"re not logged into " +
"https://" + getSite() + "/gp/css/order-history " +
err
);
return [];
}
var values = [];
var i;
for(i = 0; i !== snapshot.snapshotLength; i += 1) {
values.push(snapshot.snapshotItem(i));
}
return values;
}
function clearBody() {
Array.from(document.body.children).forEach(
function(elem) {
if( !(
elem.hasAttribute("class") &&
elem.getAttribute("class").includes("order_reporter_")
)) {
document.body.removeChild(elem);
}
}
);
}
return {
addButton: addButton,
clearBody: clearBody,
findMultipleNodeValues: findMultipleNodeValues,
findSingleNodeValue: findSingleNodeValue,
getOrderDetailUrl: getOrderDetailUrl,
getOrderPaymentUrl: getOrderPaymentUrl,
getSite: getSite,
removeButton: removeButton
};
})();