Chapter 15. Scripting and Automating Internet Explorer

The chapters in this book have thus far focused on IE development from the perspective of web pages, scripts, and extensions. These objects have an insider's view on how the browser operates as well as intimate access to its public API and state. Objects don't have to live inside IE to access its APIs, settings, and functionality, however.

In this chapter I will provide a quick overview of scripting IE through a number of methods provided with the Windows operating system. This is not a complete exploration of scripting; rather, my goal is to get you started with the IE automation object in various scripting languages, launching via thecommand line, and so on.

I begin by discussing IE's command-line parameters. Next, I delve into scripting using the Windows Scripting Host, PowerShell, and Windows Management Instrumentation, and provide some general guidance for all other systems that can access IE's automation interfaces, APIs, and system settings. Finally, I discuss how IE's APIs can be used outside of traditional scripting and application development.

Using IE with the Command Line

TheIE executable (iexplore.exe) can be run from the command line or otherwise executed through a system call with custom parameters. IE's command-line options allow for basic customization of a new process; IE's settings and feature controls, discussed later, offer finer-grained regulation of browser configurations.

Getting to Know the IE Command Line

Users, developers, and administrators can customize how IE should run when instantiated using a command prompt, a call to the shell, or an API such as CreateProcess or ShellExecute. The following command-line parameters represent those that are officially supported by IE 8:

iexplore.exe [-embedding] [-extoff] [-k] [-framemerging] [-noframemerging] [-private][<URL>]
  • -embedding: Creates IE without a user interface for OLE embedding

  • -extoff: Runs IE in No Add-Ons mode; turns extensions off for this IE instance

  • -k: Runs IE in Kiosk mode, a full-screen, reduced-UI frame

  • framemerging: Allows IE to opportunistically merge new frame processes into preexisting ones (the default)

  • -noframemerging: Prevents IE from merging the new page into an existing process

  • -private: Runs IE in InPrivate (private browsing) mode

  • <URL>: Targetsthe URL used for initial navigation

IE can be launched using all default settings and the user's home page(s) by omitting all parameters. Applications that plan to attach to an IWebBrowser2 instance or perform OLE automation functions can use the -embedding switch to create a new version of IE hidden from the visible desktop. The browser can be launched in a stripped-down mode where no add-ons are loaded (much like Windows' Safe mode) using the -extoff argument. IE can begin working in InPrivate mode through the use of the -private switch. The browser can be displayed in full-screen mode with no UI chromeby loading it in Kiosk mode via -k. The browser's interprocess coupling system (Loosely-Coupled Internet Explorer, or LCIE) can have its process-merging algorithm turned off or on through the use of -framemerging or -noframemerging. Finally, providing an URL after these switches (if present) will direct IE to load that URL as its focused startup page.

The command line has changed considerably since IE6; many removed switches had supported functionality present in older versions of the browser. The following parameters were deprecated as of IE7:

  • -channelband

  • -e

  • -eval

  • -new

  • -nowait

The following parameters were deprecated during the IE 8 development cycle; the -nomerge directive was replaced by -noframemerging.

  • -nomerge

  • -remote

  • -v

  • -version

Changing IE Registry Settings

The command line can be used to view andmanipulatesettings stored in the Windows registry, including settings used by IE.The REG application provides users with a simple means of reading, creating, modifying, removing, and even backing up registry settings.

Listing 15-1 shows an example of a command prompt entry that recursively displays the contents of the Main settings key for IE.

Example 15.1. Command for Displaying IE's Main Setting Registry Key Using REG.EXE

REG QUERY "HKCUSOFTWAREMicrosoftInternet ExplorerMain" /s

The output, shown in Figure 15-1, can be used for more than just reading; the output from this query could be redirected to a file or back into another command (such as a regular expression system) for further processing.

List of current IE settings for the current user from the Windows registry

Figure 15.1. List of current IE settings for the current user from the Windows registry

Entries can be modified in the same simple manner with this command.Listing 15-2 shows an example edit; the current user's home page is changed from its prior value to "http://www.bing.com". This is done by changing the "Start Page" string value located in IE's Main registry key.

