Reputation: 3550
I have a BHO (plugin for IE) that injects the javascript to the target page:
string inject = "<div style=\"display:none\">injected <script type=\"text/javascript\" defer=\"defer\">" + js +
"</script></div>";
body->insertAdjacentHTML(CComBSTR("afterBegin"), CComBSTR(inject.c_str()));
I can see in the Developer Console of IE (F12) that the script is correctly injected on both IE9 and IE11. The problem is that the script is executed on IE9 only. IE11 does not execute the script. What may be the cause of this difference? Is IE11 preventing this for some type of safety improvements? Is it possible to modify the BHO to get this script executed on IE11 as well?
Upvotes: 1
Views: 909
Reputation: 3550
I was able to make it work on IE10 and IE11. Instead of injecting with insertAdjacentHTML i used the appendChild method and appended the HTMLScriptElement to it. Below is an extract of my code.
LOG(_T("Beginning script injection"));
CComPtr<IHTMLElement> htmlElement;
if (!SUCCEEDED(hr = doc->createElement(CComBSTR("script"), &htmlElement)))
{
LOG_ERROR(_T("createElement of type script failed"), hr);
return;
}
CComPtr<IHTMLScriptElement> htmlScript;
if (!SUCCEEDED(hr = htmlElement.QueryInterface<IHTMLScriptElement>(&htmlScript)))
{
LOG_ERROR(_T("QueryInterface<IHTMLScriptElement> failed"), hr);
return;
}
htmlScript->put_type(CComBSTR("text/javascript"));
htmlScript->put_text(CComBSTR(js.c_str()));
CComPtr<IHTMLDocument3> htmlDocument3;
if (!SUCCEEDED(hr = doc.QueryInterface<IHTMLDocument3>(&htmlDocument3)))
{
LOG_ERROR(_T("QueryInterface<IHTMLDocument3> failed"), hr);
return;
}
CComPtr<IHTMLElementCollection> htmlElementCollection;
if (!SUCCEEDED(hr = htmlDocument3->getElementsByTagName(CComBSTR("head"), &htmlElementCollection ) ) )
{
LOG_ERROR(_T("getElementsByTagName failed"), hr);
return;
}
CComVariant varItemIndex(0);
CComVariant varEmpty;
CComPtr<IDispatch> dispatchHeadElement;
if (!SUCCEEDED(hr = htmlElementCollection->item(varEmpty, varItemIndex, &dispatchHeadElement )) )
{
LOG_ERROR(_T("item failed"), hr);
return;
}
if (dispatchHeadElement == NULL )
{
LOG_ERROR(_T("dispatchHeadElement == NULL"), hr);
return;
}
CComQIPtr<IHTMLDOMNode, &IID_IHTMLDOMNode> spHeadNode = dispatchHeadElement; // query for DOM interfaces
CComQIPtr<IHTMLDOMNode, &IID_IHTMLDOMNode> spNodeNew = htmlScript;
if (spHeadNode)
{
if (!SUCCEEDED(hr = spHeadNode->appendChild(spNodeNew, NULL)))
{
LOG_ERROR(_T("dispatchHeadElement == NULL. Script injection failed"), hr);
return;
}
LOG(_T("Script injection SUCCESS!"));
}
Upvotes: 1