Controlling an Internet Explorer Session from a JScript

Let us say that you have some product specifications stored on your intranet, and your paranoid network administrator requires authentication – the PNA was kind enough to provide a generic read only login.

You want to make life easy for your users, from MMS001 you want to press a button which will launch IE so you can see the product specifications seamlessly – automatically logging in. In this example, I am purely going to provide the authentication information, but you could potentially have the site choose a specific product or whatever you want.

One of the nice things about Internet Explorer is that it is just like MS Office, it’s COM enabled, so we can launch and control it through scripts or external programs.

In order to provide the log in information we need to determine the input box names for the Username and Passwords. You would also want to retrieve the Login button aswell in a real world example.

So below is what the login page looks like

Taking a look at the markup for this page, we see that the Username has an id of Login1_UserName. Password is Login1_Password.

And the script itself

import System;
import System.Windows;
import System.Windows.Controls;
import MForms;

package MForms.JScript
{
	class ieTest
	{
		public function Init(element: Object, args: Object, controller : Object, debug : Object)
		{
			 var content : Object = controller.RenderEngine.Content;
			 
			 // create the com object
			 var IEApp = new ActiveXObject("InternetExplorer.Application");
			 // make sure it is visible
			 IEApp.Visible = true;
			 
			 // navigate to the website (be aware, it may take time to load!)
			IEApp.Navigate("https://go.potatoit.com/potatoit/view/DocumentView.aspx?libraryProcessGroupId=-1&tagId=-1&specialCase=AllTag");
			
			// retrieve the document with all of the controls and content
			var doc = IEApp.Document;
			// find and populate the password field which we determined was called "Login1_Password"
			doc.getElementByID("Login1_Password").value = "potatoit" 
			// find and populate the user field which we determined was called "Login1_Username"
			doc.getElementByID("Login1_Username").value = "potatoit" 
		}
	}
}

Be aware that if you transition through different IE security zones and you have Protected Mode on, you won’t have access to the markup document in IE, so you won’t be able to submit your information.

About these ads
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Controlling an Internet Explorer Session from a JScript

  1. Hi Scott, nice post! The technique reminds me of Greasemonkey for Firefox. Also, for the readers would be interested in setting the user and password directly in a URL, we can send any key/value pairs as parameters of an HTTP GET Request with LaunchTask or with ScriptUtil.Launch, for example: DashboardTaskService.Manager.LaunchTask(new Uri(“https://hostname/path?user=potatoit&password=potatoit”)) or ScriptUtil.Launch(“https://hostname/path?user=potatoit&password=potatoit”). LaunchTask will open a window inside of Smart Office whereas ScriptUtil.Launch will launch the operating system’s default URI handler, usually Internet Explorer. (Google Chrome in my case). That reduces the lines of code. Also, one can send parameters in an HTTP POST Request with an HttpWebRequest. Finally, a user can also create by themselves Links, Shortcuts, and use the Link Manager thus avoiding the need for a script. Thanks!

    • potatoit says:

      You’re 100% right, definitely take the KIS approach (Keep It Simple). If you don’t need fine grained control – like needing to process information on the page or potentially navigate multiple pages, then the solutions you have listed are a far better alternative…well…unless you have masochistic tendencies ;-)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s