-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodHTML.bas
221 lines (174 loc) · 6.57 KB
/
modHTML.bas
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
Attribute VB_Name = "modHTML"
'Module written by Tim Cinel 2003
' email: sickanimations@hotmail.com
'website: http://www.sickanimations.cjb.net/
'Steal this code - i dont care!
'You like my code? Email me! Find more of it at http://www.planetsourcecode.com/
Public Type HttpFileHeader
Content_Length As Long
Content_Type As String
Content_Location As String
HTTP_Proto As String
End Type
Public Type Link
HREF As String
face As String
target As String
End Type
Function HTTP_Protocol(Protocol As String, Output As Long) As String
On Error Resume Next
Select Case Output
Case 0 '0 HTTP VERSION
If CharCount(Protocol, " ") < 2 Then Exit Function
HTTP_Protocol = Split(Protocol, " ")(0)
Case 1 '1 HTTP RETURN NUMBER
If CharCount(Protocol, " ") < 2 Then Exit Function
HTTP_Protocol = Split(Protocol, " ")(1)
Case 2 '2 HTTP RETURN STRING
If CharCount(Protocol, " ") < 2 Then Exit Function
HTTP_Protocol = Split(Protocol, " ")(2)
End Select
End Function
Private Function AnalyseLink(ByVal LinkTag As String) As Link
On Error Resume Next
Dim StartPos As Long, EndPos As Long
'Get the link face
StartPos = InStr(1, LinkTag, ">")
EndPos = InStr(StartPos, LCase(LinkTag), "</a")
If EndPos > StartPos Then
StartPos = StartPos + Len(">")
AnalyseLink.face = Mid(LinkTag, StartPos, EndPos - StartPos)
End If
'Remove spaces and make sure all quotes are the same ( " can be used as well as ' in html )
'This makes things much easier
LinkTag = Replace(LinkTag, " ", "")
LinkTag = Replace(LinkTag, Chr(34), "'")
'Get the link reference
StartPos = InStr(1, LCase(LinkTag), "href='")
EndPos = InStr(StartPos + Len("href='"), LinkTag, "'")
If EndPos > StartPos Then
StartPos = StartPos + Len("href='")
AnalyseLink.HREF = Mid(LinkTag, StartPos, EndPos - StartPos)
End If
'Get the link target
StartPos = InStr(1, LCase(LinkTag), "target='")
EndPos = InStr(StartPos + Len("target='"), LinkTag, "'")
If EndPos > StartPos Then
StartPos = StartPos + Len("target='")
AnalyseLink.target = Mid(LinkTag, StartPos, EndPos - StartPos)
End If
End Function
Function ReadDocument(ByVal Document As String, URL As String, ByRef Title As String, ByRef Links() As Link)
On Error Resume Next
Dim StartPos As Long, EndPos As Long, CurrentLine As String, CurrentLink As Link
Dim FileDirectory As String
'Get the directory of the HTML document (Used when extending links)
FileDirectory = GetDirectory(URL)
'Find the title
StartPos = InStr(1, LCase(Document), "<title>")
EndPos = InStr(1, LCase(Document), "</title>")
'In case there is no title
If EndPos > StartPos Then
Title = Mid(Document, StartPos + Len("<title>"), (EndPos - (StartPos + Len("<title>"))))
Else
Title = ""
End If
'Debug.Print Document
'Dim the array
ReDim Links(0 To 0)
'Find the start first link
StartPos = InStr(1, LCase(Document), "<a")
Do Until StartPos = 0
'Find the end of the link just found
EndPos = InStr(StartPos, LCase(Document), "</a>")
'Incase the end of the link wasn't found
If EndPos > StartPos Then
EndPos = EndPos + Len("</a>")
'Put the whole tag into a string
CurrentLine = Mid(Document, StartPos, EndPos - StartPos)
'Analyse the tag to find the HREF, Face code, and the target.
CurrentLink = AnalyseLink(CurrentLine)
CurrentLink.HREF = ExtendLink(FileDirectory, CurrentLink.HREF)
If CurrentLink.HREF = "" Then GoTo NextLink
'Add the link to the Array
If UBound(Links) = 0 Then
ReDim Links(1 To 1)
Links(1) = CurrentLink
Else
ReDim Preserve Links(1 To UBound(Links) + 1)
Links(UBound(Links)) = CurrentLink
End If
End If
'Find start of the next link
NextLink:
StartPos = InStr(StartPos + 1, LCase(Document), "<a")
Loop
'Debug.Print Document
End Function
Function ExtendLink(ByVal URL As String, ByVal Link As String) As String
On Error Resume Next
Dim HostName As String
HostName = GetHostName(URL)
CheckLink:
If Left(Link, Len("../")) = "../" Then
'This refers to a parent directory
Link = Mid(Link, Len("../*"))
URL = GetParent(URL)
GoTo CheckLink
ElseIf Left(LCase(Link), Len("/")) = "/" Then
'Refers to a root directory
ExtendLink = HostName & Link
ElseIf Left(Link, Len("javascript:")) = "javascript:" Then
'This is a javascript link
ExtendLink = ""
ElseIf Left(Link, Len("#")) = "#" Then
'This is a link to an anchor
ExtendLink = ""
ElseIf Left(Link, Len("mailto:")) = "mailto:" Then
'This is a email link
ExtendLink = ""
ElseIf Left(LCase(Link), Len("http://")) = "http://" Then
'This is an absolute link
ExtendLink = Link
Else
'It is a relative link
ExtendLink = URL & Link
End If
End Function
Public Function CharCount(InputStr As String, Character As String) As Long
On Error Resume Next
Dim CurrentPos As Long
On Error GoTo ErrorHandler
Character = Mid(Character, 1, 1)
CurrentPos = InStr(1, InputStr, Character)
Do Until CurrentPos = 0
CharCount = CharCount + 1
CurrentPos = InStr(CurrentPos, InputStr, Character)
Loop
ErrorHandler:
End Function
Private Function GetHostName(ByVal URL As String) As String
On Error Resume Next
If Left(URL, Len("http://")) = "http://" Then URL = Mid(URL, Len("http://") + 1)
If InStr(1, URL, "/") = 0 Then URL = URL & "/"
GetHostName = Split(URL, "/")(0)
End Function
Function GetDirectory(ByVal URL As String) As String
On Error Resume Next
If Left(URL, Len("http://")) <> "http://" Then URL = "http://" & URL
If InStr(Len("http://*"), URL, "/") = 0 Then URL = URL & "/"
Dim temp() As String
temp() = Split(URL, "/")
For i = 0 To UBound(temp) - 1
GetDirectory = GetDirectory & temp(i) & "/"
Next i
End Function
Function GetParent(ByVal URL As String) As String
On Error Resume Next
Dim temp() As String
temp() = Split(URL, "/")
If UBound(temp) < 1 Then GetParent = URL: Exit Function
For i = 0 To UBound(temp) - 2
GetParent = GetParent & temp(i) & "/"
Next i
End Function