Example 15.2. Changing a User's Home Page in IE's Registry Settings via the Command Line

REG ADD "HKCUSOFTWAREMicrosoftInternet ExplorerMain" /v "Start Page"
        /t REG_SZ /d "http://www.bing.com"

Developers should be aware that direct manipulation of the IE registry settings may not fall under supported scenarios by Microsoft. In fact, some types of direct manipulation may trigger action against your application. For example, an application that change the IE start page through direct registry manipulation not only violates the IE terms of use, it may also lead to this same applicationbeing classified as malware and blocked by Windows Defender.

Invoking IE APIs Using RunDLL32

The component set provided by IE provides a wide variety of APIs for use by developers. The previous sections and chapters have focused on the role of these APIs in executable, library, and script development. While not officially endorsed or supported by Microsoft, these APIs can be used by another key management entry point presented earlier in this chapter: the command line.

Windows provides a way for developers to invoke APIs from the command line. The RunDLL32 utility's purpose is to make libraryfunctions invocable from the command line. It executes specific APIs and passes arguments to them based on the arguments passed to the utility itself.

There are hundreds of APIs exposed by IE that are potential candidates for this type of usage. Listing 15-3 demonstrates loading the ClearMyTracksByProcess entrypoint of inetcpl.cpl, the Internet Settings Control Panel. This API is used to clear IE and add-on history, cookie, and cache information. It accepts one argument, a flag indicating what data should be cleared.

Example 15.3. Calling the ClearMyTracksByProcess API Through RunDLL32

rundll32.exe inetcpl.cpl,ClearMyTracksByProcess 255

This example uses a argument of 255, telling IE to clean out all history, cookies, cache, and passwords for the current user. Running this command does exactly that.

Calling an entrypoint via RunDLL32 is useful not only for calling noninteractive APIs but for launching IE-specific dialogs and settings panels as well. Listing 15-4 is a great example of this—it launches the Internet Settings Control Panel when run.

Example 15.4. Calling LaunchInternetControlPanel API in inetcpl.cpl via RunDLL32

rundll32.exe inetcpl.cpl,LaunchInternetControlPanel

Figure 15-2 shows this call in a command prompt and the result: an instance of the IE control panel on the user's desktop.

Resultant Internet Properties window from a call to LaunchInternetControlPanel

Figure 15.2. Resultant Internet Properties window from a call to LaunchInternetControlPanel

Like the call to the control panel before, the ResetIEtoDefaults brings up a dialog. The function is used in the same manner; in this case, the Reset IE Settings dialog is shown (Listing 15-5).

Example 15.5. Calling the ResetIEtoDefaults API in inetcpl.cpl via RunDLL32

rundll32inetcpl.cpl,ResetIEtoDefaults

Figure 15-3 shows the call's output: a window where users can reset their settings back to the installation or deployment defaults.

The Reset Internet Explorer Settings window displayed by calling ResetIEtoDefaults

Figure 15.3. The Reset Internet Explorer Settings window displayed by calling ResetIEtoDefaults

Microsoft has never provided official support for instantiating IE's DLL entrypoints in this manner (although there are numerous MSDN blogs that discuss different uses). Not all APIs and usages are documented, and for good reason; some were intended for use only within a product. Unsupported APIs are subject to change in any IE update, although in practice they typically only change with new versions of the browser.

Writing Basic Scripts for IE

IE's COM-based architecture lends itself nicely to reuse in a wide variety of ways. The previous chapters discussed, in detail, the use of IE's API set and objects within pages and as browser extensions. IE's functionality is not solely limited to web page designers or desktop application developers; savvy computer users can access these features using easily accessible scripting languages like VBScript and Jscript. Such scripts can access browser data and methods while running outside the browser itself.

IE exposes its objects to other applications and callersas an out-of-process COM server. Components can tap into this through COM automation, a system of controlling functionality by way of exposed IDispatch interface pointers. By invoking automation methods using the IDispatch interface, other applications can "drive" an IE instance.

