Click here to Skip to main content
Click here to Skip to main content

Inject Scripts into a Page in WebBrowser Control

, 15 Mar 2014
Rate this:
Please Sign up or sign in to vote.
Inject a script (JavaScript) into a web page loaded in a WebBrowser control.

Introduction

In this tip, I will show the easiest way to inject a script (JavaScript) into a web page loaded in a WebBrowser control.

Background

Basically, what this code does is invoke the global JavaScript eval() function, passing as parameter the script you want to invoke.

The eval() function evaluates or executes an argument. If the argument is an expression, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements.

Using the Code

Below, you can see an implementation of a method for injecting scripts:

VB.NET

Public Sub InjectScript(Script As String)
    WebBrowser1.Document.InvokeScript("eval", New Object() {Script})
End Sub  

C#

public void InjectScript(string Script)
{    
    WebBrowser1.Document.InvokeScript("eval", new object[] { Script });
}  

And a use example:

System.Text.StringBuilder scriptBuilder = new System.Text.StringBuilder();

scriptBuilder.AppendLine("{");
scriptBuilder.AppendLine("var text = ""JavaScript injection test"";");
scriptBuilder.AppendLine("alert(text);");
scriptBuilder.Append("}");
            
string script = scriptBuilder.ToString();
            
InjectScript(script); 

Remarks

If the script passed as parameter in InvokeScript returns a value, this value will be returned by InvokeScript, because it's a function!

So, you can do something like this:

string script = "function() { return document.getElementById('Example').innerText; }"; 
string elementInnerText = (string)WebBrowser1.Document.InvokeScript("eval", new object[] { script });

PS.: The code above is just to illustrate the concept. I did not test.

Original Source

This was originally posted on my personal blog (in Brazilian Portuguese). However, I thought it would be interesting to write a post about it in English.

However, if you speak Portuguese, feel the urge to visit the original article:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Herbert Lausmann
Student
Brazil Brazil
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinpremiumVolynsky Alex16-Mar-14 10:49 
GeneralRe: My vote of 5 PinprofessionalHerbert Lausmann16-Mar-14 10:58 
GeneralRe: My vote of 5 PinpremiumVolynsky Alex16-Mar-14 11:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 16 Mar 2014
Article Copyright 2014 by Herbert Lausmann
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid