<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>the kiZZ notes &#187; browser helper object</title>
	<atom:link href="http://kizzx2.com/blog/index.php/tag/browser-helper-object/feed/" rel="self" type="application/rss+xml" />
	<link>http://kizzx2.com/blog</link>
	<description>random stupid ideas from a narcissistic chode</description>
	<lastBuildDate>Thu, 01 Dec 2011 16:25:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>making an Internet Explorer add-on</title>
		<link>http://kizzx2.com/blog/index.php/2008/05/01/making-an-internet-explorer-add-on/</link>
		<comments>http://kizzx2.com/blog/index.php/2008/05/01/making-an-internet-explorer-add-on/#comments</comments>
		<pubDate>Thu, 01 May 2008 13:37:02 +0000</pubDate>
		<dc:creator>kiZZ kiZZ</dc:creator>
				<category><![CDATA[drumming]]></category>
		<category><![CDATA[geek stuffs]]></category>
		<category><![CDATA[add-on]]></category>
		<category><![CDATA[BHO]]></category>
		<category><![CDATA[browser helper object]]></category>
		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://kizzx2.com/blog/?p=45</guid>
		<description><![CDATA[I was recently doing a project to make an add-on for Internet Explorer. How do I put it? I must say that was an extremely frustrating experience. The documentation is severely lacking, if not non-existent. One page from MSDN (which states the psarray is a BSTR is even dead wrong). I have never been more [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently doing a project to make an add-on for Internet Explorer. How do I put it? I must say that was an extremely frustrating experience. The documentation is severely lacking, if not non-existent. <a href="http://msdn.microsoft.com/en-us/library/aa752639(VS.85).aspx">One page from MSDN</a> (which states the <code>psarray</code> is a <code>BSTR</code> is even dead wrong).</p>

<p>I have never been more motivated to contribute to the scene by writing my own &#8220;Internet Explorer add-on writing survival guide&#8221; sort of thing, but then I realized <a href="http://petewarden.typepad.com/">Pete</a> has beat me to it by opening an entire [bho-wiki][wiki].</p>

<p>Nevertheless, I&#8217;m writing this article so more people can find these scarce information on IE add-on. On the other hand, here are some thoughts I had when writing the IE add-on. If you&#8217;re about to write an IE add-on, these would probably be the &#8220;survival guides&#8221; you&#8217;re using (as there aren&#8217;t many).</p>

<p><strong>1. An IE add-on is a Browser Helper Object (BHO)</strong></p>

<p>To save you some time searching in vain, try using BHO in your Google searches. This would give you programming-related information instead of where to download the latest IE add-on product.</p>

<p><strong>2. Use C#</strong></p>

<p>C++ just plain sucks, in terms of writing an IE add-on. Don&#8217;t get me wrong here, the language is marvelous, but Microsoft&#8217;s support just seems broken and unfinished. The whole Win32 hacking, COM stuffs involved when using C++ just makes your life 10 times more difficult.</p>

<p>The interface for C# is much more usable, and you won&#8217;t have to deal with all the bullshits when programming in the outdated (and most probably unmaintained) Win32 API.</p>

<p>Another major issue with C++ is Visual C++ 2008 Express. It doesn&#8217;t come with ATL, which means a ridiculous amount of grunt work dealing with pointers, releasing and stuffs (as illustrated below, as I happened to use VC++ Express). VC# doesn&#8217;t have this problem and it even has a number of libraries built-in (such as regular expressions).</p>

<p><em>Example 1: getElementById from BHO</em></p>

<p><em>Using C++</em></p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">IHTMLCollection <span style="color: #000040;">*</span> elements<span style="color: #008080;">;</span><br />
HRESULT hr <span style="color: #000080;">=</span> document<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>get_all <span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>elements <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">long</span> numElements<span style="color: #008080;">;</span><br />
hr <span style="color: #000080;">=</span> elements<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>get_length <span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>numElements <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
IHTMLElement <span style="color: #000040;">*</span> element<span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> numElements<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
VARIANT index<span style="color: #008080;">;</span><br />
index.<span style="color: #007788;">vt</span> <span style="color: #000080;">=</span> VT_I4<span style="color: #008080;">;</span><br />
index.<span style="color: #007788;">intVal</span> <span style="color: #000080;">=</span> i<span style="color: #008080;">;</span><br />
<br />
IDispatch <span style="color: #000040;">*</span> elementDispatch<span style="color: #008080;">;</span><br />
<br />
hr <span style="color: #000080;">=</span> elements<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>item <span style="color: #008000;">&#40;</span> index, index, <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>elementDispatch <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
hr <span style="color: #000080;">=</span> elementDispatch<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>QueryInterface <span style="color: #008000;">&#40;</span> IID_IHTMLElement, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">**</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>element <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">assert</span> <span style="color: #008000;">&#40;</span> SUCCEEDED <span style="color: #008000;">&#40;</span> hr <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
elementDispatch<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// After all this hard work, we have finally obtained our element!</span><br />
<span style="color: #666666;">// GetAttribute is a custom function, I didn't even bother to</span><br />
<span style="color: #666666;">// list out it's content here</span><br />
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> GetAttribute <span style="color: #008000;">&#40;</span> element, L<span style="color: #FF0000;">&quot;id&quot;</span> <span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> id <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> element<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> element <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
elements<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>Release<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">return</span> element<span style="color: #008080;">;</span></div></td></tr></tbody></table></div>

<p><em>Using C#</em></p>

<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">document<span style="color: #008000;">.</span><span style="color: #0000FF;">getElementById</span><span style="color: #008000;">&#40;</span>id<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></td></tr></tbody></table></div>

<p><em>Example 2: execute some Javascript</em></p>

<p><em>Using C++</em></p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">BSTR code <span style="color: #000080;">=</span> SysAllocString <span style="color: #008000;">&#40;</span> L<span style="color: #FF0000;">&quot;alert ( 'Hello World' );&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
BSTR language <span style="color: #000080;">=</span> SysAllocString <span style="color: #008000;">&#40;</span> L<span style="color: #FF0000;">&quot;javascript&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
VARIANT result <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
window<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>execScript <span style="color: #008000;">&#40;</span> code, language, <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>result <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
SysFreeString <span style="color: #008000;">&#40;</span> code <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
SysFreeString <span style="color: #008000;">&#40;</span> language <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>

<p><em>Using C#</em></p>

<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">window<span style="color: #008000;">.</span><span style="color: #0000FF;">execScript</span> <span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;alert ( 'Hello World' )&quot;</span>, <span style="color: #666666;">&quot;javascript&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></td></tr></tbody></table></div>

<p><strong>3. Use jQuery for DOM manipulation</strong></p>

<p><a href="http://jquery.com">jQuery</a> is a client side JavaScript library to simplify common tasks (the &#8220;write less, do more&#8221; library as they say). I found it delightfully pleasant to use. It has some powerful functions like CSS selectors that would be a pain in the ass to implement in BHO. With jQuery, you can do stuff like:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Find all the &amp;lt;a&amp;gt; elements with class &quot;bookmark&quot; and</span><br />
<span style="color: #006600; font-style: italic;">// target attribute &quot;_blank&quot; and hide</span><br />
<span style="color: #006600; font-style: italic;">// them smoothly using a slow slide up animation</span><br />
<br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.bookmark[@target=&quot;_blank&quot;]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>To use jQuery, simply use <code>execScript</code> to load the jQuery file on document complete.</p>

<p><strong>4. About XmlHttpRequest
</strong></p>

<p>Fetching external Web pages using client side JavaScript will give you a permission denied error because of the cross-domain policies of most modern browsers. XmlHttpRequest must be done in the BHO. <a href="http://www.ookii.org/">Sven</a> offers <a href="http://www.ookii.org/post/using_ixmlhttprequestonreadystatechange_from_c.aspx">an example code</a> to use IXmlHttpRequest, which is very valuable considering the lack of official documentation. Pete has rolled his own <a href="http://petewarden.typepad.com/searchbrowser/2007/06/xmlhttprequest__1.html">CPeteHttpRequest</a> class using WinHttp.</p>

<p>(Strangely enough, both of the above are coded in C++. I have yet to come across an equally efficient way to do XmlHttpRequest in C#. Please feel free to leave a comment if you have good ideas)</p>
]]></content:encoded>
			<wfw:commentRss>http://kizzx2.com/blog/index.php/2008/05/01/making-an-internet-explorer-add-on/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

