-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
140 lines (118 loc) · 3.87 KB
/
popup.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
async function getTransactions(date) {
const url = `https://www.barclaycardus.com/servicing/jserv/transaction/getPostedTransactions?cycleDate=${date || ''}&_=${Date.now()}`;
const options = {
headers: {
accept: 'application/json, text/javascript',
'accept-language': 'en',
'cache-control': 'no-cache',
'content-type': 'application/json',
'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'x-requested-with': 'XMLHttpRequest',
},
referrer: 'https://www.barclaycardus.com/servicing/activity',
referrerPolicy: 'strict-origin-when-cross-origin',
body: null,
method: 'GET',
mode: 'cors',
credentials: 'include',
};
const res = await fetch(url, options);
const data = await res.json();
if (data?.redirectURL?.includes('/authenticate')) {
throw new Error('You must login first');
}
return data;
}
function setButtonState(isDownloading) {
if (isDownloading) {
download.setAttribute('disabled', true);
download.innerHTML = '<img src="three-dots.svg" width="100%" />';
} else {
download.removeAttribute('disabled');
download.innerHTML = 'Download';
}
}
function setFeedbackState(html = '', className = '') {
feedback.className = className;
feedback.innerHTML = html;
}
function saveAs(blob, filename) {
const url = window.URL.createObjectURL(blob);
const _element = document.createElement('a');
_element.href = url;
_element.setAttribute('download', filename);
_element.click();
}
function generateCsv(items, filename) {
const csv = Papa.unparse(items);
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
saveAs(blob, filename);
}
async function getCurrentTab() {
let queryOptions = { active: true, currentWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
function getStatementDateElementValue() {
return document.querySelector('#tp_cycles')?.value;
}
async function getStatementDate() {
const tab = await getCurrentTab();
const data = await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: getStatementDateElementValue,
});
return data[0]?.result;
}
function getDateString(date) {
if (!date) return getDateString(new Date().toISOString());
return date.split('T')[0];
}
const isBarclaysWebsite = string => /^https:\/\/\w+\.barclaycardus.com/.test(string);
// handlers
async function handleClick(event) {
event.preventDefault();
setFeedbackState();
setButtonState(true);
try {
const date = await getStatementDate();
const data = await getTransactions(date);
const transactions = data.transactions.map(item => {
const {
amount: { fAmount: amount },
merchantCategoryType,
merchantLocation,
} = item;
return {
...item,
amount: item.type === 'PURCHASE' ? amount : -amount,
merchantCategoryType: `${merchantCategoryType.value} - ${merchantCategoryType.description}`,
merchantLocation: `${merchantLocation.city} ${merchantLocation.state} ${merchantLocation.zipCode}`,
};
});
const { statementBeginDate, statementDate } = transactions[0];
const filename = `Barclays ${getDateString(statementBeginDate)} - ${getDateString(statementDate)}.csv`;
generateCsv(transactions, filename);
setFeedbackState('Statement download successfully', 'success');
} catch (err) {
setFeedbackState(err.message, 'error');
}
setButtonState();
}
async function onLoadHandler() {
const tab = await getCurrentTab();
if (!tab || !isBarclaysWebsite(tab.url)) {
const html = 'You must open <a class="underline" target="_blank" href="https://www.barclaycardus.com">https://www.barclaycardus.com</a>';
setFeedbackState(html, 'error');
return;
}
download.removeAttribute('disabled');
}
// events
download.addEventListener('click', handleClick);
window.onload = onLoadHandler;