Solved

VB6 Web Browser - Replace outterHTML and re-Evaluate code

Posted on 2008-06-23
10
2,240 Views
Last Modified: 2013-12-20
Hey everyone.
I've been working with Visual Basic 6, and I'm trying to do something I thought would be simple enough.

Basically, I created a new project, added the "Microsoft HTML Object Library" and "Microsoft Internet Controls" components and references, then dragged a WebBrowser component to my main form.

After this, I add the code in the snippet below and it appears to do nothing at all.

My problem is that when I do the Replace on a src of a javascript tag, it errors out with "Internet explorer could not open the Internet Page" Operation Aborted

But if I remove the Replace, everything works fine.

Also,
if I replace it with outerHTML, I do not see any replacements what so ever, it's as if it just doesn't work. But it does call the function, just doesn't set outerHTML
Any ideas are welcome!

----CODE SET 1----

'Appears to have no result what so ever.

Private Sub Form_Load()

    On Error Resume Next

    WebBrowser1.Navigate ("http://www.myipaddress.com")

End Sub
 

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)

    On Error Resume Next

    Dim doc As MSHTML.HTMLDocument

    Set doc = WebBrowser1.Document

    Dim packageContent As String

    packageContent = WebBrowser1.Document.documentElement.outerHTML

    newContent = Replace(packageContent, "http://pagead2.googlesyndication.com/pagead/show_ads.js", "http://www.test.com/test.js")

    doc.body.outerHTML = newContent

End Sub
 
 
 
 

----CODE SET 2----

'Appears to refresh the page constantly.

Private Sub Form_Load()

    On Error Resume Next

    WebBrowser1.Navigate ("http://www.myipaddress.com")

End Sub
 

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)

    On Error Resume Next

    Dim doc As MSHTML.HTMLDocument

    Set doc = WebBrowser1.Document

    Dim packageContent As String

    packageContent = WebBrowser1.Document.documentElement.innerHTML

    newContent = Replace(packageContent, "http://pagead2.googlesyndication.com/pagead/show_ads.js", "http://www.test.com/test.js")

    doc.body.innerHTML = newContent

End Sub
 
 
 
 
 
 

----CODE SET 3----

'This one works EXACTLY how I would like, with the exception that it replaces the baseURL to I'm assuming the temporary file location, instead of the website, so relatively linked images break. I can't find a way to set the baseURL back to the original page before it tries to load again.

Private Sub Form_Load()

    On Error Resume Next

    WebBrowser1.Navigate ("http://www.planet-source-code.com")

End Sub
 

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)

    On Error Resume Next

    Dim doc As MSHTML.HTMLDocument

    Set doc = WebBrowser1.Document

    Dim packageContent As String

    Dim newContent As String

    packageContent = WebBrowser1.Document.documentElement.outerHTML

    newContent = Replace(packageContent, "http://pagead2.googlesyndication.com/pagead/show_ads.js", "http://www.test.com/test.js")

    WebBrowser1.Document.Script.Document.Clear

    WebBrowser1.Document.Script.Document.Write newContent

    WebBrowser1.Document.Script.Document.Close

End Sub

Open in new window

0
Comment
Question by:ocnod1234
  • 6
  • 4
10 Comments
 

Author Comment

