Unfortunately, I am stuck writing a BHO for Internet Explorer. Its function is to introduce an iframe into every web page the user visits, displaying some (relatively) constant information when the user goes about his business. I managed to inject through the pretty hacker series mshtml cast, as follows, where Explorer is a WebBrowserClass object and foo.html is supplied with a BHO source:
IHTMLDocument2 document = (IHTMLDocument2) Explorer.Document;
IHTMLDocument3 doc3 = (IHTMLDocument3)document;
var iframe = document.createElement("iframe");
iframe.setAttribute("src", "foo.html");
iframe.setAttribute("id", "iFrame");
iframe.setAttribute("style", "position: fixed; left: 0px; top: 0px; border: 0px; width: 100%; height: 45px; background-color: white");
IHTMLElementCollection iec = doc3.getElementsByTagName("body");
IHTMLElement elem = (IHTMLElement)iec.item(0);
IHTMLDOMNode domnode = (IHTMLDOMNode)elem;
domnode.appendChild((IHTMLDOMNode)iframe);
It works! I think. Like. It causes an infinite loop if I insert it into OnDocumentComplete and it does not display an iframe anyway. The loop problem is using Explorer event listeners, such as OnDocumentComplete, because they are called whenever the page inside the iFrame finishes loading, which then calls the injection function, which then calls OnDocumentComplete, which ... well, you get this idea. I tried using OnNavigateComplete, but it still loads several times on pages downloaded from multiple sources (for example, google search main page with autocomplete turned on.) But even if it doesn't go into an infinite loop, I still don't see a visible iframe .
, , :
1) iframe? ( javascript)...
2) iframe , , BHO ?
:
void OnNavigateComplete(object pDisp, ref object URL)
{
if ((_currentDocument != null) && (isValidURL(_currentDocument.url)))
{
injectFrame();
}
}
void OnDocumentComplete(object pDisp, ref object URL)
{
if (Explorer.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
{
_currentDocument = Explorer.Document as mshtml.HTMLDocument;
if (_currentDocument != null)
{
}
}
}