65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (18 votes)

Feb 23, 2010

CPOL

2 min read

viewsIcon

127746

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:
function callMe(arg1, arg2) {
	return "arg1 is "+arg1+" and arg2 is "+arg2+"!";
}
You would call it by using:
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:
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:
function getMyVariableValue() {
	return myVariable;
}
and call it by:
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:
object[] codeString = {"alert('Hello world!');"};
webBrowser1.Document.InvokeScript("eval",codeString);
Executing a method is now as easy...
object[] codeString = {"myObject.setVariable(0);"};
webBrowser1.Document.InvokeScript("eval",codeString);
...as getting the value of a variable:
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:
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!