简体   繁体   English

如何运行我刚刚从BHO动态插入的<script>标记

[英]How can I run a <script> tag that I just inserted dynamically from a BHO

I'm completely new to developing IE extensions with Browser Helper Objects. 我非常擅长使用Browser Helper Objects开发IE扩展。

I managed to create a BHO that successfully inserts a script tag that references a javascript file in the head of the HTML page (see code below). 我设法创建了一个BHO,它成功地插入了一个脚本标记,该标记引用了HTML页面头部的javascript文件(参见下面的代码)。

But the script tag just sits there in the DOM and the external javascript file is not executed. 但是脚本标记只是位于DOM中,并且执行外部javascript文件。

Is there any way to tell the browser to run the external javascript file? 有没有办法告诉浏览器运行外部JavaScript文件?

Thanks! 谢谢!

Code Details: I call the following method on the OnDocumentComplete event: 代码详细信息:我在OnDocumentComplete事件上调用以下方法:

void CHelloWorldBHO::InsertScriptTag(IDispatch* pDispDoc)
{
HRESULT hr = S_OK;
// query for an HTML document.
CComQIPtr<IHTMLDocument3> pDocument3 = pDispDoc;
CComQIPtr<IHTMLDocument2> pDocument2 = pDispDoc;
if (pDocument2 != NULL && pDocument3 != NULL)
{
    // **********************   create our script tag Element  (pHtmlElem) ****************************
    IHTMLElement* pHtmlElem;
    CComVariant vAlert="http://www.gnpcb.org/esv/share/js/?action=getDailyVerse"; // example referencing external JS code
    CComVariant vJavascript="text/javascript";
    hr = pDocument2->createElement(_T("script"), &pHtmlElem); 
    if (SUCCEEDED(hr) && pHtmlElem != NULL)
    {
        hr = pHtmlElem->setAttribute(_T("type"), vJavascript); 
        hr = pHtmlElem->setAttribute(_T("src"), vAlert);            
    }

    // **********************   insert Element  (pHtmlElem) in HTML Head ****************************
    // Get the head from the DOM.
    static const CComBSTR sbstrHead(L"head");
    CComPtr<IHTMLElementCollection> objects;
    hr = pDocument3->getElementsByTagName(sbstrHead, &objects);
    if(SUCCEEDED(hr) && objects != NULL)
    {
        // Get the number of elements in the collection.
        long nElements = 0;
        hr = objects->get_length(&nElements);
        if (hr == S_OK && nElements > 0)
        {
            CComVariant svarItemIndex(0); // we will get the first element
            CComVariant svarEmpty;
            CComPtr<IDispatch> spdispElement;

            // Get the element out of the collection with index 0 (the first element, that is, the head)
            hr = objects->item(svarItemIndex, svarEmpty, &spdispElement);
            if (hr == S_OK && spdispElement != NULL)
            {
                CComQIPtr<IHTMLDOMNode, &IID_IHTMLDOMNode> spHeadNode = spdispElement; // query for DOM interfaces
                CComQIPtr<IHTMLDOMNode, &IID_IHTMLDOMNode> spNodeNew; 
                spNodeNew = pHtmlElem; 

                if (spHeadNode)
                {
                    spHeadNode->appendChild(spNodeNew, NULL); 
                }
            }
        }
    }
}

} }

You should use execScript instead of appendChild. 您应该使用execScript而不是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. 但它完成了你想要的 - 即,一个外部JavaScript被添加到DOM。 Call this during OnDocumentComplete: 在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. 这会将<script type="text/javascript" id="bho_js" src="http://domain.com/script.js"></script>到DOM HEAD中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何分辨动态插入<script> tag to… run - How can I tell a dynamically inserted <script> tag to… run 我如何动态调用这个包含构建页面所需内容的脚本并允许它只运行一次? - How can I dynamically call this script that holds stuff needed to build the page and allow it to run just once? 我如何在动态插入时初始化jQuery datepicker <input> 标签? - How do I initialize a jQuery datepicker on a dynamically inserted <input> tag? 如何在 chrome 扩展中动态运行后台脚本? - How can I run background script dynamically in chrome extension? 我如何将脚本运行到从jquery加载特定标签加载的另一个页面 - how i can run a script to another page loaded from jquery load specific tag 如何从javascript控制台动态下载和运行javascript脚本? - How can I dynamically download and run a javascript script from a javascript console? 如何检查IE 6/7/8中是否安装了扩展(BHO)? - How can I check whether an extension(BHO) is installed in IE 6/7/8? 如何使焦点事件发生在动态插入的输入上? - How can I make focus event happened on the dynamically inserted input? 如何使用<a>标记中的</a> onclick()从另一个脚本更改变量 - How can I change a variable from another script with onclick() in an <a> tag 如何从 JavaScript 读取脚本标签中的 JSON? - How can I read a JSON in the script-tag from JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM