4

I have a C# BHO which calls some JS functions in a document. Normally I did it like this (and everything worked fine):

IHTMLWindow2 wnd;
//...
wnd.execScript("testMethod(\"testData\");");

But now I need to return value from JS method to my BHO. I implemented test JS method which returns a string but when I use execScript nothing is returned. I started to read documentation about execScript method and found that now they recommend to use eval instead.

But I can't find any information on how to call this from my C# BHO. I have found this question and there is even c# example but it assumes that I host WebBrowser control and suggests to use Document.InvokeScript. And in MSHTML none of IHTMLDocument* interfaces have InvokeScript method. Am I missing something?

EDIT 1: Here is a question which kind of answers how to get return value from execScript. But its probably not smart to use execScript if MSDN says it is no longer supported.

EDIT 2: More code for this issue. First of all I have a JS function like this (in a file called func.js):

getElemHtml = function () {
    var myElem = document.getElementsByClassName("lineDiv")[0];
    // A lot more code goes here...
    alert(myElem.innerHTML);
    return myElem.innerHTML;
}

Then in my BHO I inject this script into the page like this:

StreamReader reader = new StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("func.js"));
string scriptContent = reader.ReadToEnd();
IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)ihtmlDoc2.all.tags("head")).item(null, 0);
IHTMLScriptElement scriptObject = (IHTMLScriptElement)htmlDoc2.createElement("script");
scriptObject.type = @"text/javascript";
scriptObject.text = scriptContent;
((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject);

Then in another part of BHO I want to get return value from getElemHtml():

var retVal = ihtmlWindow2.execScript("getElemHtml();");

but retVal is null. I see that script is executed and I see that return value is not null because I see alert window with return value. What I want is a return value from this JS function in my C# BHO code. It looks like this can be done using this answer but as I have said MSDN says I should use eval instead of execScript. The question is how to call eval and get a return value from my JS function.

9
  • your 2 lines of code doesn't give much info to work on. Post your code. Commented Jul 7, 2014 at 16:32
  • @L.B, my whole BHO code is a bit too complicated to post, I can create a demo BHO proj later but the point is that IHTMLWIndow2.exec script is no longer supported and in any case does not work as it should. And I am asking if somebody have example of calling "eval" from c# bho Commented Jul 7, 2014 at 16:42
  • I could post some code, but I am not sure what you really want to do. If you don't explain what you actually want to do, we can't help. Read this to see what I mean. Commented Jul 7, 2014 at 16:45
  • @L.B, funny link :) Added more code. Commented Jul 7, 2014 at 17:02
  • 1
    What are you actually trying to do here? If you told us what you're trying to accomplish, maybe we can direct you to a better solution. Commented Jul 7, 2014 at 17:05

1 Answer 1

6

I have found some links which allow to get return value from JS in C++ BHOs but I haven't managed to convert them in C# so here is a workaround way which worked for me:

// Execute method and save return value to a new document property.
ieHtmlWindow2.execScript("document.NewPropForResponse = getElemHtml();");

// Read document property.
var property = ((IExpando)ieHtmlDocument2).GetProperty("NewPropForResponse", BindingFlags.Default);
if (property != null)
    return property.GetValue(ieHtmlDocument2, null);  // returns return value from getElemHtml.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.