JavaScript Injection in Internet Explorer, returns null

1

I'm developing an application that performs automation in internet explorer, I'm using SHDocVw to do this.

My problem is that I can not get the feedback from a JavaScript injection

   
public static void wJScript(string script) {
  try {
    Thread th = new Thread(ExecuteJavaScriptWorker);
    th.SetApartmentState(ApartmentState.STA);
    th.Start(script);
  }
  catch (Exception) {
    throw;
  }
}

private static void ExecuteJavaScriptWorker(object script) {
  try {         
    IHTMLDocument2 document = IE.Document;
    object resp = document.parentWindow.execScript(script.ToString(), "JScript");
    Console.Write(resp);
  }
  catch (Exception) {
    throw;
  }
}

My resp variable always returns null

The variable script can get any JavaScript command from a simple document.getElementById('id').value to even call a function that I have on the page:

Let's say I have the following function on my page:

   
function soma(a,b) {
  return a + b;
}

And pass as a parameter to my function wJScript("soma(1,2)") it should return the result of the function, but this does not happen!

    
asked by anonymous 10.07.2014 / 16:46

2 answers

3

This is what is expected of this method.

According to documentation the execScript always returns null

See an excerpt:

  

This method always returns null.

What translated is:

  

This method always returns null

    
15.09.2014 / 02:16
0

Do testing with this snippet and see if it generates some exception

       try{
          IHTMLDocument2 doc = (IHTMLDocument2)IE.Document;

            if (doc != null){
                IHTMLWindow2 parentWindow = doc.parentWindow;
                if (parentWindow != null)
                    parentWindow.execScript(script, "javascript");
             }
        }
        catch(Exception ex) {
        }
    
16.07.2014 / 21:50