Results 1 to 26 of 26

Thread: (REOPENED) Changing WebBrowser control contents before 1st render

  1. #1

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    Question (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 ***

    This question actually stems from this post:
    http://www.vbforums.com/showthread.php?t=419160

    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.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,731

    Re: Changing WebBrowser control contents before 1st render

    Given that there's an ASP.NET forum and a C# forum, why is this post here?

  3. #3

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    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.

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

    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.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,731

    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.

    http://www.vbforums.com/showthread.php?t=390140

  6. #6

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    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.

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

    Re: Changing WebBrowser control contents before 1st render

    Ah yea, but you can download the HTML yourself and load it using the DOM methods.

  8. #8

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    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.

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

    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.

  10. #10

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    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?

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

    Re: Changing WebBrowser control contents before 1st render

    Good point.

    In that case, look at Phil's code.

  12. #12

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    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?

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

    Re: Changing WebBrowser control contents before 1st render

    False.

    Unless you have VS and it's a multi language solution. Note solution, not project. You can't mix and match languages within a project.

  14. #14

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    Re: Changing WebBrowser control contents before 1st render

    Cool - thanks for all the help and suggestions.

  15. #15
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Changing WebBrowser control contents before 1st render

    Quote 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.

  16. #16
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  17. #17
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.
    I don't live here any more.

  18. #18

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    Re: *** RESOLVED *** Changing WebBrowser control contents before 1st render

    So when you set AllowNavigate = false then it will still load the new html into the DocumentText property, just not render it?

  19. #19

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    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...

    Any ideas? I have attached my source.
    Attached Files Attached Files

  20. #20

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    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.

    VB Code:
    1. ForumModder1.SetHTML(pageHtml.Replace("red", "blue"))

    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?

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

    Re: (REOPENED) Changing WebBrowser control contents before 1st render

    innerHTML reflects the HTML code at the initial loading point of the DOM

    It is not very reliable in any circumstance.

  22. #22

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    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.

  23. #23
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    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.

  24. #24

    Thread Starter
    Addicted Member finn0013's Avatar
    Join Date
    Jan 2001
    Location
    Charleston, SC
    Posts
    222

    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.

    Thanks for the reply.

  25. #25
    New Member
    Join Date
    Jan 2011
    Posts
    1

    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...
    Code:
    WebBrowser1.Document.parentWindow.execScript("window.print=function emptyMethod() {};")
    you can pass VBScript if you prefer (but have to pass the optional 2nd parameter of execScript -- ,"VBScript", etc.)

    Happy coding...

  26. #26
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: (REOPENED) Changing WebBrowser control contents before 1st render

    Thanks. Exactly what I needed.

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