Calling a javascript object method using WebBrowser.Document.InvokeScript

In my WinForms application, I need to call the javascript function from my WebBrowser control. I used Document.InvokeScript and works great with functions like

Document.InvokeScript("function"). 

But when I want to call a javascript object method, for example.

 Document.InvokeScript("obj.method") 

he does not work. Is there any way to make it work? Or another solution to this problem? Without changing anything in javascript code!

Thanks in advance:)

+7
source share
2 answers

The example in the documentation does NOT include brackets.

 private void InvokeScript() { if (webBrowser1.Document != null) { HtmlDocument doc = webBrowser1.Document; String str = doc.InvokeScript("test").ToString() ; Object jscriptObj = doc.InvokeScript("testJScriptObject"); Object domOb = doc.InvokeScript("testElement"); } } 

Try

 Document.InvokeMethod("obj.method"); 

Note that you can pass arguments if you use the HtmlDocument.InvokeScript (String, Object []) method .

Edit

It looks like you're not the only one in this problem: HtmlDocument.InvokeScript is an object method call . You can make a "proxy function" as the poster of this link suggests. Basically you have a function that calls your object function. This is not an ideal solution, but it will definitely work. I will continue to search if this is possible.

Another post on the same issue: Using WebBrowser.Document.InvokeScript () to interact with foreign JavaScript . An interesting solution proposed by C. Gross in CodeProject:

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

You can make this extension method on HtmlDocument and call it to run your function, only using this new function, you must include parentheses, arguments, nine yards in the line you go to (since it is simply passed a eval).

It seems that the HtmlDocument does not support calling methods for existing objects. Only global functions .: (

+9
source

Unfortunately, you cannot call object methods from the box using WebBrowser.Document.InvokeScript.

The solution is to provide a global function on the JavaScript side that can redirect your call. In its simplest form, it will look like this:

 function invoke(method, args) { // The root context is assumed to be the window object. The last part of the method parameter is the actual function name. var context = window; var namespace = method.split('.'); var func = namespace.pop(); // Resolve the context for (var i = 0; i < namespace.length; i++) { context = context[namespace[i]]; } // Invoke the target function. result = context[func].apply(context, args); } 

In your .NET code, you should use this as follows:

 var parameters = new object[] { "obj.method", yourArgument }; var resultJson = WebBrowser.Document.InvokeScript("invoke", parameters); 

As you mentioned that you cannot change anything in your existing JavaScript code, you will need to somehow introduce the above JavaScript method. Fortunately, the WebBrowser control can also do for you by calling the eval () method:

 WebBrowser.Document.InvokeScript("eval", javaScriptString); 

For a more reliable and complete implementation, see the WebBrowser tools I wrote and an article explaining ScriptingBridge , which is specifically designed to solve the problem you are describing.

+2
source

All Articles