Results 1 to 9 of 9

Thread: [RESOLVED] Prevent WebBrowser From Loading Pictures

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    Resolved [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?
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Prevent WebBrowser From Loading Pictures

    You can remove the <img> tags as follows:
    VB Code:
    1. Private Sub WB1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    2.     Dim source As String
    3.  
    4.     source = WB1.Document.body.parentElement.innerHTML
    5.  
    6.     Do
    7.         Dim tagPos As Long, tagEnd As Long
    8.  
    9.         tagPos = InStr(1, source, "<img")
    10.         If (tagPos) Then
    11.             tagEnd = InStr(tagPos, source, ">")
    12.  
    13.             If (tagEnd) Then
    14.                 Mid$(source, tagPos, 4) = "<!--"
    15.                 Mid$(source, tagEnd - 2, 3) = "-->"
    16.                 tagPos = 0
    17.             End If
    18.         End If
    19.  
    20.     Loop While (tagPos > 0)
    21.  
    22.     WB1.Document.Clear
    23.     WB1.Document.write source
    24. End Sub

    Hacky I know, but still...

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    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.
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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:
    1. Private nextURL As String
    2. Private inParse As Boolean
    3.  
    4. Private Sub Form_Load()
    5.     WB1.Navigate2 "www.vbforums.com"
    6. End Sub
    7.  
    8. 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)
    9.     If ((pDisp Is WB1.Object) And (Not inParse)) Then _
    10.         nextURL = CStr(URL)
    11. End Sub
    12.  
    13. Private Sub WB1_DownloadBegin()
    14.     Static parsed As Boolean
    15.    
    16.     If (Not parsed) Then
    17.         inParse = True
    18.  
    19.         Dim buffer As String
    20.         WB1.Stop
    21.         buffer = Inet1.OpenURL(nextURL)
    22.  
    23.         Do
    24.             Dim tagPos As Long
    25.             Dim srcPos As Long, srcEnd As Long
    26.  
    27.             tagPos = InStr(tagPos + 1, buffer, "<img")
    28.             If (tagPos) Then
    29.                 srcPos = InStr(tagPos, buffer, "src=""") + 5
    30.                 If (srcPos) Then
    31.                     srcEnd = InStr(srcPos, buffer, """")
    32.                    
    33.                     If (srcEnd) Then
    34.                         Mid$(buffer, srcPos, srcEnd - srcPos) = Space$(srcEnd - srcPos)
    35.                     End If
    36.                 End If
    37.             End If
    38.  
    39.         Loop While (tagPos > 0)
    40.  
    41.         parsed = True
    42.         WB1.Navigate2 "about:blank"
    43.         WB1.Document.write buffer
    44.  
    45.         inParse = False
    46.     End If
    47.  
    48.     nextURL = vbNullString
    49. 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.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    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.
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    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
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width