使用 IE 插件浏览器助手对象 (BHO) 在 iframe 中访问正文(至少一些数据) [英] Accessing body (at least some data) in a iframe with IE plugin Browser Helper Object (BHO)
问题描述
我正在开发一个 IE8+ BHO 插件.现在,我只是尝试将文本插入另一个 iframe (id="canvas_frame") 中包含的 iframe (class="Al Ai Editable").
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").
我设法获得了我想要添加文本的 iframe 的 IHTMLElement(class="Al Ai editable").我可以通过 IHTMLElement 类型的 el 变量来证明这一点:
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
在 MessageBox 中正确显示 iframe 的类.
is correctly displaying the class of the iframe in the MessageBox.
我现在遇到的问题是我无法访问相应 iframe 的 body 元素.
The problem i have now is that i can't access the body element of this respective iframe.
例如,当我尝试使用以下代码访问带有 id 的 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
此外,尝试获取内部 HTML 或内部文本我只是获取一个空字符串:
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);
或
el->get_innerText(&htm);
MessageBox(hwnd, htm, L"BHO cl", MB_OK);
不显示任何内容 ("").
displays nothing ("").
我什至尝试过 el->get_children 方法,但也没有用.
I've even tryed the el->get_children method and that didn't help either.
这是一个完整的函数:
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(&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();
}
}
推荐答案
错误的原因是安全限制.您正在访问嵌套的 iframe,因此您需要重新配置您的函数以递归方式工作以绕过限制.
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.
看看如何使用
hr = pContainer->EnumObjects(OLECONTF_EMBEDDINGS, &pEnumerator);
然后使用
doc->getElementsByTagName(SysAllocString(L"iframe"), &iframes);'
访问嵌套的 iframe 吗?这就是错误,您需要像第一个一样访问嵌套的 iframe,使用 EnumObjects,然后获取 IWebBrowser2 接口.
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.
这篇关于使用 IE 插件浏览器助手对象 (BHO) 在 iframe 中访问正文(至少一些数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!