how to disable images in webbrowser control - vb.net

in vb.net 2003 , how to disable images loading in webbrowser control

The way I have done this in the past is using Regex to replace image tags with empty quotes. Give the following a try, stick it into your DocumentLoaded event, or such:
origHTML = webBrowser.DocumentText
Dim newHTML as String
Dim regex as String = "<img.*/>"
newHTML = Regex.Replace(origHTML, regex, "", RegexOptions.Multiline)
webBrowser.DocumentText = newHTML
This should solve your problem.
Best Regards

Related

check a checkbox on writer Libreoffice with vb.net

I want to mark the checkboxes I have on a writer document, I try but don't know what's the problem.
I tried putting the cursor on range and click but it doesn't work.
Dim oAnchor 'Bookmark anchor
Dim oCursor 'Cursor at the left most range.
Dim oMarks
oMarks = oDoc.getBookmarks()
oAnchor = oMarks.getByName("Documentado").getAnchor()
oCursor = oDoc.getCurrentController().getViewCursor()
oCursor.gotoRange(oAnchor, False)
oDoc.SendKeys(MouseButtons.Left)
I tried
oDoc.getBookmarks().getByName("Documentado").getAnchor.setState(1)
which doesn't work, I read something about "com.sun.star.awt.State","com.sun.star.awt.UnoControl", "com.sun.star.awt.XCheckBox", but don't know what I need to do.

Grab specific part of text from a local html file and use it as variable

