Codeguru Forum Search Accelerator for IE 8

Contents

Introduction

Accelerators are a cool new feature in IE 8. They are contextual menu items which can be used to get some quick information based on the context. What makes them interesting is that, they are very easy to write and easy to install. Another advantage is, there is no code involved and a small amount of XML gets the job done. More information on accelerators can be found here:
OpenService Accelerators Developer Guide

About the Article

In this article, am not going to be discussing things that are already detailed in the aforementioned reference, but, am going to detail an approach I took to add my own Codeguru forums and Codeguru articles search accelerators. I hope this will help appreciate how easy it is to write one for your own needs and hope the Codeguru accelerators itself is of some use to you folks.

To write an accelerator , all you need is Notepad, since as I mentioned before, all you are doing is composing an XML file.

To install an accelerator in IE 8, though, is a different matter. Accelerators can be installed only from an add-on gallery or from the website which the accelerator itself references in it's homepage URL itself. So, installing from a locally present XML file is out of question, unless you are a developer who has Visual Studio or are running a web server yourself. We shall be employing the Visual Studio approach here.

Basic structure of an accelerator

In it's most basic form, an accelerator has
  • a textual entry in the context menu of IE 8 which summarises the accelerator functionality in a concise manner
  • an optional icon alongside it that can help visually identify the accelerator at quick glance
  • an action that it takes on selecting the menu option
If you want to get a bit more fancy, there is a possibility for a preview window. In this article, we won't be concerned with the preview window and I will leave it as an exercise for you folks to figure it out.

Let's now get to work on the basic structure. I'll call the accelerator Codeguru Forums Search. Let us associate it with Codeguru site icon. Fire up notepad and type in the following contents into it.

<?xml version="1.0" encoding="UTF-8"?>
<os:openServiceDescription
    xmlns:os="http://www.microsoft.com/schemas/openservicedescription/1.0">
    <os:homepageUrl>http://www.codeguru.com</os:homepageUrl>
    <os:display>
        <os:name>Codeguru Forums Search</os:name>
        <os:description>Codeguru Forums Search</os:description>
	<os:icon>http://www.codeguru.com/favicon.ico</os:icon>
    </os:display>
    <os:activity category="Search">
        <os:activityAction context="selection">
	    <os:execute action="http://www.codeguru.com" method="get">
	    </os:execute>
        </os:activityAction>
    </os:activity>
</os:openServiceDescription> 
Save this as, say, CGForumsSearch.xml and most important, make sure you set the encoding type as UTF-8. The XML is self explanatory for the most part, especially the display section, which simply has information on what IE should show for the context menu text as well as the icon. The activity part needs a little more explaining though. The category is what is used to tell IE what group this accelerator is to appear under. You can give any name you choose, if there is one with that name that already exists, this accelerator will appear under it alongside the others in the same group, else IE will create a new group for this.

To make it logical, name the category Search. The activityAction section needs to specify the context under which this accelerator appears. You can pick from three choices as mentioned on MSDN documentation. In our case, you want the accelerator to appear on selection of text on the page, so set the context as selection. The execute tag needs to specify an action that has to happen when the menu item is clicked/selected. For now, just set it to navigate to www.codeguru.com. Note, this action can only happen on the same server as the one that is specified in the homepageURL tag. If there is a mismatch between the two, the accelerator cannot be installed.

Installation of the accelerator

Installation of an accelerator can be done only from the website that hosts the accelerator XML file or from a gallery that I mentioned before. The one other way for developers of accelerators to install is to serve the XML file locally through ASP.Net server or IIS. We shall use the former approach.

Launch Visual Studio. Select "New Project" and select "Other Languages" and on the right hand side select ASP.Net Web Application. Type in any name for the project and then click Ok. You should be presented with a default.aspx in the editor with some HTML. Navigate to the folder where the default.aspx resides and copy your CGForumsSearch.xml file to this location.

Now, in the Visual Studio editor window for default.aspx, replace the contents of the body section with the following:

<body>
    <button id="myButton"
    onclick="window.external.AddService('http://localhost:1709/CGForumsSearch.xml')">
    Add Codeguru Forums Search Accelerator to Internet Explorer 8</button>
    <br>Now, for some text to look for in Codeguru forums, say, Accelerators.</br>
</body>

Build and Run by hitting F5. You should now see a page with a button. Click on it. You should be presented with a confirmation to install the accelerator like below:

Click on Add and if all is well, the accelerator should be installed. Now, any word on the page. An arrow icon should appear immediately. Click on it will popup a menu and if you drill down into All Accelerators, you should see "Codeguru Forums Search" listed there. Clicking on it will open a new tab in IE8 bringing up Codeguru main page. The reason being that is what we specified as the action in the XML file. All is well and you have the basic structure ready and good to go.

Adding support for Codeguru Forums search

Let's now get into the specifics. For sure, it is worthless to launch codeguru main site. What you need is a way to highlight text on any page and run it through Codeguru Forums Search. To be able to do that, you need to be find out what URL to use for the execute action as well as the parameters to pass in. The way I figured it out is as follows. I navigate to the following URL: http://www.codeguru.com/forum/search.php and then punch in a keyword, say, WinHTTP. I then click on "Search now". The results show up after a few seconds. Herein, right click on the Search link and select Copy Shortcut from the context menu.

Paste it over to notepad. It looks something similar to this:

http://www.codeguru.com/forum/search.php?query=WinHTTP&exactname=1&starteronly=0&forumchoice[]=0


There is a lot of stuff in there, but the interesting part is the query string passed in the very beginning which has the keyword used in the search. So, perhaps, for a basic search, it suffices to pass in the portion shown below to execute the search:

http://www.codeguru.com/forum/search.php?query=WinHTTP

The only thing you need to do now is to replace WinHTTP with variable text at runtime which will be the text that is selected on the browser page. Open up the CGForumSearch.xml and modify the activity section as below:
<os:activity category="Search">
        <os:activityAction context="selection">
	    <os:execute action="http://www.codeguru.com/forum/search.php" method="get">
		<os:parameter name="query" value="{selection}" />
		<os:parameter name="do" value="process" />
	    </os:execute>
        </os:activityAction>
</os:activity>

Note the change in the action URL as well as addition of parameter tag. The parameter tag holds the key value pairs that are passed along with the GET request. For multiple parameters, you would simply add another os:parameter tag with the new key, value data. Save the XML file, and again, hit F5 on Visual Studio to launch the test page. Click on the button again. This time, since the accelerator is already installed, you will be prompted to replace it. Just hit Replace.

Double click the word Accelerators to select it. Click on the arrow button and drill down to All Accelerators and select the Codeguru Forums Search accelerators from the list. In a few seconds, you should be taken to a new tab with the results for keyword Accelerator in Codeguru Forums.