Developers, system administrators, and power users can access IE and its objects by way of simple scripts written in a variety of languages. The following sections introduce simple methods of obtaining an IE automation object in a number of scripting languages and performing some basic tasks with each of those objects.

Creating IE Objects with the Windows Scripting Host

The Windows Scripting Host (WSH) is an application that interprets and runs scripts for languages supporting the ActiveScript interfaces. There are two languages shipped with Windows supported by WSH: Visual Basic for Applications (VBA or VBScript) and JScript. WSH makes it easy for script developers to access COM APIs of Windows applications and, more pertinent for this discussion, those public APIs and objects of IE. These scripts are run through the WSH executable cscript.exe.

The first step in using IE through WSH is getting a hold of the IE automation object. Developers can spin up COM objects using the CreateObject() function exposed by WSH. An instance of IE can be created by passing in the ProgID of IE's main application ("InternetExplorer.Application") to this function. The call returns a reference to a new instance of the object when successful; it references a null object on failure (Listing 15-6).

Example 15.6. Creating and Loading an IE Object in VBScript

'  Declare a variable to hold an IE object
Dim objIE

'  Grab an IE automation object
Set objIE = WScript.CreateObject("InternetExplorer.Application")

'  Navigate to a webpage
objIE.Navigate "http://www.bing.com"

'  Set the browser as visible
objIE.Visible = 1

The example in Listing 15-6 is VBScript that opens up a new instance of IE and points it to http://www.bing.com. First, the variable objIE requests a new IE instance by calling CreateObject using the ProgID"InternetExplorer.Application". Next, the script calls the Navigate function and passes in one parameter: the URL of the target site (Bing). It finishes by setting the object's Visible property to 1, unhiding the window and placing it on the interactive user's desktop.

The same procedure can be used when programming for WSH in JScript. Unlike VBScript, the CreateObject function is not available for use in Jscript. JScript instead provides a more restrictive ActiveXObject function to perform the same task. Listing 15-7 demonstrates this script.

Example 15.7. Creating and Loading an IE Object in JScript

//  Create a new IE automation object
var objIE = new ActiveXObject("InternetExplorer.Application");

//  Navigate to a URL for the main tab
objIE.Navigate("http://www.bing.com");

//  Show the browser window
objIE.Visible = true;

This sample follows the same code flow as previous example with VBScript. First, the IE object is created; the only difference in this case is the use of ActiveXObject instead of CreateObject. Next, navigation is performed by invoking the object's Navigate function. Last, the IE window is shown by setting the objects Visible property to true.

Creating IE Objects with PowerShell

IEautomation is not limited to WSH, VBScript, or even JScript. Any language that supports the loading and use of COM objects and COM automation can create and automate IE instances. To demonstrate this, Listing 15-8 is presented. This is a simple PowerShell script that performs the same actions as the last few scripts: it loads a new IE object, navigates to an URL, and displays the browser window.

Example 15.8. PowerShell Script Loading an Instance of IE and Navigating It to Bing

#  Grab a new IE object instance
$objIE = new-object -comobject "InternetExplorer.Application"

#  Navigate to an URL
$objIE.navigate("http://www.bing.com")

#  Set the browser window as visible
$objIE.visible = $true

Sinking Events Using VBScript and CreateObject

Scripts can receive notification of events raised by an automation object. WSH provides a super-simple way of doing this from VBScript: CreateObject, the same method used to spin up the IE instance in the first place. CreateObject accepts a second parameter called prefix, representing a string prefix used by any or all potential event handlers for an object. Unfortunately, there is no equivalent functionality provided in JScript.

Listing 15-9 shows an example of the CreateObject function being used with a string prefix. The prefix used is "objIE_", and it is applied to the start of a function name later the script.

Example 15.9. Sinking the OnQuit Event

'  Declare a variable to hold an IE object
Dim objIE

'  Grab an IE automation object
Set objIE = WScript.CreateObject("InternetExplorer.Application", "objIE_")

'  Navigate to a webpage
objIE.Navigate "http://www.bing.com"

'  Set the browser as visible
objIE.Visible = 1

'  Wait for the page to load
WScript.Echo "Keep this box open"

