Results 1 to 1 of 1

Thread: Inject a Javascript code to a HTM document by using WebBrowser Control

  1. #1

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Inject a Javascript code to a HTM document by using WebBrowser Control

    vb Code:
    1. Private Sub Form_Load()
    2.   Me.Show
    3.   DoEvents
    4.  
    5.   WebBrowser1.Navigate "http://www.vbforums.com"
    6.  
    7.   Do While Not WebBrowser1.ReadyState = READYSTATE_COMPLETE
    8.     DoEvents
    9.   Loop
    10.  
    11.   Dim scriptNode As Object, headNode As Object
    12.  
    13.   Set scriptNode = WebBrowser1.Document.createElement("SCRIPT")       'create a new <script> node
    14.   scriptNode.Type = "text/javascript"                                 'set type to javascript
    15.  
    16.   scriptNode.Text = "function showMe(e) { alert(e.srcElement.src); e.srcElement.parentNode.outerHTML = e.srcElement.outerHTML; }" 'inject javascript code. After the src tag shown up, it will revert the changes on that image, so you get back the normal behavior.
    17.  
    18.   Set headNode = WebBrowser1.Document.getElementsByTagName("HEAD")    'select <head> node
    19.   Call headNode(0).appendChild(scriptNode)                            'attach <script> node to <head>
    20.  
    21.   Do While Not WebBrowser1.ReadyState = READYSTATE_COMPLETE
    22.     DoEvents
    23.   Loop
    24.  
    25.   Dim oImages As Object, oImage As Object. imageNode As Object  
    26.  
    27.   Set oImages = WebBrowser1.Document.getElementsByTagName("img")      'select all <img> tags
    28.  
    29.   'Select an image, temporarily neutralizes any <a> tag around the image, then attach an onclick event to it.
    30.   For Each oImage In oImages
    31.     Set imageNode = WebBrowser1.Document.createElement("A")           'create a new <a> tag
    32.     imageNode.href = "javascript:void(0)"                             'the href will be a .. nothing :)
    33.     imageNode.attachEvent "onclick", WebBrowser1.Document.parentWindow.showMe() 'attach event to the <a> tag
    34.     imageNode.innerHtml = oImage.outerHTML                            'place the <img> tag with all settings
    35.     Call oImage.replaceNode(imageNode)                                'replace the <img> tag, that is will be <a><img></a>
    36.   Next
    37.  
    38. End Sub

    The code will inject a simple javascript to the downloaded htm document, also attach event to each <img> (see: *1) tags, so it will pop up a messagebox, containing the src= property of any image you are clicked on.

    Edit:
    - The onclick event will be attached to the <a> tag that is a temporary tag. (*1)
    - If you click on an image, the src property of the image will be displayed, then it reverts the changes.

    Edit2:
    Code attached, that demonstrate an intelligent (vb6) way to inject the js, then clean up all changes, to revert the original. You can inject/remove the extra code any time to any pages you are browsing.
    Attached Files Attached Files
    Last edited by Jim Davis; Feb 21st, 2009 at 04:29 PM.

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