IE 自动化:如何确定用户启动的导航何时正在发生/已经发生?

发布于 2024-07-12 05:21:26 字数 609 浏览 7 评论 0原文

我有一个 Internet Explorer BHO(在 c# .net 中),想要识别用户何时启动导航,或者用户启动的导航何时完成。 我所说的“用户发起”是指单击链接或类似操作。 特别是如果正在加载的文档中有多个框架,我想将它们视为单个“导航”,但我想不出任何简单的方法来做到这一点。 我知道 BeforeNavigate2 和 DocumentComplete 事件,但看不到任何方法来区分用户单击链接时触发的 BeforeNavigate/DocumentComplete 事件和由于加载框架而触发的事件。

我想到的一种可能的解决方案是,顶部框架的 BeforeNavigate2 总是在内部框架之前触发(显然),然后子框架的 DocumentComplete 在顶部的 DocumentComplete 之前被调用,而顶部框架总是最后调用。 例如,我可以在 BeforeNavigates 中增加一个计数器,并在 DocumentComplete 中减少它,只有当它为 0 时,它才是用户启动的导航。

但我不确定我是否可以依靠这个或者是否有更好的方法来做到这一点。 例如,如果用户在其中一帧但并非所有帧都完成加载后按 ESC,会发生什么情况:顶部帧的 DocumentComplete 是否会被调用?

有什么建议么?

I have an Internet Explorer BHO (in c# .net) and want to identify either when a user initiates a navigation, or when a user-initiated navigation has completed. By user-initiated I mean clicking on a link or similar action. In particular if there are multiple frames in the document being loaded I want to treat them as a single 'navigation', but I can't think of any easy way to do this. I know the BeforeNavigate2 and DocumentComplete events, but can't see any way to differentiate between BeforeNavigate/DocumentComplete firing when a user has clicked on a link and it firing because a frame is loading.

One possible solution I'm thinking is that the BeforeNavigate2 for the top frame always gets fired before that of the inner frames (obviously), and then the DocumentComplete of the child frames get called before the DocumentComplete of the top, which is always called last. So for instance I could increment a counter in BeforeNavigates and decrement it in DocumentComplete, and only when it's 0 is it a user-initiated navigation.

But I'm not sure if I can rely on this or if there's a better way to do it. e.g. What happens if the user presses ESC after one of the frames but not all have finished loading: does the DocumentComplete of the top frame ever get called?

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2024-07-19 05:21:26

您可以通过针对您存储在 SetSite 中的浏览器对象指针测试 pDispParams 参数来测试 BeforeNavigate/NavigateComplete/DocumentComplete 事件是否来自 ineere 框架或最顶层的事件 你的 BHO 方法。

下面是执行此操作的 C++ 代码,我希望您可以轻松地将其转换为 C#:

STDMETHODIMP MyBHO::Invoke(DISPID dispidMember,
  REFIID riid, 
  LCID lcid,
  WORD wFlags,
  DISPPARAMS* pDispParams,
  VARIANT* pvarResult,
  EXCEPINFO* pExcepInfo,
  UINT* puArgErr)
{
  if( dispidMember != DISPID_BEFORENAVIGATE2 &&
      dispidMember != DISPID_NAVIGATECOMPLETE2 &&
      dispidMember != DISPID_DOCUMENTCOMPLETE )
    return S_OK;

  CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> frame =
    pDispParams->rgvarg[ 1 ].pdispVal;

  if( webBrowser2 != frame )
    return S_OK;
}

webBrowser2 是指向您在 SetSite 方法中获得的浏览器对象的指针,

You can test whether BeforeNavigate/NavigateComplete/DocumentComplete event came from ineere frame or the topmost one simple by testing pDispParams agruments against pointer to browser object you have stored in SetSite method of your BHO.

Here's C++ code to do this, I hope you can convert it easily to C#:

STDMETHODIMP MyBHO::Invoke(DISPID dispidMember,
  REFIID riid, 
  LCID lcid,
  WORD wFlags,
  DISPPARAMS* pDispParams,
  VARIANT* pvarResult,
  EXCEPINFO* pExcepInfo,
  UINT* puArgErr)
{
  if( dispidMember != DISPID_BEFORENAVIGATE2 &&
      dispidMember != DISPID_NAVIGATECOMPLETE2 &&
      dispidMember != DISPID_DOCUMENTCOMPLETE )
    return S_OK;

  CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> frame =
    pDispParams->rgvarg[ 1 ].pdispVal;

  if( webBrowser2 != frame )
    return S_OK;
}

webBrowser2 is pointer to browser object that you got in SetSite method,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文