'  Event handler for the OnQuit event
Sub objIE_OnQuit()

    '  Show a message
    WScript.Echo "The user quit"

End Sub

The script begins by creating an object and telling CreateObject to call event handlers whose names match the concatenation of the provided prefix and the event name. For example, if the prefix provided was "objIE_", the IE object will call a function named "objIE_OnBeforeNavigate" when the object's OnBeforeNavigate eventis triggered.

The example continues by the script "pausing" itself by opening a dialog that a test user would not close; this puts the process into a wait state as that dialog awaits for user input. In the meantime, a function is defined at the end of the script, and its name is objIE_OnQuit. Since the IE object exposes an event called OnQuit and the provided prefix was "objIE_", this function will be called by IE on a new thread when the event is fired.

This script launches IE when it is run. The first dialog is kept open, which prevents the script from completing execution and shutting down before the browser events are raised. When the user closes the IE window launched by the script, a new dialog box appears indicating that the browser was closed.

Learning Common IE Scripting Techniques by Example

This chapter is not meant to be a deep dive into administering IE via script. Nonetheless, the power and simplicity provided by scripts is worth considering in many scenarios. At minimum, the following examples should be put in every developer's arsenal of software solutions.

Setting Basic Window Properties (VBScript)

TheIE automation object returned from CreateObject (or ActiveXObject in the case of JScript) exposes properties and methods related to the web site's container window (which could be properties of either a frame or a tab). Window settings can be applied once the object is created passed back to the originating script.

Listing 15-10 provides an example of a script that edits window-related properties exposed at the top level of the returned IE object.

Example 15.10. Setting Window Properties on an IE Object

'  Declare a variable to hold an IE object
Dim objIE

'  Grab an IE automation object
Set objIE = WScript.CreateObject("InternetExplorer.Application")

'  Navigate to a webpage
objIE.Navigate "http://www.bing.com"

'  Set document and window properties
objIE.ToolBar        = False
objIE.Resizable      = False
objIE.StatusBar      = False
objIE.Width          = 200
objIE.Height         = 200

'  Set the browser as visible
objIE.Visible = 1

The script begins as the others have, loading a new IE object and navigating to an URL. After the navigation, a number of settings on the window object are changed. The main IE toolbar is removed by setting the ToolBar value to False; the StatusBar is hidden by setting it to False; the Width and Height are defined in pixels; and the window is locked to size by setting Resizable to False (Figure 15-4).

Custom IE window object modified using VBScript

Figure 15.4. Custom IE window object modified using VBScript

Opening Multiple Tabs in a Single Window (JScript)

IE's command line does not accept multiple URLs for launching as multiple tabs.COM automation, however, does make this possible. The IE object exposes a navigation function named Navigate2 that allows a developer to optionally specify additional parameters than affect how a URL is loaded.

Listing 15-11 begins with some JScript that defines a constant openInBackgroundTabFlag (used later). A new IE object is created and Navigate2 is called with a single parameter (an URL) that makes an initial page load (Bing).

Example 15.11. JScript That Launches a New IE Window with Two Background Tabs

//  Define the background flag const for Navigate2
var openInBackgroundTabFlag = 0x1000;

//  Create a new IE automation object
var objIE = new ActiveXObject("InternetExplorer.Application");

//  Navigate to a URL for the main tab
objIE.Navigate2("http://www.bing.com");

//  Load other tabs in the background
objIE.Navigate2("http://www.yahoo.com", openInBackgroundTabFlag);
objIE.Navigate2("http://www.google.com", openInBackgroundTabFlag);

//  Show the browser window
objIE.Visible = true;

The script continues with two more navigations to both Yahoo! and Google. Unlike the first call to Navigate2, these calls use a second parameter called flags. The constant value openInBackgroundTabFlag is passed in that parameter, instructing IE to perform these navigations in new background tabs in the same browser window. The script displays the newly created window before finishing.

Accessing the Document Object and Finding All Links (VBScript)

The previous chapters of this book have used the WebBrowser control (and the IWebBrowser2 interface) to access objects relating to a webpage (such as a IHTMLDocument object instance). These objects may also be accessed using script, and oftentimes in a more straightforward manner.

