开发者

Accessing body (at least some data) in a iframe with IE plugin Browser Helper Object (BHO)

开发者 https://www.devze.com 2023-02-14 00:08 出处:网络
I\'m developing an IE8+ BHO plugin. For now I\'m simply trying to insert a text into an iframe (class=\"Al Ai Editable\") contained in another iframe (id=\"canvas_frame\").

I'm developing an IE8+ BHO plugin. For now I'm simply trying to insert a text into an iframe (class="Al Ai Editable") contained in another iframe (id="canvas_frame").

I managed to obtain the IHTMLElement of the iframe i want to add text to (the class="Al Ai editable"). I can prove this by the fact that the el variable which is of type IHTMLElement :

el->get_className(&cl); //Al Ai editable

is correctly displaying the class of the iframe in the MessageBox.

The problem i have now is that i can't access the body element of this respective iframe.

For example when i try to access the body of the iframe with its id using the following code then this crashes the browser:

el->getElementById(L":d6", &el); // ":d6" is the id of the body inside the iframe

Also, trying to obtain the inner HTML or inner Text i simply obtain an empty string:

el->get_innerHTML(&htm);
MessageBox(hwnd, htm, L"BHO cl", MB_OK);

or

el->get_innerText(&htm);
MessageBox(hwnd, htm, L"BHO cl", MB_OK);

displays nothing ("").

I've even tryed the el->get_children method and that didn't help either.

Here's a the whole function :

void CgmailAdderBHO::checkIframes(HWND hwnd, IDispatch *lpDisp) {

USES_CONVERSION;

if (lpDisp) {
    IOleContainer* pContainer;

    // Get the container
    HRESULT hr = lpDisp->QueryInterface(IID_IOleContainer,
                                       (void**)&pContainer);
    lpDisp->Release();

    if (FAILED(hr)) {
      return;
    }

   IEnumUnknown* pEnumerator;

   // Get an enumerator for the frames
   hr = pContainer->EnumObjects(OLECONTF_EMBEDDINGS, &pEnumerator);
   pContainer->Release();

   if (FAILED(hr)) {
      return;
   }

   IUnknown* pUnk;
   ULONG uFetched;

   // Enumerate and refresh all the frames
   for (UINT i = 0; S_OK == pEnumerator->Next(1, &pUnk, &uFetched); i++)
   {
      IWebBrowser2* pBrowser;

      hr = pUnk->QueryInterface(IID_IWebBrowser2, (void**)&pBrowser);
      pUnk->Release();

     if (SUCCEEDED(hr))
     {
         // process the iframe
         CComPtr<IDispatch> docDisp;
        pBrowser->get_Document(&docDisp);
        CComQIPtr<IHTMLDocument3> doc = docDisp;
        CComQIPtr<IHTMLElementCollection> iframes;

        HRESULT hr = doc->getElementsByTagName(SysAllocString(L"body"), &iframes);
        long length;
        iframes->get_length(&length);
        CComVariant itemIndex(0);
        CComVariant empty;
        CComQIPtr<IDispatch> htmlEl;

        iframes->item(itemIndex, empty, &htmlEl);
        CComQIPtr<IHTMLElement> el = htmlEl;
        BSTR cl;
        BSTR cln(L"cP");
        el->get_className(&cl);

        if (cl && strcmp(OLE2A(cl), "cP") == 0) {
            //this is the canvas_frame
            // check if it has any other subframes
            BSTR html;
            el->get_innerHTML(&开发者_StackOverflow社区;html);

            doc->getElementsByTagName(SysAllocString(L"iframe"), &iframes);
            if (iframes) {
                iframes->get_length(&length);
                if (length > 0) {
                    //MessageBox(hwnd, L"We are on compose!", L"BHO", MB_OK);
                    //add encrypt button code here
                    iframes->item(itemIndex, empty, &htmlEl);
                    el = htmlEl;
                    el->get_className(&cl); //Al Ai editable

                    BSTR htm;
                    el->get_innerHTML(&htm);
                    MessageBox(hwnd, cl, L"BHO cl", MB_OK);
                    MessageBox(hwnd, htm, L"BHO cl", MB_OK);

                    CComQIPtr<IHTMLDocument3> docul = htmlEl;

                    //docul->getElementById(L":d6", &el);

                    break; // found iframe ; now exit for
                }
            }
        }
        pBrowser->Release();
      }
   }
   pEnumerator->Release();
}
}


The reason for the error is a security restriction. You're accessing a nested iframe so you'll need to reconfigure your function to work recursively to bypass the restriction.

See how you access the first level of iframes using

hr = pContainer->EnumObjects(OLECONTF_EMBEDDINGS, &pEnumerator);

but then use

doc->getElementsByTagName(SysAllocString(L"iframe"), &iframes);'

to access the nested iframes ? That's the error, you need to access the nested iframes the same as the first ones, using EnumObjects and then getting the IWebBrowser2 interface.

0

精彩评论

暂无评论...
验证码 换一张
取 消