|
-
Dec 1st, 2005, 10:03 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Prevent WebBrowser From Loading Pictures
I need my app to load webpages very quickly, so how can I prevent it from downloading images? The specific images that I am trying to stop are from a certain domain, so maybe its possible to prevent downloading from that domain?
I need to make sure it doesnt change any settings in internet explorer.
I heard somewhere that this can be done by removing the img src=, but how do I do this?
-
Dec 1st, 2005, 10:26 AM
#2
Re: Prevent WebBrowser From Loading Pictures
You can remove the <img> tags as follows:
VB Code:
Private Sub WB1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim source As String
source = WB1.Document.body.parentElement.innerHTML
Do
Dim tagPos As Long, tagEnd As Long
tagPos = InStr(1, source, "<img")
If (tagPos) Then
tagEnd = InStr(tagPos, source, ">")
If (tagEnd) Then
Mid$(source, tagPos, 4) = "<!--"
Mid$(source, tagEnd - 2, 3) = "-->"
tagPos = 0
End If
End If
Loop While (tagPos > 0)
WB1.Document.Clear
WB1.Document.write source
End Sub
Hacky I know, but still...
-
Dec 1st, 2005, 10:28 AM
#3
Re: Prevent WebBrowser From Loading Pictures
On second thoughts, you may want to use an Inet control to download the page source first, run it through my parsing loop above, then feed it to the WebBrowser control AFTER the img tags have been removed. Because my code above probably doesn't stop them from being downloaded.
You can feed HTML source in to the control by navigating to "about:blank" and then using the .Document.Write method, passing the parsed HTML source code.
-
Dec 1st, 2005, 10:36 AM
#4
Thread Starter
Fanatic Member
Re: Prevent WebBrowser From Loading Pictures
Thanx penagate, but I dont think that'll work 4 me as I've already developed my entire app and I'll have to change it to make this work.
Is there any other way to do it? Can't I prevent it from downloading anything from certain URLs?
Last edited by shirazamod; Dec 1st, 2005 at 10:41 AM.
-
Dec 1st, 2005, 11:00 AM
#5
Re: Prevent WebBrowser From Loading Pictures
OK, Mr. HackySolution™ to the rescue 
Change "WB1" to the name of your browser control. Add an Internet Transfer Control to your form and change "Inet1" to its name.
You can then use the WebBrowser just as you had been doing before - the added code doesn't affect how you call it or anything, it just strips images from the HTML source as you go.
VB Code:
Private nextURL As String
Private inParse As Boolean
Private Sub Form_Load()
WB1.Navigate2 "www.vbforums.com"
End Sub
Private Sub WB1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
If ((pDisp Is WB1.Object) And (Not inParse)) Then _
nextURL = CStr(URL)
End Sub
Private Sub WB1_DownloadBegin()
Static parsed As Boolean
If (Not parsed) Then
inParse = True
Dim buffer As String
WB1.Stop
buffer = Inet1.OpenURL(nextURL)
Do
Dim tagPos As Long
Dim srcPos As Long, srcEnd As Long
tagPos = InStr(tagPos + 1, buffer, "<img")
If (tagPos) Then
srcPos = InStr(tagPos, buffer, "src=""") + 5
If (srcPos) Then
srcEnd = InStr(srcPos, buffer, """")
If (srcEnd) Then
Mid$(buffer, srcPos, srcEnd - srcPos) = Space$(srcEnd - srcPos)
End If
End If
End If
Loop While (tagPos > 0)
parsed = True
WB1.Navigate2 "about:blank"
WB1.Document.write buffer
inParse = False
End If
nextURL = vbNullString
End Sub
Shout out if you want me to explain any of the code for ya
Last edited by penagate; Dec 1st, 2005 at 11:17 AM.
-
Dec 1st, 2005, 11:06 AM
#6
Thread Starter
Fanatic Member
Re: Prevent WebBrowser From Loading Pictures
wow, thanx penagate!
That looks great. I'll try it out after I have dinner (abt 30 min) & let you know how it goes.
-
Dec 1st, 2005, 11:20 AM
#7
Re: Prevent WebBrowser From Loading Pictures
I've made some changes. It now replaces the src attribute of every <img> tag with spaces (of the same length as the original address, so that the buffer string is not re-created - faster this way). I also fixed the unfortunate infinite loop 
The downside is that you will get Javascript error messages on pages that use JS to manipulate the images, because the images no longer exist. And it is a bit slow. A better alternative might be to have a small 1x1 pixel GIF image in the application directory and replace the src attribute of all images with the address of that GIF file (e.g. "file://" & App.Path & "/blank1x1.gif"). That would probably fix the problem with JS error messages.
-
Dec 1st, 2005, 11:53 AM
#8
Re: Prevent WebBrowser From Loading Pictures
OK, encountered more problems - relative links don't work because of the need to navigate to "about:blank" in order to dump in the source - hence the relativity is broken.
-
Dec 1st, 2005, 12:10 PM
#9
Thread Starter
Fanatic Member
Re: Prevent WebBrowser From Loading Pictures
That's fine, I will be navigating to a page with lyrics so I will only need to get the text from the page and that is all
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|