cutting@microsoft.com Download the code (13KB) |
Dino Esposito |
Customizing Microsoft Internet Explorer 5.0 |
There is just one aspect missing from all these pre-Internet Explorer 5.0 programming goodies: you couldn't associate new functionality with the browser's user interface. You had no way to add new toolbar buttons or to customize any of the menus. Thankfully, this problem has been solved with Internet Explorer 5.0. Let's look at the new ways you can interface with the Internet Explorer UI, the points at which you're allowed to intervene, and what you can actually obtain through code. I'll briefly review the highlights of BHOs, focusing on scripting-related aspects of Internet Explorer 5.0 customization and registry changes. Windows Script Host (WSH) will be my development environment of choice here.
Customizable Aspects of Internet Explorer 5.0
Customization Techniques
|
Figure 2: Available Deskbands |
Writing a custom deskband is neither painfully difficult nor particularly easy. A few examples can be found in the latest Platform SDK.
Adding a New Toolbar Button
|
Figure 4: The New Run Script Toolbar Button |
There are two categories of information to be added for a new button: configuration and behavior. Figure 5 shows the entries that describe the button. All the entries mentioned here are strings (type REG_SZ). Notice that if Default Visible is set to No, the button won't appear on the toolbar, although it will still be available through the toolbar's Customize dialog. If the user has already customized the toolbar, the button will not appear on the toolbar automatically no matter the value of Default Visible.
The Clsid entry can take two possible GUIDs as values:
and
Use the first GUID if you're attaching a deskband to the button, otherwise use the second one.
These entries must all be written to the following root key:
Under this path, however, you have to create a new key with a unique GUID. You can use GuidGen.exe to generate a new one. Incidentally, this is exactly what I did to come up with the GUID in Figure 3. Notice that as long as you use the HKEY_LOCAL_MACHINE root node, the button will be visible to all users on the specified machine. If you want it to apply only to certain users, enter the registry changes under the HKEY_CURRENT_USER node instead.
These changes are sufficient to make a new button appear on the Internet Explorer toolbar. The icons must include two formats each: 16X16 (16 colors) and 20X20 (256 colors). You can create these icons with the integrated resource editor of Visual Studio®. To give the button an associated behavior, you have three options: running an executable, running a script or an HTML page, or invoking a command defined in some COM component implementing the IOleCommandTarget interface. Let's take a look at what you have to add to the registry in each case. To spawn an external program you need to add a further value, Exec, whose content is the fully qualified name of the executable. My tests indicate that you can't specify a command-line argument. For example, works fine, while
doesn't, even if you surround the whole thing with quotes.
If your goal is to run a Web page or a script file, then you have to change the entry. Use Exec for a Visual Basic® Scripting Edition (VBScript) or JScript script, and use Script for an HTML page. There are some subtle differences between these two options. Exec means that the specified file is executed through the program registered to manage that type of file. If you use the Exec entry with an HTML file, you force the default browser (in most cases Internet Explorer 5.0 itself) to navigate to that page. If you use Exec with a .vbs file, you simply end up executing that codeprovided that you have WSH properly installed. Running a VBScript or a JScript file as a standalone executable doesn't let you access the current instance of the browser and the object model exposed by the currently viewed document. If you want to use these objects, you must resort to using an HTML file that contains only script code and run it through a Script (not Exec) entry. For example, this code adds "HELLO, MIND!" to the current page when you click the button.
Using the Script entry and a script-only HTML file allows you to use the External object to access the document's object model. If you put any decorative tags in this HTML file, they will be ignored. Finally, you can establish a link between an Internet Explorer 5.0 toolbar button and a COM object via the ClsidExtension entry. Its content is the CLSID of the COM object implementing the IOleCommandTarget interface. If you have a deskband, use the BandClsid entry instead of ClsidExtension.
Accessing the Document Object Model
A script that wants to access the DOM can do something like this: From now on, myPageObj can be used to access the various items that form the DHTML object model. For example, you could enumerate all the links in a page:
Figure 7 shows the final result. |
Figure 7: Enumerating Links in a Page |
Modifying the Menus
You can also modify the Internet Explorer 5.0 menu without affecting the toolbar. As mentioned earlier, each toolbar button has a corresponding command in the Tools menu. However, this is only true in the default case. In other words, a new command goes under the Tools menu unless you specify otherwise. Unfortunately, you can't add the menu item wherever you want. You have just two choices: you can add it under the Help menu or the Tools menu. By default, it goes under Tools unless you create another string value called MenuCustomize when you're setting up the registry entries. Set MenuCustomize to "Help" and the menu item will appear under the Help menu. Set it to any other string (say, "File") and the menu item will appear under the Tools menu. When you specify the text for the item (its value is MenuText), you can't specify your choice of underlined character to denote a keyboard shortcut. This is done as a precaution so that you don't introduce a conflict.
The Context Menus
However, if you assign a procedure to the oncontextmenu property, you can decide at runtime whether to display a menu and, if you do, which one should appear. You can also build your own menu on the fly, as demonstrated in the Platform SDK example.
If you don't mind all the standard context menu items, but simply want new and more specific items, describe them in a registry subtree located under:
Each subkey of this node will be considered a new item for any context menu. Notice that now you must work under HKEY_CURRENT_USER. For example, you could create a key called "Do Something…" and set its default value to the following HTML page:
From now on, any context menu of Internet Explorer 5.0 includes a "Do Something…" item, which prompts you with the title of the current page. In Figure 8, you can see how the context menu for both images and selected text show off a brand new item.
|
Figure 8: A New Context Menu Item |
As for toolbar buttons, the HTML code associated with the command will be run in a hidden dialog box that is passed the parent window object through the external.menuArguments property.
You can also limit the number of context menus where a given command appears. To do so, add another value and name it Contexts. Note that it must be a DWORD value. Assign it any combination of the hex values listed in Figure 9. For example, a value of 0x12 will cause the new item to appear only on the Image and Text context menu, as shown in Figure 8.
Related Applications
|
Figure 10: Internet Explorer-related Programs |
Let's see how to make other applications available. Five out of six categories can be managed through this registry node:
These categories include: Mail, News, Internet Call (conferencing), Calendar, and Contacts. To define a new mail reader (even a dummy entry), create a new node under the Mail key and duplicate the settings you can see for the other entries. Figure 11 shows the result.
|
Figure 11: Registry Settings for a New Mail Reader |
The remaining category of Internet Explorer 5.0 applications is HTML editors. If an HTML editor is made available, it must first be configured elsewhere in the registry. The key to look for is:
Under this node, you can create a new key for any new program that processes HTML file names on the command line. Take note of the OpenWithList keyit's a sneak preview of a shell feature that's scheduled to ship with Windows 2000. If you're already running a prerelease version of Windows 2000, chances are you already noticed that some file types have dropdown menus in their context menus under the Open With item, allowing you to select from multiple programs to open the file type. All the items shown in this list are taken from the OpenWithList registry key.
Deploying the Customization
Summary
|
From the November 1999 issue of Microsoft Internet Developer.