I am making a small "home" application using VB. As the title says, I want to grab a part of text from a local html file and use it as variable, or put it in a textbox.
I have tried something like this...
Private Sub Open_Button_Click(sender As Object, e As EventArgs) Handles Open_Button.Click
Dim openFileDialog As New OpenFileDialog()
openFileDialog.CheckFileExists = True
openFileDialog.CheckPathExists = True
openFileDialog.FileName = ""
openFileDialog.Filter = "All|*.*"
openFileDialog.Multiselect = False
openFileDialog.Title = "Open"
If openFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim fileReader As String = My.Computer.FileSystem.ReadAllText(openFileDialog1.FileName)
TextBox.Text = fileReader
End If
End Sub
The result is to load the whole html code inside this textbox. What should I do so to grab a specific part of html files's code? Let's say I want to grab only the word text from this span...<span id="something">This is a text!!!</a>
I make the following assumptions on this answer.
Your html is valid - i.e. the id is completely unique in the document.
You will always have an id on your html tag
You'll always be using the same tag (e.g. span)
I'd do something like this:
' get the html document
Dim fileReader As String = My.Computer.FileSystem.ReadAllText(openFileDialog1.FileName)
' split the html text based on the span element
Dim fileSplit as string() = fileReader.Split(New String () {"<span id=""something"">"}, StringSplitOptions.None)
' get the last part of the text
fileReader = fileSplit.last
' we now need to trim everything after the close tag
fileSplit = fileReader.Split(New String () {"</span>"}, StringSplitOptions.None)
' get the first part of the text
fileReader = fileSplit.first
' the fileReader variable should now contain the contents of the span tag with id "something"
Note: this code is untested and I've typed it on the stack exchange mobile app, so there might be some auto correct typos in it.
You might want to add in some error validation such as making sure that the span element only occurs once, etc.
Using an HTML parser is highly recommended due to the HTML language's many nested tags (see this question for example).
However, finding the contents of a single tag using Regex is possible with no bigger problems if the HTML is formatted correctly.
This would be what you need (the function is case-insensitive):
Public Function FindTextInSpan(ByVal HTML As String, ByVal SpanId As String, ByVal LookFor As String) As String
Dim m As Match = Regex.Match(HTML, "(?<=<span.+id=""" & SpanId & """.*>.*)" & LookFor & "(?=.*<\/span>)", RegexOptions.IgnoreCase)
Return If(m IsNot Nothing, m.Value, "")
End Function
The parameters of the function are:
HTML: The HTML code as string.
SpanId: The id of the span (ex. <span id="hello"> - hello is the id)
LookFor: What text to look for inside the span.
Online test: http://ideone.com/luGw1V

How can I use decode encoded URL using VB.NET

I have an encoded url as follows;
dim inStr As String ="https%3A%2F%2Fmyweb%2page1%2Egov%2Emy%3A448%2Fserver%2FForm%3Fuserid%3Dtrouble"
I want to decode the url and assign this url to my webBrowser control.
From this thread:
Dim decodedUrl As String = HttpUtility.UrlDecode(encodedUrl)
I got a snippet to decode but am not using web application, am using windows application; can anyone suggest me the best way to do this?
you can use Uri.UnescapeDataString() method as follows:
Dim inputURL As String = "https%3A%2F%2Fmypage%2Eme%2Epage%2Esample%3A448%2Flfserver%2FForm%3Fuserid%3DTrouble"
Dim outputURL As String = Uri.UnescapeDataString(inputURL)
The output will be https://mypage.me.page.sample:448/lfserver/Form?userid=Trouble

Put text into a textbox from a website in VB.NET

I want to know how to put text to a website's textbox with VB.NET.
Like when you complete a textbox and press enter.
Something like this:
Dim web as webbrowser
web.document.textbox(1).insert("text")
it would look something like this:
WebBrowser1.Document.GetElementById("login_password").SetAttribute("value", TextBox2.Text)
As giuseppe has suggested, you need to search for the element in the document and then set it's value property. for example following code logins into website by setting its userId and password textboxes and clicking the submit button.
Dim htmlDoc=WebBrowser1.Document
Dim elem_Input_UsrName As HtmlElement = htmlDoc.GetElementById("username")
Dim elem_Input_PssWrd As HtmlElement = htmlDoc.GetElementById("password")
Dim elem_Input_Submit As HtmlElement = getElementByClassName("Submit", "Input", htmlDoc)
If elem_Input_UsrName IsNot Nothing AndAlso elem_Input_PssWrd IsNot Nothing Then
elem_Input_UsrName.SetAttribute("value", "xyz#ABC.com")
elem_Input_PssWrd.SetAttribute("value", "yourpassoword")
elem_Input_Submit.InvokeMember("click")
End If
This will help 100%:
webbrowser1.document.getElementById("yourtextboxidinwebsite").innertext = textbox1.text
I get a way that if u want to receive from the website itself another way I need to receive results from the website into the text box. U can use this method.
first, make a web browser to load the page
WebBrowser1.Navigate("https://tools.keycdn.com/geo")
then this
TextBox1.Text = WebBrowser1.Document.GetElementById("geoResult").InnerText

Text from webpage

I need to get some text from this web page. I want to use the trade feed for my program to analyse the sentiment of the markets.
I used the browser control and the get element command but its not working. The problem is that whenever my browser starts to open the page I get Java scripts errors.
I tried with DOM but seems that i dont quite understand what i need to do :)
Here is the code:
Dim code As String
Using client As New WebClient
code = client.DownloadString("http://openbook.etoro.com/ahanit/#/profile/Trades/")
End Using
Dim htmlDocument As IHTMLDocument2 = New HTMLDocument(code)
htmlDocument.write(htmlDocument)
Dim allElements As IHTMLElementCollection = htmlDocument.body.all
Dim allid As IHTMLElementCollection = allElements.tags("id")
Dim element As IHTMLElement
For Each element In allid
element.title = element.innerText
MsgBox(element.innerText)
Next
Update: So I tried the HTML Agility pack, as suggested in the comments, and I am stuck again on this code
Dim plain As String = String.Empty
Dim htmldoc As New HtmlAgilityPack.HtmlDocument
htmldoc.LoadHtml("http://openbook.etoro.com/ahanit/#/profile/Trades/")
Dim goodnods As HtmlAgilityPack.HtmlNodeCollection = htmldoc.DocumentNode.SelectNodes("THE PROBLEM")
For Each node In goodnods
TextBox1.Text = htmldoc.DocumentNode.InnerText
Next
Any advice what to now?
Ok I think I know what the problem is somehow the div that I need is hidden and its not loaded when I load the web page just the source code. Does someone knows how to load all the hidden divs ??
Here is my new code
Dim doc As New HtmlAgilityPack.HtmlDocument
Dim web As New HtmlWeb
doc = web.Load("http://openbook.etoro.com/ahanit/#/profile/Trades/")
Dim nodes As HtmlNode = doc.GetElementbyId("feed-items")
Dim id As String = nodes.WriteTo()
TextBox1.Text = TextBox1.Text & vbCrLf & id
user1336635,
Welcome to SO! Something you might try is to check out his source code, figure out what javascript function is populating the field you want (using firebug - I assume it's the one that "trades result in profit" next to it), and then embedding that script into a web page that your webbrowser control loads. That's where I'd try to start. I checked his source code and searched for "trades result in profit" and didn't find anything which leads me to believe hunting for the element 'might' not be possible. Just a starting place until someone with more experience with this chimes in!! Best!
-sf