How can I run a [removed] tag that I just inserted dynamically from a BHO

后端 未结 1 673
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 13:42

I\'m completely new to developing IE extensions with Browser Helper Objects.

I managed to create a BHO that successfully inserts a script tag that references a javascri

相关标签:
1条回答
  • 2021-02-04 14:14

    You should use execScript instead of appendChild. And the syntax of what you need to exec is very, very wierd. But it accomplishes what you want -- namely, an external JavaScript is added to the DOM. Call this during OnDocumentComplete:

    VARIANT vrt = {0};
    CComQIPtr<IHTMLWindow2> win;
    spHTMLDoc->get_parentWindow(&win);
    CComBSTR bstrScript = L"var html_doc = document.getElementsByTagName('head')[0]; var _js = document.createElement('script');  _js.setAttribute('type', 'text/javascript'); _js.setAttribute('id', 'bho_js'); _js.setAttribute('src', 'http://domain.com/script.js'); if(!document.getElementById('bho_js')) html_doc.appendChild(_js);";
    CComBSTR bstrLanguage = L"javascript";
    HRESULT hrexec = win->execScript(bstrScript,bstrLanguage, &vrt);
    

    This will add <script type="text/javascript" id="bho_js" src="http://domain.com/script.js"></script> into the DOM HEAD.

    0 讨论(0)
提交回复
热议问题