Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
23 Feb 2010 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here