如何运行我刚从BHO中动态插入的

I'm completely new to developing IE extensions with 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); 
                }
            }
        }
    }
}

}

}

1 个解决方案

#1


12  

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:

您应该使用execScript而不是appendChild。你需要exec的语法非常,非常的wierd。但是它实现了您想要的——也就是说,向DOM添加一个外部JavaScript。在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.

这将添加 <脚本类型="text javascript" id="bho_js" src="http://domain.com/script.js"> 到DOM HEAD。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/04/28/33c935479b8ccc9cda093bab83762ed4.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告