-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
executable file
·109 lines (96 loc) · 3.38 KB
/
background.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
let categoryMap = {};
//TODO: this is a race condition with the transaction filter below.
browser.webRequest.onBeforeRequest.addListener(
listener, {
urls: ["https://home.personalcapital.com/api/transaction/getUserTransactions"],
types: ["xmlhttprequest"]
}, ["blocking"]
);
browser.webRequest.onBeforeRequest.addListener(
(details) => {
let filter = browser.webRequest.filterResponseData(details.requestId);
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();
let fulltext = "";
filter.onstart = () => {
fulltext = "";
}
filter.ondata = event => {
let str = decoder.decode(event.data, {
stream: true
});
//Append json data until received full request
fulltext += str;
}
filter.onstop = () => {
let categorydata = JSON.parse(fulltext);
categorydata.spData.forEach(category => {
categoryMap[category.transactionCategoryId] = category.type;
});
filter.write(encoder.encode(fulltext));
filter.disconnect();
}
}, {
urls: ["https://home.personalcapital.com/api/transactioncategory/getCategories"],
types: ["xmlhttprequest"],
}, ["blocking"]
);
function listener(details) {
let filter = browser.webRequest.filterResponseData(details.requestId);
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();
let fulltext = "";
filter.onstart = () => {
fulltext = "";
}
filter.ondata = event => {
let str = decoder.decode(event.data, {
stream: true
});
//Append json data until received full request
fulltext += str;
}
filter.onstop = () => {
let transactiondata = JSON.parse(fulltext);
rewriteTransactions(transactiondata);
let rewritten = JSON.stringify(transactiondata);
filter.write(encoder.encode(rewritten));
filter.disconnect();
}
return {};
}
function rewriteTransactions(pcdata) {
let initialCount = pcdata.spData.transactions.length;
let finaldata = [];
let seen = {};
pcdata.spData.transactions.forEach(trans => {
//PC sends back duplicate transactions...
if (seen.hasOwnProperty(trans.userTransactionId)) {
return;
}
seen[trans.userTransactionId] = true;
switch (categoryMap[trans.categoryId]) {
case 'INCOME':
if (!trans.isCredit) {
trans.amount *= -1;
trans.isCredit = true;
trans.transactionType = 'Credit';
}
break;
case 'EXPENSE':
if (trans.isCredit) {
trans.amount *= -1;
trans.isCredit = false;
trans.transactionType = 'Debit';
}
break;
}
finaldata.push(trans);
});
if (initialCount != finaldata.length) {
let diff = initialCount - finaldata.length;
console.log('Hiding ' + diff + ' duplicates from PC API');
}
console.log('Processed ' + finaldata.length + ' transactions');
pcdata.spData.transactions = finaldata;
}