BHO Internet Explorer插件-脚本注入问题

BHO Internet Explorer plugin - script injection issues

本文关键字:脚本 注入 问题 插件 Internet Explorer BHO      更新时间:2023-09-26

我有一个BHO(IE插件),它将javascript注入目标页面:

string inject = "<div style='"display:none'">injected <script type='"text/javascript'" defer='"defer'">" + js +
        "</script></div>";
    body->insertAdjacentHTML(CComBSTR("afterBegin"), CComBSTR(inject.c_str()));

我可以在IE(F12)的开发人员控制台中看到,该脚本在IE9和IE11上都被正确注入。问题是该脚本仅在IE9上执行。IE11不执行脚本。造成这种差异的原因可能是什么?IE11是否为了某种类型的安全改进而阻止了这种情况?是否可以修改BHO以使该脚本也在IE11上执行?

我能够使它在IE10和IE11上工作。我没有注入insertAdjacentHTML,而是使用appendChild方法,并将HTMLScriptElement附加到它上面。

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!"));
    }