-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseAmazonEmail.js
118 lines (96 loc) · 2.74 KB
/
parseAmazonEmail.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
/**
* Parse Amazon Email
*/
function parseAmazonEmail(passedEmailHtml) {
"use strict";
var emailHtml = null,
receipt = {
orderNumber : null,
orderDate : null,
products : [],
shippingInfo : null
},
receiptLanguage = 'en';
/**
* Public method - parses entire email or just html segment
*
* @return reciept
*
*/
function parse() {
// Retrieve only HTML from email
setEmailHtml();
// Determine receipt language
setLanguage();
// Parse basic order information
parseOrderInfo();
// Parse shipping info
parseShippingInfo();
// Parse product info
parseProductInfo();
// Return receipt object
return receipt;
};
/**
* Set product information
*/
function parseProductInfo() {
var productsContainer,
currentTr = $('h3:contains("Order Details")').closest('tr').next();
while ($(currentTr).next().text().indexOf('Item Subtotal') == -1 && $(currentTr).next().text().indexOf('Shipping & Handling') == -1) {
var thisObject = {};
currentTr = $(currentTr).next(),
thisObject.name = $(currentTr).find("a:not(:has('img'))").html();
thisObject.id = $(currentTr).find("a").attr("title");
thisObject.image = $(currentTr).find("img").attr("src");
thisObject.price = $(currentTr).find('strong:contains("$")').text().replace('$','');
receipt.products.push(thisObject);
}
}
/**
* Set basic order information
*/
function parseOrderInfo() {
// Order number
if ($(emailHtml).find('h2:contains("Order Confirmation")').next("a").length) {
receipt.orderNumber = $(emailHtml).find('h2:contains("Order Confirmation")').next("a").text();
}
// Order Date
if ($(emailHtml).find('td:contains("Order #") span:contains("Placed on")').length) {
var dateString = $(emailHtml).find('td:contains("Order #") span:contains("Placed on")').text();
dateString = dateString.split("Placed on ")[1];
receipt.orderDate = new Date(dateString);
}
}
/**
* Set shipping information
*/
function parseShippingInfo() {
var fullShippingString,
shippingSegments,
i = 0;
if ($(emailHtml).find('p:contains("Your order will be sent to:")').length) {
fullShippingString = $(emailHtml).find('p:contains("Your order will be sent to:")').text();
fullShippingString = fullShippingString.split("Your order will be sent to:")[1];
receipt.shippingInfo = fullShippingString.split(" ");
for ( ; i < receipt.shippingInfo.length; i++) {
if (receipt.shippingInfo[i] === '') {
delete receipt.shippingInfo[i];
}
}
}
}
/**
* Get HTML content from passed email content string, put into jQuery object
*/
function setEmailHtml() {
emailHtml = $(passedEmailHtml);
}
/**
* Set language of email
*/
function setLanguage() {
receiptLanguage = 'en';
}
return parse();
};