(REOPENED) Changing WebBrowser control contents before 1st render
*** UPDATE ***
This problem is with the WebBrowser component. The code posted below is what I am trying to insert into the control before it renders the page for the first time.
*** UPDATE ***
I am developing the application in C#, however the problem is not really language specific.
When I have a page that has a javascript call to:
Code:
window.print()
I am wanting to override the print method so that pages do not pop up the print dialog. I can do so by inserting this into the top of the html:
Code:
window.print=function emptyMethod() {};
I now just need to figure out how to get this code into the document html before the browser control renders it for the first time. I have tried a number of things (for a complete list, see the post listed above), all with no success. I find it hard to believe that there is no way to do it - I just haven't been able to figure it out so far...
Last edited by finn0013; Aug 16th, 2006 at 11:05 AM.
Re: Changing WebBrowser control contents before 1st render
Perhaps I wasn't clear...
This is an issue with the WebBrowser control, which is not VB/C# specific. There is a post in the C# forum, however I have posted here as well to broaden the views and hopefully get a solution.
The code I posted is simply the current solution I have come up with. My problem is that I have not been able to figure out how to change the HTML before the WebBrowser control renders it for the first time. It is important that I catch it before render because sometimes the javascript fires before my application is able to update the HTML, so I randomly get the print dialog window popping up.
Re: Changing WebBrowser control contents before 1st render
Well, the WebBrowser control isn't going to download the page of its own accord, it will only do it when you tell it to, so there is no need to then try to "catch" it in the act. Just alter the HTML before feeding it to it.
Re: Changing WebBrowser control contents before 1st render
In that case, you might like to have a look at the source for the VBForumsWrapper by Phill64. I don't know how it works but I believe he was altering the code of the pages before they were loaded. If that's the case it sounds like just what you're after.
Re: Changing WebBrowser control contents before 1st render
penagate - I am not quite following you... When I call navigate() and point it at a web site, I do not get the contents back to hand to the browser control, it just renders it. This application will allow a user to specify a list of pages to navigate to that it will "scrape" and place into a word document, so I need to be able to suppress the print dialog/alert boxes on a number of unknown pages. By overriding the javascript method, I can do that - if I can get it into the page.
jmcilhinney - Thanks, I will check that out and see how he solved this.
Re: Changing WebBrowser control contents before 1st render
I forgot to mention why that would not work in my last post...
Many of the pages users are likely to want will include graphics, dynamic html, and content rendered via ajax calls. By grabbing just the source I would be responsible for handling all that, which sounds like a much harder solution. By letting the browser control handle it, I can just grab the final rendered contents and I am done.
Re: Changing WebBrowser control contents before 1st render
Maybe you misunderstood, but you can do all that, all you are doing is altering the HTML then loading it into the webbrowser as if it was what was downloaded. It's exactly what you are asking for.
Re: Changing WebBrowser control contents before 1st render
By doing that, will the server persist the session? If a user signs into a site (i.e.: a forum) and points it at a bunch of pages that rely on the user being singed in, using the web browser control will allow the sesion to carry over. Will the WebClient (?) do the same?
Re: Changing WebBrowser control contents before 1st render
Thanks for the help, his code is doing exactly what I want.
This is really a C# question, but I think you guys may know the answer to this:
I started rewriting it in C#, but someone mentioned that they were pretty sure that I could just include the VB file in my source. Is this true?
Re: Changing WebBrowser control contents before 1st render
Originally Posted by finn0013
Thanks for the help, his code is doing exactly what I want.
This is really a C# question, but I think you guys may know the answer to this:
I started rewriting it in C#, but someone mentioned that they were pretty sure that I could just include the VB file in my source. Is this true?
Only as its own project...
basically you can have a VB.NET project and a C# project in the same C# solution... but you can't just stick a .vb file in a C# app.
Make the VB file into a class library, and then reference it from the C# app.
Re: *** RESOLVED *** Changing WebBrowser control contents before 1st render
Also, Maybe phil's code will help you.. however when you were originally having the issue, I did look into it, and I found that there is an event you can override in a derived WB class called PrintTemplateInstantiation, however it appears that if fires AFTER the print dialog has been generated.
I couldn't seem to find a way to cancel it from happening.. but it may be something to look at.
Re: *** RESOLVED *** Changing WebBrowser control contents before 1st render
I'm doing similar stuff at work. To stop the browser taking matters into its own hands I'm setting "AllowNavigate" to false at various stages and also hacking the HTML/XML before i mnually stuff it into the DocumentText property of the browser.
This way I am able to render WAP2.0 pages directly from a USB connected 3G-capable cellphone.
You may have to manually asign event handlers individually to all the links in the document too but thats another story. The alterative is to build your own rendering engine. That is hard work.
Re: *** RESOLVED *** Changing WebBrowser control contents before 1st render
I have created a test project with phil's code to see if it will work for me before going through the trouble of adding it to my project, but I can't get it to work - it does not actually update the Document.Body.InnerHtml. I can step through the code and see that, in the ForumModder.SetHTML method, the modified html is passed in and "set" to the Document.Body.InnerHtml, however if I check the value after the statement executes, the contents have not changed.
This seems to have worked for others under the application originally written by phil, and I have only modified 2 lines (added try/catch in the getHtml() method and removed the call to FixDropDowns() as I do not need it), so I would assume it should work the same...
Re: (REOPENED) Changing WebBrowser control contents before 1st render
So, in looking at this further, it appears that the rendered html is being updated, even though the InnerHtml property is not. If you update the code I attached, changing the doMods() method to do the following, you will notice that the text shows up blue, even though the InnerHtml (including viewing source on the page) shows the text as red.
This works, so I am going to include it in my project, but I am very curious as to what is happening in the background for this behavior to occur. Any ideas?
Re: (REOPENED) Changing WebBrowser control contents before 1st render
Why is it not reliable?
FYI for anyone interested: After looking at this more, it appears that it doesn't matter if I use the control created by phil or not. As long as I place my update code in the Navigated event, it appears to fire soon enough before rendering so that the contents get modified. I am not sure what advantage his control gives as he is not implementing either of the two extended events that it provides.
Re: (REOPENED) Changing WebBrowser control contents before 1st render
Hi all,
I know im digging an old thread up here, but i've been away
It's been awhile since i looked at my source for it, and i'm not sure which functions you say i havn't used? because GetHTML and SetHTML are the main functions which allow it to work.
I did it this way because it was the only way i could modify the pages without the control "forgetting" what domain it was in, it would change the url to about:blank, preventing links, cookies, images etc from correctly functioning. It is "tricking" the control into believing that is the true contents of the page by modifying its contents directly within the control.
Everything else, as you say you don't need, it's mostly to correct the javascript on the vbforum pages, as function load ups fail after the modifications.
There wasn't much to go by out there, so this was all basically trial and error for me. This was also converted from .NET1.1 to .NET2.0, there might have been some functions i didn't require after the upgrade.
Hope that answers your questions.
AntRush - Real-Time Strategy game, based around ants!
Re: (REOPENED) Changing WebBrowser control contents before 1st render
Cool - thanks for the clarification. I am new enough to .net that I was having trouble figuring out what your code allowed that the normal web browser didn't. I actually used your control for a test run but it was working for what I needed. I never did figure out a way to modify the page before the first load, so in most cases the javascript fired before i could modify what would be rendered.
Re: (REOPENED) Changing WebBrowser control contents before 1st render
You can easily do this by using the execScript method. By default, it passes the script as JScript. We have used it to execute JQuery (where it is hosted on the target window) and VBScript as well.
I have seen the Navigate method used, but this isn't very reliable. Sometimes the target is busy. With this it then becomes a matter of when to execute, but this may vary by application. We use OnBeforeNavigate2, which passes the target URL and if it's top level navigation. We usually only want to fire script if it isn't top level navigation (indicating an iframe or other frame navigation is happening.) That said, it makes it easy to run code on the window. We then test for WebBrowser1.Document & WebBrowser1.Document.parentWindow to make sure they exist. However, the target code is executed as it would be if in the target window. (basically exactly the code you show.)
We are using javascript, but this should give you the idea...
You have to target the window properly as follows...