在MSHTML IE8中修改DOM时是否会触发事件?

时间:2011-02-24 18:41:04

标签: internet-explorer browser webbrowser-control mshtml

尝试检测是否在MSHTML中添加或删除了DOM中的新元素。会有这样的事件发生吗?

http://msdn.microsoft.com/en-us/library/aa741877(v=VS.85).aspx

在IE9中有IDOMMutationEvent :: initMutationEvent

http://msdn.microsoft.com/en-us/library/ff975905(v=vs.85).aspx

3 个答案:

答案 0 :(得分:5)

是的,现在可以在IE8中使用它,虽然它不是MSHTML事件结构的一部分,但是可访问性。

触发器(EVENT_OBJECT_REORDER)是IE8中的新功能 - 请参阅http://blogs.msdn.com/b/ie/archive/2008/12/10/new-accessibility-features-in-ie8.aspx?PageIndex=7

我们在没有BHO的情况下使用它,但如果你选择的话,你可以同样在IE之外挂钩。

BHOClass::BHOClass()
{ 
if( TrackDHTML ) {
// end event is not used so +1.
// out of process notifications from accessibility
    m_eHook = ::SetWinEventHook(EVENT_OBJECT_REORDER, EVENT_OBJECT_REORDER+1, 0, MSAALib_WinEventProc, GetCurrentProcessId(), GetCurrentThreadId(), WINEVENT_OUTOFCONTEXT );
// in process injecting the dll into IE
//  m_eHook = ::SetWinEventHook(EVENT_OBJECT_REORDER, EVENT_OBJECT_REORDER+1, GetModuleHandle(L"yourhook.dll"), MSAALib_WinEventProc, GetCurrentProcessId(), GetCurrentThreadId(), WINEVENT_INCONTEXT );
}
}

以下是您可以查看辅助功能并获取文档/浏览器的方法

static void CALLBACK MSAALib_WinEventProc(HWINEVENTHOOK hook, 
  DWORD event, 
  HWND hwnd,  // this appears to be the hwnd for the tab and not the specific frame
  LONG idObject,  
  LONG idChild,
  DWORD dwEventThread, // the thread being watched that triggered this call
  DWORD dwmsEventTime)
{
if( hwnd != NULL // exclude most mouse move 
    && ( OBJID_WINDOW == idObject ) // || OBJID_CLIENT == idObject )
    )
{ 
    switch( event ) {
        case EVENT_OBJECT_REORDER: break;
        case EVENT_OBJECT_SHOW: break;
        case EVENT_OBJECT_HIDE: break;
        default:
            return;
    }

    Log(L"Event START - (%ld) object %ld on window(0x%x)%ld thread (0x%x)%ld\n", event, idObject, hwnd, hwnd, dwEventThread, dwEventThread);
    CComPtr<IAccessible> acc;
    VARIANT varChild;
    AccessibleObjectFromEvent(hwnd, idObject, idChild, &acc, &varChild);
    if( acc ) {
        // step 1 - change from Accessibility interface to html to check we have right type of reorder message
        CComPtr<IServiceProvider> provider;
        HRESULT hr = acc->QueryInterface(IID_IServiceProvider,(LPVOID *)&provider);
        if( SUCCEEDED(hr) && provider ){
            CComPtr<IHTMLElement> spElement;
            hr = provider->QueryService(IID_IHTMLElement,IID_IHTMLElement,(LPVOID *)&spElement);

            if( spElement ) {
                // step 2 - for this doc element get the service provider and then the browser element
                CComPtr<IServiceProvider> provider2;
                HRESULT hr = spElement->QueryInterface(IID_IServiceProvider,(LPVOID *)&provider2);
                CComPtr<IServiceProvider> provider3;
                if( provider2 )
                    hr = provider2->QueryService(SID_STopLevelBrowser,IID_IServiceProvider,(LPVOID *)&provider3);
                CComPtr<IWebBrowser2> browser;
                if( provider3 )
                    hr = provider3->QueryService(SID_SWebBrowserApp,IID_IWebBrowser2,(LPVOID *)&browser);

                if( browser ) {
                    // step 3 - Do stuff 
                }
            }
        }
    }
    Log(L"Event DONE - (%ld) object %ld on window(0x%x)%ld thread (0x%x)%ld\n", event, idObject, hwnd, hwnd, dwEventThread, dwEventThread);
}
}

答案 1 :(得分:0)

不,但是如果你通过像JQuery you can fake the event by yourself这样的类库修改DOM。

答案 2 :(得分:0)