-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffscreen.js
139 lines (123 loc) · 4.69 KB
/
offscreen.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
//Under developing from Van-Dung NGO (email: [email protected])
//Original release to github in July of 2023.
chrome.runtime.onMessage.addListener(OnReceive);
var keyWords = [];
// This function performs basic filtering and error checking on messages before
// dispatching the message to a more specific message handler.
async function OnReceive(message) {
// Return early if this message isn't meant for the offscreen document.
if (message.target !== 'offscreen') return false;
else {
keyWords = init_keyW_lst(message._addInfo);
const [ParsedRes,period] = await ParsingHTML2String(message.string);
SendMess(ParsedRes,period);
}
return;
}
function init_keyW_lst(Clas_name){
switch (Clas_name){
case 'techcom-account-transaction-history':
return ['techcom-transaction-history-list','TECHCOM-ACCOUNT-TRANSACTION-HISTORY-ITEM',"section-leading","account-transaction-history-item__content"
,"account-balance__amount","visible","amount--outgoing","info__name","info__message"];
case 'techcom-account-statement':
return ['techcom-account-statement-list','TECHCOM-ACCOUNT-STATEMENT-ITEM',"current-account-detail__section-leading","account-statement-item__content"
,"account-balance__amount","visible","account-statement-item__content__amount__outgoing","typo-default-semi-bold","bb-ellipsis--line-clamp"];
}
}
async function SendMess(data,period) {
const SendOrder = await chrome.runtime.sendMessage({
sender: 'offscreen',
target: 'background',
pipelineID: '4',
string: data,
_addInfo: period
});
}
async function ParsingHTML2String(MessString){
const parser = new DOMParser();
const ParsDoc = parser.parseFromString(MessString, 'text/html');
const TransList = ParsDoc.getElementsByTagName(keyWords[0]);
var SpawnChilds = TransList[0].children;
let ArrayString,period = ValidateEle(SpawnChilds);
return ArrayString,period
}
function ValidateEle(ListEle){
var TR = {date : '1999-01-01',
Amount : 'N/A',
Sinfo : 'N/A',
Info : 'N/A',
Balance : 'N/A'};
const ReturnArray = [['date','amount','purpose','info','balance']];
for (Elem of ListEle){
switch (Elem.tagName){
case keyWords[1]:
[TR.Amount,TR.Balance,TR.Sinfo,TR.Info] = ItemParsing(Elem);
let Ary = [TR.date.toString(),TR.Amount.toString(),TR.Sinfo.toString(),TR.Info.toString(),TR.Balance.toString()]
ReturnArray.push(Ary);
break;
case 'DIV':
if(Elem.classList.contains(keyWords[2]))
TR.date = DateUpdate(Elem);
break;
}
}
const period = `TCB_${TR.date.toString()} to ${ReturnArray[1][0]}`
return [ReturnArray,period];
}
function DateUpdate(DateSection){
const dateString = DateSection.textContent.trim();
if(dateString.match('Today') || dateString.match('nay'))
{
return find_date(0);
}
if(dateString.match('Yesterday') || dateString.match('qua'))
{
return find_date(1);
}
if(dateString.indexOf(" ") !== -1)
{
const dayApart = dateString.slice(0,dateString.indexOf(" "));
return find_date(dayApart-1);
}
else
//Temporary re-format dateString
if(dateString.match('/')) return format_date(dateString);
else return dateString;
}
function format_date(dayInput){
let subStr = dayInput.split("/");
let ReturnDate = `${subStr[2]}-${subStr[1]}-${subStr[0]}`;
return ReturnDate;
}
function find_date(daysApart){
const date = new Date();
let day = date.getDate();
let month = (date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : '0'+ (date.getMonth() + 1);
let year = date.getFullYear();
let day2find = day - daysApart;
//Refine the over-the-month date
if(day2find <= 0)
{
month = date.getMonth()-1;
day2find = new Date(year,month,0).getDate() + day2find;
month = date.getMonth() > 10 ? (date.getMonth() + 1) : '0'+ (date.getMonth() + 1);
}
day2find = (day2find<10)? `0${day2find}` : `${day2find}`;
let returnDate = `${year}-${month}-${day2find}`;
return returnDate;
}
function ItemParsing(Item)
{
const _Content = Item.getElementsByClassName(keyWords[3]);
//Transaction amount and balance
const _Balance = _Content[0].getElementsByClassName(keyWords[4]);
const _Direction = _Content[0].getElementsByClassName(keyWords[5]);
const Amount = (_Direction[0].classList.contains(keyWords[6])) ? ('-' + _Balance[0].textContent) : (_Balance[0].textContent);
const Balance = _Balance[2].textContent;
//Transaction Sinfo and info
const _infoName = _Content[0].getElementsByClassName(keyWords[7]);
const _infoMessage = _Content[0].getElementsByClassName(keyWords[8]);
const Sinfo = _infoName[0].textContent.trim();
const Info = _infoMessage[0].textContent.trim()
return [Amount,Balance,Sinfo,Info];
}