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 javascript file in the head of the HTML page (see code below).
But the script tag just sits there in the DOM and the external javascript file is not executed.
Is there any way to tell the browser to run the external javascript file?
Thanks!
Code Details: I call the following method on the OnDocumentComplete event:
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);
}
}
}
}
}
}
The “script” tag You can run the example by clicking the “Play” button in the right-top corner of the box above. The <script> tag contains JavaScript code which is automatically executed when the browser processes the tag.
Dynamic loadingCreate a script element. Set the src , async , and type attributes. Append the script element to the body. Check if the file loaded or not in the load event.
We start by creating an empty <script></script> tag in the memory as script and then assign the necessary attributes to its src and the id to identify the script later. Finally, we append the script to our <body></body> tag to actually load this.
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With