how can i change the source code of a webpage with webbrowser control??

i want to add some source code to a webpage to add some functions for my own uses....

for example a webpage has a javascript function a(str){ ..... } i want to add an other function b to filter out some text.. so function a(str) {b(str); .... }..



Answer this question

how can i change the source code of a webpage with webbrowser control??

  • mark aoki

    er,,, i am not trying to change the source code of a file that is on the server side,i just want to modify the source on the client side only,, like i say above,, just want to add some javascript function to translate some text.. like: function a(var str){ // i want to add filterFunction(str) ...... }
  • Yaakov Davis

    can you modify the source and it still in the same url not in about:blank

    if so,, congraduation... i didn't make that works,,,

    and for your problem, you can try Navigated event....


  • spanky4_3

    I've moved this thread to the Windows Forums forum. You should have a better chance of getting it answered there.

    - Charlie



  • Gulden

    i'm using .net 2 with vs2005,, i 'm using webbrowser control too,, i got Navigated event,,,, in that event, its before the source code execute in the browser,,,
  • Bobby110uk

    for example, there are some ad blockers will block some of the ADs, and replace the AD's picture with "AD Blocked" or something,, but the page is still the "original page" not a new blank page with the original page's source code,,.. if you open a new blank page and write the original page's source code to it,, it will affect the browsing,, it'll consider as a different host's page,, the child frames from the original page can't access their parent page any more,, because their parent page is not there any more,, and the javascript functions try to access their parent's function will get permission denied..
  • Sascha Zeidler

    Yo Scorp,

    I'm working on this right now as well. No help on the web either. I need to remove a javascript function from the document that's calling a pop-up before it is presented to my webbrowser control.

    I have the popup somewhat restrained, but It's making my web auto-processing app take focus (grrr) and now it's time to kill it.

    I'm going to try to modify the document.script element. I'll let you know what I come up with.


  • Nguyen Minh Dung

    I can do this, but it's already too late. The script has already executed .

    Private Sub wb_DownloadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles wb.DownloadComplete

    Try

    Dim str As String = wb.Document.frames.item(1).document.scripts.item(9).outerHTML

    If str.IndexOf("createPopup") > 0 Then wb.Document.frames.item(1).document.scripts.item(9).outerHTML = ""

    Catch ex As Exception

    End Try

    End Sub

    How can I remove the script before it is rendered in the webbrowser (wb)


  • satya999

    hm.... how come i don't have Document.Scripts is that the vb.net and C# difference
  • Ty Y

    Well, yeah, you can't change the source code of a file that is on someone else's server... its illegal unless you own or own a share of that server and have permission. Even to do so, you would have to modify the source code from your program and upload the new file to the server. Which is why, you can only do it client side, and not server side.


  • bobhug

    In debug mode it gets rid of the script. But it's already too late.

    When I hit viewsource it is still there, but in step-thru debug mode it goes away as n element.

    I don't have Navigated event. I'm using the webbrowser com control in .net.

    This should not be this hard...anyone


  • hrubesh

    Thanks Charlie, much appreciated.

    The cool thing about the webbrowser control is that you can access the html elements and the entire page source code as its streamed to the client, like when you go to view > source in Internet Explorer for example.

    You can change the content on the document_completed event of the webbrowser control if you like.

    Now, in terms of actually getting (and setting) the code, take a look at the Document property. This property has pretty much everything for what you want to do. You can get (or set) the inner text/inner html of the current page forms (accessible by index), this maybe what you are after. You can also get (or set) the outer text and outer html properties too.

    I think looking at the docs would help you more but hope we have helped in some way :-)

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx



  • Rusty Trawler

    if i use browser.Document.Body.OuterHtml = "some code" it will raise an exception. Body.InnerHtml = "some code" same exception... :(
  • zapacila89

    can anyone help me i had search the web for many many hours...
  • Michael Hansen

    thanks for your reply,, i can change the source(actual its not changing the source, it writes a new html page with the url=about:blank), but the problem is,, after i change the source,, like webbrowser.DocumentText = "<a href='menu.htm'>test</a>"; it will change the source,, but the menu.html is linked to "about:blankmenu.htm" is not the url it should be... and the script from the child frame can't access its parent javascript function,, the permission blocks it..  :(

    if i use webbrowser.Document.Write() to write the html code,, it just a blank page,,,, won't show anything,, but the source code is written, i can see it when i right click and choose view source


  • how can i change the source code of a webpage with webbrowser control??