Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / Javascript
Tip/Trick

Using WebBrowser.Document.InvokeScript() to mess around with foreign JavaScript

Rate me:
Please Sign up or sign in to vote.
5.00/5 (19 votes)
23 Feb 2010CPOL2 min read 125.3K   13   9
The WebBrowser.Document.InvokeScript() method claims to provide a way to interact with JavaScript code inside of a WebBrowser HTML document. Trying to mess around with a third-party JavaScript over which I had no control (meaning: included in an external webpage that I could not alter), however,...
The WebBrowser.Document.InvokeScript() method claims to provide a way to interact with JavaScript code inside of a WebBrowser HTML document. Trying to mess around with a third-party JavaScript over which I had no control (meaning: included in an external webpage that I could not alter), however, I ran into some problems...

(Code examples will be in C#)

You have to pass on the arguments of the JavaScript function as an object[] argument of the InvokeScript() method. Let's presume you have the following JavaScript function:

JavaScript
function callMe(arg1, arg2) {
	return "arg1 is "+arg1+" and arg2 is "+arg2+"!";
}


You would call it by using:

C#
object[] args = {"argString1", "argString2"};
webBrowser1.Document.InvokeScript("callMe",args);


(Note that you use the function's name as a string without the parenthesis as the first argument of InvokeScript)

This works for functions built-in to JS as well, so for an ordinary alert()-message, you could do the following:

C#
object[] args = {"my important message"};
webBrowser1.Document.InvokeScript("alert",args);


However, I ran into problems when trying to call JS-methods of objects or trying to get the value of a variable. For the latter, if you were able to edit the JS code, you could of course use the following:

JavaScript
function getMyVariableValue() {
	return myVariable;
}


and call it by:

C#
webBrowser1.Document.InvokeScript("getMyVariableValue",args);


retreiving the variable's value.

But as I said, in my case I could not alter the code. For the objects-method issue, maybe it was just me - but something like ...InvokeScript("myObject.doSomething",args) didn't work. So I came up with the following solution:
JavaScript has, for those of you not too familiar with it, a function eval(string) which parses the given string as actual JavaScript code. Normally a possible security threat - bad thing! - it gets quite handy for our purpose.

Knowing this, you can now do the following:

C#
object[] codeString = {"alert('Hello world!');"};
webBrowser1.Document.InvokeScript("eval",codeString);


Executing a method is now as easy...

C#
object[] codeString = {"myObject.setVariable(0);"};
webBrowser1.Document.InvokeScript("eval",codeString);


...as getting the value of a variable:

C#
object[] codeString = {"aVariable;"};
string result = webBrowser1.Document.InvokeScript("eval",codeString);


Note here that InvokeScript really returns the JS data, so would now have the value of the aVariable Javascript variable in the C# variable result. Please make sure not to try to use "return aVariable;" in this case, as this will not return anything because of the data already being returned.

Wrapping it up into a function, you can make its use even more comfortable:

C#
private string sendJS(string JScript) {
	object[] args = {JScript};
	return webBrowser1.Document.InvokeScript("eval",args).ToString();
}


sendJS will be kind of your own personal JS-eval() working within your C# environment.

Hope this is of any use for someone!

License

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


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation (No members)


Comments and Discussions

 
QuestionUsing with VB.Net (Tipp) Pin
dherrmann13-Apr-17 22:59
dherrmann13-Apr-17 22:59 
GeneralMy vote of 5 Pin
Naren Neelamegam24-Jan-13 2:57
Naren Neelamegam24-Jan-13 2:57 
GeneralMy vote of 5 Pin
ashcairo19-Jan-13 9:06
professionalashcairo19-Jan-13 9:06 
GeneralMy vote of 5 Pin
Ivan Ferrer24-Oct-12 23:31
Ivan Ferrer24-Oct-12 23:31 
GeneralMy vote of 5 Pin
Jordan Marr2-May-12 5:38
Jordan Marr2-May-12 5:38 
GeneralReason for my vote of 5 I am new to JS so I didnt know the e... Pin
martin.skuta2-Apr-11 1:31
martin.skuta2-Apr-11 1:31 
GeneralReason for my vote of 5 that was a great help :) Pin
Pennpann21-Mar-11 5:18
Pennpann21-Mar-11 5:18 
QuestionAccess a javascript function's method which returns a DOM <textArea> element with VB.net Pin
Member 811162625-Jul-11 15:06
Member 811162625-Jul-11 15:06 
GeneralInvoking WPF from javascript Pin
frankhevans2-Apr-10 11:53
frankhevans2-Apr-10 11:53 

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

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