sm.abdullah
sm.abdullah

Reputation: 1812

how to inject javascript function and call it later using gecofx

I am using GeckoFX 29 as well as xulrunner.

            string text = " function hi(){ return 1;}";
            GeckoElement script = geckoWebBrowser.Document.CreateElement("script");
            script.SetAttribute("type", "text/javascript");
            script.SetAttribute("language", "javascript");
            GeckoTextNode popText = geckoWebBrowser.Document.CreateTextNode(text);
            script.AppendChild(popText);
            geckoWebBrowser.Document.Head.AppendChild(script);

later on i am calling this function like this.

using (AutoJSContext context = new AutoJSContext(geckoWebBrowser.Window.JSContext))
            {
    context.EvaluateScript(" hi();" , out result);
}

i am not getting the value return by function hi result is empty evaluate script is returning false.

what is wrong with this any help ? thanks

Upvotes: 0

Views: 1436

Answers (1)

Tom
Tom

Reputation: 6729

I doubt that appending a script to the document like that causes it to be parsed.

But executing the script like this should have the desired effect of adding the function:

string text = " function hi(){ return 1;}";
using (AutoJSContext context = new AutoJSContext(geckoWebBrowser.Window.JSContext))
{
    context.EvaluateScript(text, out result);
}

And then later on you can call it like you were:

using (AutoJSContext context = new AutoJSContext(geckoWebBrowser.Window.JSContext))
{
        context.EvaluateScript(" hi();" , out result);
}

Upvotes: 1

Related Questions