C# BHO插件工作不正常

C# BHO插件工作不正常,c#,internet-explorer,bho,C#,Internet Explorer,Bho,我使用C#实现了BHO插件来检测网页中的数字。每次加载网页时,在网页中附加javascript,javascript检测数字并用超链接数字替换 代码 public void WebBrowser_DownloadComplete() { HTMLDocument document = (HTMLDocument)webBrowser.Document; IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)

我使用C#实现了BHO插件来检测网页中的数字。每次加载网页时,在网页中附加javascript,javascript检测数字并用超链接数字替换

代码

 public void  WebBrowser_DownloadComplete()
{
HTMLDocument document = (HTMLDocument)webBrowser.Document;

    IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)
                           document.all.tags("head")).item(null, 0);
    IHTMLScriptElement scriptObject = 
      (IHTMLScriptElement)document.createElement("script");
    scriptObject.type = @"text/javascript";
    scriptObject.src = @"myscript.js";
((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject);


}
问题

当我用离线网页测试插件时,它运行良好。但对于在线页面,它不起作用。DownloadComplete事件多次触发

问题


刷新和加载页面时IE会触发哪个事件

您必须在
DocumentLoaded
事件中添加条件,如下所示

if (rootDocLoaded && (HTMLDocument != null) && url != "about:blank"){
  //Your code here
}

使用上面的条件,它只在加载完整页面后调用一个。您还可以使用
之前导航
导航
方法。

您必须在
文档加载
事件中添加条件,如下所示

if (rootDocLoaded && (HTMLDocument != null) && url != "about:blank"){
  //Your code here
}
使用上面的条件,它只在加载完整页面后调用一个。您还可以使用
beforeNavigate
Navigate
方法