我可以简单地做:
object DomElement = ChooseMyDomElement(webBrowser1); //this is a ID less element
webBrowser1.DocumentText = NewDocumentTextWithInjectedJavaScriptFunction;
webBrowser1.Document.InvokeScript("myfnc", DomElement);但是,我不想对加载的文档进行任何修改,比如设置DocumentText、创建一个新的脚本元素等等。
我试过:
object DomElement = ChooseMyDomElement(webBrowser1); //this is a ID less element
var js = "function myfnc(r) {alert(r);} myfnc(" + DomElement +");"; //DomElement is converted to string!
webBrowser1.Document.InvokeScript("eval", new object[] { js });问题在于,java将DomElement视为字符串!
我希望发送带有javascript函数的DomElement对象,以便在脚本中的DomElement上进行处理。
发布于 2014-02-19 05:38:26
试试这个:
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var anyScripts = webBrowser1.Document.GetElementsByTagName("script");
if (anyScripts == null || anyScripts.Count == 0)
{
// at least one <script> element must be present for eval to work
var script = webBrowser1.Document.CreateElement("script");
webBrowser1.Document.Body.AppendChild(script);
}
// use anonymous functions
dynamic func = webBrowser1.Document.InvokeScript("eval", new[] {
"(function() { return function(elem, color) { elem.style.backgroundColor = color; } })()" });
var body = this.webBrowser1.Document.Body.DomElement;
func(body, "red");
}发布于 2014-02-19 05:19:22
这样的东西应该在控件中执行javascript,而不必修改文档:
BrowserControl.Navigate("javascript:function test(){console.log('test');}test();")https://stackoverflow.com/questions/21871643
复制相似问题