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

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

By , 23 Feb 2010
 
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!

License

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

About the Author

C. Groß
Germany Germany
Member
Organisation (No members)

No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberNaren Neelamegam24 Jan '13 - 2:57 
GeneralMy vote of 5memberAshraf Samy Hegab19 Jan '13 - 9:06 
GeneralMy vote of 5memberIvan Ferrer24 Oct '12 - 23:31 
GeneralMy vote of 5memberJordan Marr2 May '12 - 5:38 
GeneralReason for my vote of 5 I am new to JS so I didnt know the e...membermartin.skuta2 Apr '11 - 1:31 
GeneralReason for my vote of 5 that was a great help :)memberPennpann21 Mar '11 - 5:18 
QuestionAccess a javascript function's method which returns a DOM <textArea> element with VB.netmemberMember 811162625 Jul '11 - 15:06 
GeneralInvoking WPF from javascriptmemberfrankhevans2 Apr '10 - 11:53 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 24 Feb 2010
Article Copyright 2010 by C. Groß
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid