Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("http://examplesite.com/")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        getItems(WebBrowser1.Document.Body.InnerHtml.Split(vbCrLf),)
    End Sub

    Private Sub getItems(ByVal arLines() As String, ByVal selectedListBox As ListBox)
        selectedListBox.Items.Clear() '// Clear in case the webpage reloads.
        For i As Integer = 0 To arLines.Length - 1 '// Loop thru all Strings in the Array.
            If arLines(i).Contains("<script>") Then '// Locate the String that .Contains...
                Dim arTemp() As String = arLines(i).Split(">") '// .Split the entire String into Arrays.
                For Each itm As String In arTemp '// Loop thru all Strings in the Array.
                    If itm.EndsWith("</A") Then '// check if String .EndsWith("</A")
                        itm = itm.Replace("</A", "") '// Remove the HTML Code from String.
                    End If
                Next
                Exit For '// Exit the Loop.
            End If
        Next
    End Sub
End Class

I've been trying to block javascript/script for ever and just cannot get it.

I'm trying to block pesky ads etc. That's the code I was trying to use.

Recommended Answers

All 2 Replies

Let me know if this helps.
New Project, 1 WebBrowser.

Public Class Form1
    Private sVisitedURL As String = "" '// keeps track of Visited webpage, to not Reload.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.ScriptErrorsSuppressed = True '// since it will throw a few Alerts if not removing/replacing HTML correctly.
        WebBrowser1.Navigate("http://watch-series.com/serie/the_penguins_of_madagascar") '// JUST A RANDOM SITE SELECTED THAT CONTAINED ADS.
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If Not sVisitedURL = WebBrowser1.Url.AbsoluteUri Then '// Check if same URL to not keep Reloading Page once Modified.
            sVisitedURL = WebBrowser1.Url.AbsoluteUri '// set URL.
            WebBrowser1.Document.Body.InnerHtml = replaceHTML(WebBrowser1.Document.Body.InnerHtml.ToString) '// replace HTML.
        End If
    End Sub

    Function replaceHTML(ByRef wbHTML As String) As String
        '// sStart and sEnd work only with the current WebSite as to when App is Loaded.
        Dim sStart As String = "openx.org" '// change it to "<script" to remove all <SCRIPTS>, NOT RECOMMENDED TO REMOVE SCRIPTS, JUST LINKS.
        Dim sEnd As String = "/" '// change it with: "</script>" to remove all <SCRIPTS>
        Dim iStartIndex, iEndIndex As Integer, iCounter As Integer = 0
        Do While wbHTML.ToLower.Contains(sStart.ToLower)
            iStartIndex = wbHTML.ToLower.IndexOf(sStart.ToLower) '// Locate the Start String.
            iEndIndex = wbHTML.ToLower.IndexOf(sEnd.ToLower, iStartIndex) + sEnd.Length  '//Locate End String + sEnd.Length to include the sEnd.
            '//--- Testing Purpose Only - Displays which Strings will get Replaced with Nothing.
            MsgBox(wbHTML.Substring(iStartIndex, iEndIndex - iStartIndex))
            '---\\
            wbHTML = wbHTML.Replace(wbHTML.Substring(iStartIndex, iEndIndex - iStartIndex), "") '// replace .Substring with "nothing".
            iCounter += 1
        Loop
        MsgBox(iCounter & " Strings Removed that Started with: """ & sStart & """ and Ended with: """ & sEnd & """") '// FOR TESTING.
        Return wbHTML
    End Function
End Class

As commented in the code, you can replace your strings to have replaced.
For all "<scripts>", change it as the following.

Dim sStart As String = "<script" '// NOT RECOMMENDED TO REMOVE SCRIPTS, JUST LINKS.
        Dim sEnd As String = "</script>"

This is just a BETA StartUp Project. It will get results, just not as expected.

Forgot to mention that the previous post of mine, only blocks ads from "openx.org", at least for the most part, if not all.

No personal intentions to harm "openx.org" as a online business, it was just a site that was available to displays ads with the current URL for the WebBrowser.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.