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.

Community
  • 1
  • 1
Oleg
  • 591
  • 4
  • 15
  • your 2 lines of code doesn't give much info to work on. Post your code. – L.B Jul 07 '14 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 – Oleg Jul 07 '14 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](http://www.perlmonks.org/?node=xy+problem) to see what I mean. – L.B Jul 07 '14 at 16:45
  • @L.B, funny link :) Added more code. – Oleg Jul 07 '14 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. – Robert Harvey Jul 07 '14 at 17:05
  • @RobertHarvey, my BHO enables advanced spellchecking on some sites. One of such sites is google docs editor. Among everything else I need to be able to get text around cursor. I wrote a JS function which does this and want to call it from my BHO – Oleg Jul 07 '14 at 17:17
  • I know how to call a JS function from my BHO and accessing the return value, but that's a C++ BHO, and this implies using IDispatch interfaces. Are you interested by some C++ code? Do you know how to translate such code in C#? – manuell Jul 10 '14 at 08:35
  • @manuell, if you have such a code - please post it and I will try to translate it into C# – Oleg Jul 24 '14 at 15:28
  • Will do, but not now. Holiday! – manuell Jul 24 '14 at 17:13

1 Answers1

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.
Oleg
  • 591
  • 4
  • 15