-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlDataExtractor.ts
More file actions
47 lines (37 loc) · 1.1 KB
/
Copy pathHtmlDataExtractor.ts
File metadata and controls
47 lines (37 loc) · 1.1 KB
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
export class HtmlDataExtractor {
public extractArray(html: string, variableName: string): string | null {
const startPattern = `let ${variableName} = `;
const startIndex = html.indexOf(startPattern);
if (startIndex === -1) {
return null;
}
let cursor = startIndex + startPattern.length;
while (cursor < html.length && /\s/.test(html[cursor])) {
cursor++;
}
if (html[cursor] !== '[') {
return null;
}
let bracketCount = 0;
const startDataIndex = cursor;
bracketCount++;
cursor++;
while (cursor < html.length && bracketCount > 0) {
if (html[cursor] === '[') {
bracketCount++;
} else if (html[cursor] === ']') {
bracketCount--;
}
cursor++;
}
if (bracketCount === 0) {
return html.substring(startDataIndex, cursor);
}
return null;
}
public extractGlobalDefinitions(html: string): string | null {
const definitionsRegex = /(?:let|const) GLOBAL_DEFINITIONS = (\{.*?\});/;
const match = html.match(definitionsRegex);
return match && match[1] ? match[1] : null;
}
}