-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapayin.js
236 lines (210 loc) · 10.6 KB
/
apayin.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
/**
* @file apayin.js
* @description A utility script to extract and export payment transaction details
* from the Amazon India Payment History page as an HTML file.
*
* @version 1.0.0
* @date 2024-10-24
* @author Nikhil - @dishapatel010
*
* @usage
* 1. Ensure the Amazon Pay India History Exporter extension is installed in your browser.
* 2. Navigate to the Amazon India Payment History page:
* https://www.amazon.in/pay/history
* 3. Scroll down to load all transactions until the last transaction you want to export is visible.
* 4. Click on the "Export Payment History" button provided by the extension.
* 5. The script will automatically extract the payment history and prompt you to download it as an HTML file.
*
* @note
* Ensure that you are logged into your Amazon account to access the payment history.
* This script is intended for personal use only.
*
* @changelog
* - v1.0.0 - Initial release.
* - Grouping Transactions by Month
* - Extension Support by @AgarwalKritik
* - v1.1.0 - Months, Extension support
*/
// Function to create an export button on the page
function createExportButton() {
const button = document.createElement('button');
button.innerText = 'Export Payment History';
button.style.position = 'fixed'; // Fixed position to stay in view while scrolling
button.style.bottom = '20px';
button.style.right = '20px';
button.style.padding = '10px';
button.style.backgroundColor = '#232f3e'; // Amazon Gray
button.style.color = '#ffffff'; // White text color
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
button.style.zIndex = 1000; // Ensure it appears above other content
document.body.appendChild(button);
button.addEventListener('click', exportPaymentDetailsAsHTML);
}
// Function to extract and export payment transaction details as an HTML file
function exportPaymentDetailsAsHTML() {
const transactionElements = document.querySelectorAll('.a-expander-container');
if (transactionElements.length === 0) {
console.log("No transactions found.");
return;
}
// Create an HTML structure for the exported data
let htmlContent = `
<html>
<head>
<title>Payment Transaction Details</title>
<style>
body { font-family: Arial, sans-serif; }
table { width: 100%; border-collapse: collapse; margin: 20px 0; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
tr:hover { background-color: #f1f1f1; } /* Hover effect */
.received { background-color: #d4edda; } /* Light green for "Received from" */
.paid { background-color: #f8d7da; } /* Light reddish-brown for Paid to */
.gift-paid { background-color: #ffcccb; } /* Light red-yellow mix for Paid to (Gift Cards) */
.gift-received { background-color: #d4f0c2; } /* Light green-yellow mix for Received from (Gift Cards) */
.white-row { background-color: #ffffff; } /* White background for N/A transactions */
.strikethrough { text-decoration: line-through; } /* Strike-through style */
h2 { margin-top: 20px; } /* Month title style */
@media screen and (max-width: 600px) {
table, thead, tbody, th, td, tr { display: block; }
th { position: absolute; left: -9999px; }
td { border: none; position: relative; padding-left: 50%; }
td:before { content: attr(data-label); position: absolute; left: 15px; font-weight: bold; }
}
footer {
margin-top: 20px;
padding: 10px;
text-align: center;
font-size: 12px;
color: #666;
border-top: 1px solid #ddd;
}
</style>
</head>
<body>
<h1>Payment Transaction Details</h1>`;
// Object to hold transactions by month
const transactionsByMonth = {};
// Loop through each transaction element
transactionElements.forEach(transaction => {
const dateTimeElement = transaction.querySelector('.payment-details-desktop + .a-size-base');
if (!dateTimeElement) return; // Skip if there's no date
const dateTimeText = dateTimeElement.innerText;
const date = new Date(dateTimeText);
const monthYear = date.toLocaleString('default', { month: 'long', year: 'numeric' });
// Initialize array for this month if it doesn't exist
if (!transactionsByMonth[monthYear]) {
transactionsByMonth[monthYear] = [];
}
// Collect transaction details
const paidToElement = transaction.querySelector('.pad-header-text .a-size-medium');
const paymentMethodElement = transaction.querySelector('.payment-details-desktop .a-size-base.a-color-tertiary');
// Capture both normal and strikethrough amounts
const normalAmountElement = transaction.querySelector('.a-size-medium.a-color-price, .a-size-medium.a-color-attainable');
const struckThroughAmountElement = transaction.querySelector('.a-size-medium.a-color-tertiary.a-text-strike, .a-size-medium.a-color-attainable');
let paidTo = paidToElement ? paidToElement.innerText : 'N/A';
let amount = normalAmountElement ? normalAmountElement.innerText : 'N/A';
let isStruckThrough = struckThroughAmountElement ? struckThroughAmountElement.innerText : null;
// If the amount is received and it has a '+' sign, do not strike through
if (isStruckThrough) {
if (isStruckThrough.includes('+')) {
amount = isStruckThrough; // Use the received amount directly
} else {
amount = `<span class="strikethrough">${isStruckThrough}</span>`; // Strikethrough if applicable
}
} else {
amount = amount !== 'N/A' ? amount : 'N/A'; // Default to N/A if nothing is found
}
// Determine transaction type based on the amount's sign
let transactionType;
if (amount.startsWith('-')) {
transactionType = "Paid to"; // Negative amount means a payment
} else if (amount.startsWith('+')) {
transactionType = "Received from"; // Positive amount means received
} else {
transactionType = "N/A"; // If neither sign is present
}
// UPI ID, Paid Using, Reference ID
const miniDetails = transaction.querySelector('.miniDetailsPage-desktop');
const upiIdElement = miniDetails ? miniDetails.querySelector('.pad-mini-details-text .a-size-base.a-color-base:nth-of-type(1)') : null;
const paidUsingElement = miniDetails ? miniDetails.querySelector('.pad-mini-details-text:nth-of-type(2) .a-size-base.a-color-base') : null;
// Find the Reference ID by checking for both "Bank Reference ID" and "UPI Reference ID"
let referenceId = 'N/A';
const referenceLabels = miniDetails ? miniDetails.querySelectorAll('.a-size-base.a-color-tertiary') : [];
referenceLabels.forEach(label => {
let labelText = label.innerText.trim();
if (labelText === "Bank Reference ID:" || labelText === "UPI Reference ID:") {
const refIdElement = label.closest('.a-row.pad-mini-details-text').querySelector('.a-size-base.a-color-base');
if (refIdElement) {
referenceId = refIdElement.innerText.trim();
}
}
});
let upiId = upiIdElement ? upiIdElement.innerText : 'N/A';
let paidUsing = paidUsingElement ? paidUsingElement.innerText : 'N/A';
// Determine row class based on transaction type and UPI ID
let rowClass = "";
if (transactionType === "N/A") {
rowClass = "white-row"; // White background for N/A transactions
} else if (upiId.includes("Gift Cards")) {
if (transactionType === "Paid to") {
rowClass = "gift-paid"; // Light red-yellow mix for "Paid to Gift Cards"
} else if (transactionType === "Received from") {
rowClass = "gift-received"; // Light green-yellow mix for "Received from Gift Cards"
}
} else {
// Default background colors if not Gift Cards
rowClass = transactionType === "Received from" ? "received" : "paid";
}
// Append transaction to the month array
transactionsByMonth[monthYear].push(`
<tr class="${rowClass}">
<td>${transactionType}</td>
<td>${paidTo}</td>
<td>${paymentMethodElement ? paymentMethodElement.innerText : 'N/A'}</td>
<td>${dateTimeText}</td>
<td>${amount}</td>
<td>${upiId}</td>
<td>${paidUsing}</td>
<td>${referenceId}</td>
</tr>`);
});
// Append transactions for each month to the HTML content
for (const month in transactionsByMonth) {
htmlContent += `<h2>${month}</h2><table><tr>
<th>Transaction Type</th>
<th>Paid To / Received From</th>
<th>Payment Method</th>
<th>Date & Time</th>
<th>Amount</th>
<th>Payee UPI ID</th>
<th>Paid Using</th>
<th>Reference ID</th>
</tr>`;
transactionsByMonth[month].forEach(transactionRow => {
htmlContent += transactionRow;
});
htmlContent += `</table>`;
}
// Add footer to the HTML content
htmlContent += `
<footer>
<p>Exported on: ${new Date().toLocaleString()}</p>
<p>AmazonPayHistoryIN - <a href="https://github.com/dishapatel010/AmazonPayHistoryIN" target="_blank">@dishapatel010</a></p>
</footer>
</body>
</html>`;
// Create a Blob from the HTML content and trigger a download
const blob = new Blob([htmlContent], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", "payment_details.html");
document.body.appendChild(link); // Required for FF
link.click(); // This will download the file
URL.revokeObjectURL(url); // Release the URL object
}
// Initialize the export button when the script loads
createExportButton();