-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetStatement.js
52 lines (50 loc) · 1.77 KB
/
getStatement.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
const parent = "../index.html";
const CURR_PATH = window.location.href.split("/");
const DOC_TYPE = CURR_PATH[CURR_PATH.length - 2].toUpperCase();
const STATEMENT_CODES_DEF = {
"BAL": "Balance Sheets",
"DEF": "Proxy Statements",
"DOC": "Issuer Reports",
"PNL": "Profit, Loss, & Retained Earnings",
};
const DOC_DISPLAY_TYPE = STATEMENT_CODES_DEF[DOC_TYPE];
function headerContainsText(header, text) {
return header.innerText.includes(text);
}
fetch(parent)
.then(response => response.text())
.then(html => {
const issuerInfo = document.createElement("div");
issuerInfo.innerHTML = html;
let statements = null;
const H3s = issuerInfo.querySelectorAll("h3");
H3s.forEach(function (header) {
if (header.textContent.includes(DOC_DISPLAY_TYPE)) {
statements = header.nextElementSibling;
}
});
if (DOC_DISPLAY_TYPE && statements) {
const list = statements.querySelector("ul");
if (list) {
const statementLinks = Array.from(list.querySelectorAll("a[href]"))
.map(a => a.getAttribute("href"));
if (statementLinks.length > 1) {
statementLinks.sort((a, b) => a.localeCompare(b));
}
if (statementLinks.length) {
const mostRecentStatementLink = statementLinks[0];
window.location.href = mostRecentStatementLink.split("/")[1];
} else {
throw new Error(`links in <ul> for ${DOC_TYPE}.`);
}
} else {
throw new Error(`${DOC_DISPLAY_TYPE} <ul> child.`);
}
} else {
throw new Error(`H3 content for type ${DOC_TYPE}.`);
}
})
.catch(error => {
console.log("Error finding statement: no", error.message);
window.location.href = "https://www.blocktransfer.com/404?utm_source=issuersInfo404&utm_medium=error";
});