by:ocnod1234
ID: 21850830
Hi Guys,
With code set 3, I have figured that I can add a
newContent = Replace(newContent, "<HEAD>", "<HEAD><BASE href=""" & WebBrowser1.LocationURL & """ />")

But when I do the Document.Script.Write newContent
This does not evaluate the Javascript in the page that I've added
newContent = Replace(newContent, "<HEAD>", "<HEAD><BASE href=""" & WebBrowser1.LocationURL & """ /><script language=""text/javascript"">alert(""Hello World!"");</script>")

the above code will not show an alert dialog box, and this is basically what I am attempting to do, I just can't find a way to replace a javascript src , or replace content including javascripts and then run the page.

Any help is appreciated
0
 

Author Comment

by:ocnod1234
ID: 21852922
Well,
I've worked with it a lot since I posted this, and I'm pretty much where I was at before.

It doesn't seem I can actually modify the text/html before it executes the javascript, only after.
Even if I  use the WebBrowser1_ProgressChange control, it executes it as the page is rendered, before firing this control event.

Any help is greatly appreciated at this point, it's been about 2 weeks with simply trying to replace a javascript url before it runs the javascript on any page the webBrowser control visits
0
 
LVL 10

Expert Comment

by:cool12399
ID: 21883881
Hi,

There are a number of different ways to do this, and using the HTMLDocument is a pain in the butt, but try this when re-writing

WebBrowser1.navigate "about:blank"
WebBrowser1.Document.open
WebBrowser1.Document.write "YOUR NEW CONTENT HERE"
WebBrowser1.Document.Close
0
 

Author Comment

by:ocnod1234
ID: 21885840
Hi,
Thanks for your comment,
I tried a method like this, but my problem became sites like mail.yahoo.com that use javascript with location.href (Including inside <script src=""> includes which doesn't get parsed with outterHTML) that would navigate to about:blank instead of the true yahoo mail site, even with the html <BASE> tag forcing another location.


0
 
LVL 10

Accepted Solution

by:
cool12399 earned 500 total points
ID: 21886577
Ok,

Easiest thing to do is this then.

Download the .htm file that you want, read it into a text string, save it as a new file and open that. Then you won't have to worry about any DOM and it will work :)

Here's a sample:

(in a class file)

Public Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
   (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, _
    ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Public Function DownloadFile(ByVal URL As String, ByVal LocalFileName As String) As Boolean
 
  Dim lngRetval As Long
  DownloadFile = False
  lngRetval = URLDownloadToFile(0, URL, LocalFileName, 0, 0)
  If lngRetval = 0 Then DownloadFile = True
 
End Function

then in your main function:

Sub command1_click()

result = DownloadFile("http://www.mywebsite.com/test.htm","c:\temp.htm"

open "c:\temp.htm" for input as #1
while not eof(1)
  line input #1, tempstring
  filecontents  = filecontents   & tempstring & vbCrLf
wend

' do you replacing stuff

' resave the file (i.e., for output as #1... etc)

'now navigate to the 'new' page, i.e.,

webbrowser1.navigate "c:\test.htm"
end sub

and voila! it works! :)

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:ocnod1234
ID: 21894783
Hi,
Thank you for your time and comments, the solution would work for my application except one flaw that I found when visiting http://mail.yahoo.com/

It appears the location.href in javascripts still point to the local file. So I opened up a new question at http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23513903.html that I've tried to find a solution to the only issue with this method.

I would LOVE for this method to work, as it works perfectly, but I've spent quite some time figuring out that it doesn't seem possible to use a temporary file and have location.href set to another location other than the temp file by the WebBrowser control.
0
 
LVL 10

Expert Comment

by:cool12399
ID: 21895218
Well, could you please award me the points here, and I'll take a look at your second question for the javascript?

Thanks.
0
 

Author Comment

by:ocnod1234
ID: 21895833
Hi cool, if there are no solutions (And from my research I do not think there will be) that lead to a truly fully functional way of replacing outterHTML before the page is rendered and javascripts are run, then I will gladly award you the points. But currently, I have tried your method (My code set 3 in the original post) and acknowledged that this would not work for my situation before you posted your solution that uses the same code as my code example 3.

Since I have opened the question with this method stating it would not work, I hope you understand why I don't accept it right away as the accepted solution, as I've personally already tried this method before buying a membership to experts exchange :)

I do want to leave this question open in case a solution can be found to this issue, but if it goes on to much longer, I will acknowledge your proposed solution as the answer. I just simply don't want to close this question as I still have not found a solution, and would like any and all feedback I can receive on the subject matter as it's been over a week on 1 problem and I'm not that great of a vb programmer, but I can definitely do my research, and I am having trouble on my own finding a method that will work flawlessly without consequences.
0
 
LVL 10

Expert Comment

by:cool12399
ID: 21902522
Hi,

I answered your question here, and it should be closed now, especially because you've now opened a new question. As per the experts-exchange rules, you can't do that. Please award the points.

Thanks.
0
 

Author Comment

by:ocnod1234
ID: 21902858
Hi Cool,
To put it simply, your proposed solution is something I tried before posting this question. It is my code set 3. I have tried this method, and explained the issues that come from implementing this work-around.

Therefore, proposing something I have already tried, and this is proven by my posts prior to yours, I do not see as a valid reason for closing this ticket, as I clearly explained why this could not be used prior to you posting your solution.

I am sure anyone at EE would agree, that opening a second ticket, to solve a Reprocution from the original tickets proposed solution, while leaving the original question un-answer is completely fine, simply because there is still no working solution to replacing outterHTML before the onLoad command is run in  the web browser control, while keeping full functionality of the web browser control itself (It loses the ability to redirect to location.href as a string in javascript, therefore breaking any major site such as mail.yahoo.com which is not optional as these major sites must work in a web browser.).

I am sorry if there has been some confusion in my questions or replies trying to help in explaining what my issue is, but the fact remains that I have 2 seperate issues, and one is simply trying to fix a work-around proposed as a solution for the first issue. If this could be fixed (Location.href) then it would be a 100% accurate solution and I would GLADLY close it! But the problem is that with all of the proposed solutions, not one lets me modify outterHTML prior to executing the sites onLoad command allowing me to modify the true source of the website.

Again, like I said before, if there is not a true solution provided, even though I clearly stated your proposed solution as something I have tried and stated the problem with it prior to your original post, then I will mark yours as the accepted solution simply because  nobody has provided a closer answer.

I quote from my second post "Hi Guys,
With code set 3, I have figured that I can add a
newContent = Replace(newContent, "<HEAD>", "<HEAD><BASE href=""" & WebBrowser1.LocationURL & """ />")

But when I do the Document.Script.Write newContent
This does not evaluate the Javascript in the page that I've added
newContent = Replace(newContent, "<HEAD>", "<HEAD><BASE href=""" & WebBrowser1.LocationURL & """ /><script language=""text/javascript"">alert(""Hello World!"");</script>")

the above code will not show an alert dialog box, and this is basically what I am attempting to do, I just can't find a way to replace a javascript src , or replace content including javascripts and then run the page.

Any help is appreciated"

As I clearly said, using code set 3 (Piping the content to a temporary file, replacing the source in that and navigating to it) has issues, and is not a viable solution unless the bug that arises from it can be solved, as it breaks more than it helps.

I do appreciate your time, and believe me, if you can show me a browser that can replace outterHTML and fully navigate mail.yahoo.com , then I will accept this, but I have yet to find this simply because of yahoo's method of using the javascript DOM location.href object as a string variable to write the location of the login screen.

Thanks again,
Zack M.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now