This example demonstrates a script that reports information from an object using the IHTMLDocument interface. This object is exposed from and child to an IE object created through CreateObject. Developers using WSH need not know the real name of the document interface, its type, or the types and interfaces of its children.

The example begins by loading up Bing in a new instance of IE. It makes the new window visible. The script then waits for the window's ready state to report as "complete," represented by the integer 4. When the ready state signals complete, the script knows a page has been loaded into the object and that the object representing that page (the document object) is most likely available (Listing 15-12).

Example 15.12. JScript That Launches a New IE Window with Two Background Tabs

'  Declare a variable to hold an IE object
Dim objIE

'  Grab an IE automation object
Set objIE = WScript.CreateObject("InternetExplorer.Application")

'  Navigate to a webpage
objIE.Navigate "http://www.bing.com"

'  Set the browser as visible
objIE.Visible = 1

'  Wait for the page to load
Do
  Loop Until objIE.ReadyState = 4

'  Refer to the document object
Set objHtmlDocument = objIE.Document

'  Return the number of links on the page
WScript.Echo "There are " & objHtmlDocument.links.length & _
             " links on this page."

The objHtmlDocument variable stores a reference to the document when the page has loaded. The script proceeds to access information about the page. In this case, it grabs the number of URLs on thepage by looking at the number of entries in the Links collection property on the document. This value is read and reported back using a message box (Figure 15-5).

Script accessing child object (document) information on an instance of IE

Figure 15.5. Script accessing child object (document) information on an instance of IE

Setting a Home Page Using Windows Management Instrumentation (VBScript)

Earlyparts of this chapter used IE's registry settings as an example of items that could be changed and edited using command-line applications. While those command lines are pretty easy to use, they can also be inadequate, inconvenient, or impossible to use based on the configuration of a computer or a computer's domain. Enterprises tend to seek more robust solutions that provide a more organized, deliberate, predictable functionality.

The Windows Management Instrumentation (WMI)system and API was created to be a happy medium between the simplicity of direct registry modification and the robustness provided by true Win32 applications with broad API access. The result was a thorough yet simple, robust yet scalable solution that turns system information into predictable, organized datasets easily managed by scripts. WMI APIs enable automation of a broad range ofadministrative tasks. WMI makes managing IE through script simple.

Listing 15-13 demonstrates WMI being used for something pretty simple: loading a registry object and changing a browser setting.

Example 15.13. Using WMI to Set the User's Home Page

'  Store values or this script
HKEY_CURRENT_USER = &H80000001
strComputer = "."
strKeyPath = "SOFTWAREMicrosoftInternet ExplorerMain"
strValue = "Start Page"
strData = "http://www.bing.com"

'  Get the registry provider for the local machine from WMI
Set objReg = GetObject("winmgmts:\" & strComputer & _
             "
ootdefault:StdRegProv")

'  Ensure the target key exists (fails gracefully)
objReg.CreateKey HKEY_CURRENT_USER, strKeyPath

'  Set the value and data to the target key
objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, _
   strValue, strData

Unlike the other examples, this one uses a GetObject call not into IE, but rather into the WMI back-end management system. The objReg variable is permitted to access the HKEY_CURRENT_USER hive. When access is achieved, the object writes to the IE settings key.

Summary

The flexibility of IE's command line, COM automation system, and rich API set helps developers, system administrators, and enthusiasts to perform common tasks with batch files and simple scripts. Such scripts can be used to streamline installs across a business, customize the browser, and simplify common management tasks.

In this chapter I provided an overview of techniques used to manage and operate IE through the shell and simple script. I began with an introduction to the command line and the features it provides, followed by a short foray into the little-known but very useful invocation model offered by RunDLL32. Scripting was next, and it was there that I described how you can access a simple-to-understand object model exposed by IE, WSH, PowerShell, and any other language that supports COM automation. I closed by providing a few examples of different tasks the IE object allows you to perform, and other systems, such as WMI, that play nicely with it. If you haven't already, I hope this overview inspires you to look into scripting as a powerful and everyday development tool.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.188.16