0

I am write extension for Internet Explorer (BHO). I read many questions, answers, but can't found and understand, how make execute Javascript file from resource on every page? Not append web page, need necessary execute JS on every page.

I know, that i need make execute after OnDocumentComplete, but i can't know, how do this in C++... I found this extension, but it is written in C# and i can't convert it to C++.:

https://github.com/dvdotsenko/livereload_ie_extension

I have example BHO in C++: http://www.codeproject.com/Articles/37044/Writing-a-BHO-in-Plain-C

Can you help me to add execute JavaScript from resource on every page?

1
  • Could you provide feedback with our answers? Commented Nov 12, 2013 at 8:13

2 Answers 2

1

You are looking for IHTMLWindow2::execScript.

You can get the IHTMLDocument2 pointer by doing the following:

  1. Call IWebBrowser2::get_Document().
  2. QueryInterface() the resulting IDispatch pointer for IID_IHTMLDocument2.

You should be able to get the IHTMLWindow2 pointer by calling get_parentWindow on the IHTMLDocument2 object and doing a similar dance.

Here are some example functions. You must implement IObjectWithSite and cache your site pointer. You can pass that to these functions.

HRESULT Web2FromSite(IUnknown *punkSite, IWebBrowser2 **pWeb2) {
    IServiceProvider* psp;
    HRESULT hr = punkSite->QueryInterface(IID_IServiceProvider, (void **)&psp);
    if (SUCCEEDED(hr))
    {
        hr = psp->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, (void **)pWeb2);
        psp->Release();
    }
    return hr;
}

HRESULT Doc2FromWeb2(IWebBrowser2 *pWeb2, IHTMLDocument2 **ppDoc2) {
    CComPtr<IDispatch> spDisp;
    HRESULT hr = pWeb2->get_Document(&spDisp);
    if (SUCCEEDED(hr) && spDisp)
    {
        hr = spDisp->QueryInterface(IID_IHTMLDocument2, (void**)ppDoc2);
    } else {
        hr = E_FAIL;
    }
    return hr;
}

HRESULT Doc2FromSite(IUnknown *punkSite, IHTMLDocument2 **ppDoc2) {
    CComPtr<IWebBrowser2> spWeb2;
    HRESULT hr = Web2FromSite(punkSite, &spWeb2);
    if (SUCCEEDED(hr)) {
        hr = Doc2FromWeb2(spWeb2, ppDoc2);
    }
    return hr;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I know about this method, but i can't know, how exercise it in C++. In C# it is easier!
Okay, I updated with some more info. If you have more specific issues, you can post what you're trying to do, or ask more specific questions.
I know I ask to write the code for you - not very well. But could you help with code?
0

I suppose here that you already have a running BHO and that it processes OnDocumentComplete. I also suppose that your processing of OnDocumentComplete get you an IWebBrowser2 interface pointer. Let me know if that's not the case.

warning: error checking "perfectible".

HRESULT ExecScriptOnDocComplete(  CComPtr<IWebBrowser2> & spIWebBrowser2, BSTR bstrScriptToExec ) {

    // get the IHTMLDocument2
    CComPtr<IDispatch> spIDispatchDocument;
    HRESULT hr = spIWebBrowser2->get_Document( &spIDispatchDocument );
    if ( FAILED( hr ) ) return hr;
    CComPtr<IHTMLDocument2> spIHTMLDocument2;
    hr = spIDispatchDocument.QueryInterface<IHTMLDocument2>( &spIHTMLDocument2 );
    if ( FAILED( hr ) ) return hr;

    // get the IHTMLWindow2
    CComPtr<IHTMLWindow2> spIHTMLWindow2;
    hr = spIHTMLDocument2->get_parentWindow( &spIHTMLWindow2 );
    if ( FAILED( hr ) ) return hr;

    // Execute the script
    CComVariant ccomvariantRetVal;
    hr = spIHTMLWindow2->execScript( bstrScriptToExec, CComBSTR( L"JavaScript" ), &ccomvariantRetVal );

    return hr;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.