-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhtml-inspector.js
More file actions
118 lines (97 loc) · 2.88 KB
/
Copy pathhtml-inspector.js
File metadata and controls
118 lines (97 loc) · 2.88 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
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
"use strict";
// Remove accents from strings
String.prototype.removeAccents = function () {
return this
.replace(/[áàãâä]/gi, "a")
.replace(/[éè¨ê]/gi, "e")
.replace(/[íìïî]/gi, "i")
.replace(/[óòöôõ]/gi, "o")
.replace(/[úùüû]/gi, "u")
.replace(/[ç]/gi, "c")
.replace(/[ñ]/gi, "n")
.replace(/[^a-zA-Z0-9]/g, " ");
}
// Decode HTML entities using regex fallback
String.prototype.decodeEntities = function () {
return this
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/&#(\d+);/g, (_, dec) => String.fromCharCode(dec));
}
// Extract user ID from HTML
var extractUserId = function (htmlData) {
if (htmlData.includes('id="button_login"')) return 0;
const reg = /\?user_id=(\d+)&/;
const match = reg.exec(htmlData);
if (match) return match[1];
throw "User ID Not Found";
}
// Extract topic names
var extractTopicNames = function (htmlData) {
const res = {};
const reg = /<span id.*?topic=(\d+)\.0.*?">(.*?)<\/a>/g;
let match;
while ((match = reg.exec(htmlData))) {
res[match[1]] = match[2].trim().decodeEntities();
}
return res;
}
// Extract board names using regex
var extractBoardNames = function (htmlData) {
const res = {};
const anchorRegex = /<a\s+[^>]*href=["']([^"']+)["'][^>]*>(.*?)<\/a>/gi;
let match;
while ((match = anchorRegex.exec(htmlData)) !== null) {
const href = match[1];
const text = match[2].trim().decodeEntities();
const boardMatch = /board=(\d+)\.0$/.exec(href);
if (boardMatch && text.length > 0) {
res[boardMatch[1]] = text;
}
}
return res;
}
// Extract last posters
var extractLastPosters = function (htmlData) {
var res = {};
var reg = /<span id.*topic=(\d+)\.0.*">(.*)<\/a>/g;
var found;
while ((found = reg.exec(htmlData))) {
res[found[1]] = htmlData.substring(htmlData.indexOf(found[0])).match(/[\t ]{2,}(by|por).*">(.*)<\/a>/)[2].trim().decodeEntities();
}
return res;
}
// Extract unread topic links
var extractUnreadLinks = function (htmlData) {
const links = extractAnchorHrefs(htmlData);
return filterUnreadLinks(links);
};
// Extract all anchor hrefs
function extractAnchorHrefs(htmlData) {
const res = [];
const anchorRegex = /<a\s+(?:[^>]*?\s+)?href=["']([^"']+)["']/gi;
let match;
while ((match = anchorRegex.exec(htmlData)) !== null) {
res.push(match[1]);
}
return res;
}
// Filter unread topic links
var filterUnreadLinks = function (linksList) {
const res = {};
for (let i = 0; i < linksList.length; i++) {
if (linksList[i].includes("topicseen#new")) {
const topicId = extractTopicID(linksList[i]);
res[topicId] = linksList[i];
}
}
return res;
};
// Extract topic ID from link
var extractTopicID = function (link) {
const match = /topic=(\d+)/.exec(link);
return match ? match[1] : 0;
};