Javascript injection in Internet Explorer, returns null

Asked

Viewed 244 times

1

I am developing an application that performs automation in internet explorer, I am using the Shdocvw to do this.

My problem is I can’t get the return of an injection of Javascript

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 variable Resp always returns null

The variable script can receive any Javascript command from a single document.getElementById('id').value even call a function I have on the page:

Let’s say I have following function on my page:

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

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

  • You can order what the variable script contains?

  • @Danguilherme I edited the question... But clarifying, it can contain any Javascript command.

2 answers

3


This is what is expected from this method.

In accordance with the documentation the method execScript always returns null

See an excerpt:

This method Always Returns null.

How translated is:

This method always returns null

0

Test this section and see if it generates any exception

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

            if (doc != null){
                IHTMLWindow2 parentWindow = doc.parentWindow;
                if (parentWindow != null)
                    parentWindow.execScript(script, "javascript");
             }
        }
        catch(Exception ex) {
        }
  • This treatment would only check if there is a parentWindow or not, and this is not the question... I can run the scripts on the pages, only it does not return me anything.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.