如何注入javascript函数并在以后使用gecofx调用它
我正在使用Geckofx29和xulrunner如何注入javascript函数并在以后使用gecofx调用它,javascript,c#,geckofx,Javascript,C#,Geckofx,我正在使用Geckofx29和xulrunner string text = " function hi(){ return 1;}"; GeckoElement script = geckoWebBrowser.Document.CreateElement("script"); script.SetAttribute("type", "text/javascript"); script.SetAtt
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);
稍后我会这样调用这个函数
using (AutoJSContext context = new AutoJSContext(geckoWebBrowser.Window.JSContext))
{
context.EvaluateScript(" hi();" , out result);
}
我没有得到函数返回的值hi结果为空,评估脚本返回false
这有什么问题吗?有什么帮助吗?
谢谢我怀疑在文档中添加这样的脚本会导致解析 但是像这样执行脚本应该具有添加函数的预期效果:
string text = " function hi(){ return 1;}";
using (AutoJSContext context = new AutoJSContext(geckoWebBrowser.Window.JSContext))
{
context.EvaluateScript(text, out result);
}
然后你可以像以前那样称呼它:
using (AutoJSContext context = new AutoJSContext(geckoWebBrowser.Window.JSContext))
{
context.EvaluateScript(" hi();" , out result);
}
我已经意识到并修复了它。。无论如何,谢谢你的关心。