WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with MSIE
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

HTML Extender Replacement code


I went thru the source code for the HTML extender and expected to discover the secret way the C code was determining that a button on the browser had been pushed and I was going to somehow translate that into Winbatch code. My surprise was that the C source did no such thing, it just simply looked to see if the URL was blank, much like the HTML Dialog help file. Marty had once mentioned to me that working with the browser wasn't too arcane and of course he was right. So like good little Borg I adapted and it lead me to inspect the .locationURL property and I found that when an HTML form/page is submitted locally the text "ignored" appears in the .locationURL, and it's that simple. As the saying goes, the rest is...

I had to modify a few functions slightly and I added some PRIVATE FUNCTIONS which the code uses and are transparent to the coder.

hBrowse() -- no longer closes the window (see above) and mimics *most* of the original's functions. Try it and see what works for you.

New is hEndBrowse() -- which closes the browser (yes you could simply issue the .quit method, but this helps the non-MSIE/OLE people a bit).

hGetURL() -- a function which examines the current URL of the browser to check if it's valid. This was necessary to make sure the page was still part of the document, so that the variable list and they're corresponding values weren't lost if the document closed before the "inventory" was complete (which happened constantly at first during testing).

hGetVarNames() -- roughly the same as the original, behind the scenes -- much different.

hGetVarValue() -- roughly the same as the original, behind the scenes -- much different.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;	You don't have to use forms on the page anymore since these functions
;	"inventory" the entire page for input objects. Make sure you NAME each
;	object though with the standard: name="MyName" so that your variable gets
;	recorded. AVOID using the same name multiple times.
;
;	unchecked checkboxes will not show up on the hVarNames, so if you query
;	one for the value, you'll get the default value you specify returned to you.
;	see below. also read the comments inside the function hProcessHTML().
;
;	some variables can return an empty string "", such as TEXTAREAs that are
;	left blank. this is different than a variable that is NOT THERE, so these
;	WON'T RETURN A DEFAULT VALUE.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;	Notes: I had to use text files since functions kept variables separate
;	and I wanted to keep as much as possible TRANSPARENT to the user.
;	I tried using WSC but that would force a combination of #INCLUDE with
;	these functions plus using the WSC file, which seemed a waste. I s'pose
;	a better option would be a WSC file with it all in there, but I prefer
;	to code in Winbatch where possible. :)

;	BTW -- file paths are hardcoded since using DIRGET() is problematic
;	due to the fact that the script could change folders or drives and the
;	functions would then miss data. Same with using INI files. This way everything
;	is contained in 1 file.


;The page you are looking for is currently unavailable
;. The Web site might be experiencing technical difficulties, or you may need to adjust your browser settings.

#DefineFunction hBrowseHidden()
;	first see if the data file is there, if not exit...
	hpath = "c:\Program Files\HTMLExtender\"
	filename = strcat(hpath, "hdata.txt")
	Terminate(!fileexist(filename), "Debug", "hBrowseHidden...Browser Data File Missing.")
;	read the data, paint the hidden screen...
	html = ""
	ih = fileopen(filename, "read")
	while @true
		line = fileread(ih)
		if line == "*EOF*" then break
		html = strcat(html, line)
	endwhile
	fileclose(ih)
	Browser = ObjectOpen("InternetExplorer.Application")
	browser.visible = @false
	url = "C:\Test\Blank.html"
	browser.navigate(url)
   WaitForpageLoad()
	;	setup the document object...
	browserDoc = Browser.Document
	browserdoc.writeln(html)
	objectclose(browserdoc)
	;	get a list of variable names and their values...
	hvarnamelist = hProcessHTML(browser, 1)
	hvarvaluelist = hProcessHTML(browser, 2)
	filename = strcat(hpath, "hvarnamelist.txt")
	oh = fileopen(filename, "write")
	filewrite(oh, hvarnamelist)
	fileclose(oh)
	filename = strcat(hpath, "hvarvaluelist.txt")
	oh = fileopen(filename, "write")
	filewrite(oh, hvarvaluelist)
	fileclose(oh)
	objectclose(browser)
	return
#EndFunction

#DefineFunction hEndBrowse(browser)   ;, html)
;	close the browser and the OLE object...
	browser.quit
	objectclose(browser)
;	setup a hidden browser and write out the last snapshot of data...
	hBrowseHidden()
#EndFunction

#DefineFunction hGetURL(browser)
;#DefineFunction hGetHTML(browser)
;	an extra function usuable if you for some reason need to save the page's HTML,
;	it will save the changes made by the user...(if any)...I used this for debugging...

;;;;;	if !hGetURL(browser) then return		; <--- check the URL and make sure it's valid
;	I was forced to keep the above line commented otherwise I'd get the NESTING OF 
;	STRUCTURES TOO COMPLEX error message.

;	setup a reference to the browser's document...
	browserdoc = browser.document
;	retrieve the HTML collection...
	bcoll      = browserdoc.getElementsByTagName("HTML")
;	reference the first member of the collection...
	bitem      = bcoll.item(0)
;	reference the actual HTML...
	bHTML      = bitem.outerHTML		; <--- use OUTER html to get everything...
;	close the OLE objects...
	objectclose(bitem)
	objectclose(bcoll)
	objectclose(browserdoc)
;	
	if !strindexNC(bHTML, "The page you are looking for is currently unavailable", 1, @fwdscan) && strreplace(bHTML, @crlf, "") <> ""
		hpath = "c:\Program Files\HTMLExtender\"
		filename = strcat(hpath, "hdata.txt")
		oh = fileopen(filename, "write")
		filewrite(oh, bHTML)
		fileclose(oh)
		return(1)
	else
	   return(0)
	endif
#EndFunction

;#DefineFunction hGetURL(browser)
;;	check to see if the current locationURL contains the error message
;;	from a non-existant page...
;	return(hGetHTML(browser))
;#EndFunction

#defineFunction hSetupEnvironment()
;	this is a private function called when the first hBrowse is used...
	hpath = "c:\Program Files\HTMLExtender\"
	if !direxist(hpath) then dirmake(hpath)
	return
#endFunction

#DefineFunction hBrowse(action, url, flags)
;	make some preliminary checks for correct arguments...
	Terminate(url == "", "Debug", "No URL...exiting program.")
	Terminate(action < 1 || action > 2 || !isnumber(action), "Debug", "hBrowse...ACTION argument invalid.")
	; set up the environment to handle the data...
	hSetupEnvironment()
	select @true
		case action == 1
;	start the normal browser...
			hbr = hStartMSIE(url)
			break
		case action == 2
;	start the browser, but remove navigation buttons if the "2" flag is specified...
			hbr = hStartMSIE(url)
			if strindexnc(flags, "2", 1, @fwdscan)
				hbr.addressbar = @false
				hbr.statusbar = @false
				hbr.menubar = @false
				hbr.toolbar = @false
			endif
;	remove the nav buttons and the close buttons if the "4" flag is specified...
		   if strindexnc(flags, "4", 1, @fwdscan) then hbr.fullscreen = @true
			break
	endselect
;	once navigation is complete, show the browser...
	hbr.visible = @true
;	return a reference to the browser object for other functions...
	return(hbr)
#EndFunction

#DefineFunction hStartMSIE(url)
;	internal function, starts the browser and navigates to specified URL...
	Browser = ObjectOpen("InternetExplorer.Application")
	browser.visible = @false
	browser.navigate(url)
   WaitForPageLoad()
;	return a reference to the browser object...
	return(browser)
#EndFunction

#DefineSubroutine WaitForPageLoad()  ; assume Browser
   While browser.busy || browser.readystate == 1
      TimeDelay(0.5)
   EndWhile
   While browser.Document.ReadyState != "complete"
      TimeDelay(0.5)
   EndWhile
   return
#EndSubroutine


#DefineFunction hGetVarNames()
;	find the folder where the variable list is located...
	hpath = "c:\Program Files\HTMLExtender\"
	filename = strcat(hpath, "hvarnamelist.txt")
	Terminate(!fileexist(filename), "Debug", "hGetVarNames...Variable File Missing.")
;	retrieve it and return it to the user...
	hvlist = ""
	ih = fileopen(filename, "read")
	while @true
		line = fileread(ih)
		if line == "*EOF*" then break
		hvlist = strcat(hvlist, line)
	endwhile
	fileclose(ih)
	return(hvlist)
#EndFunction

#DefineFunction hGetVarValue(variable, default)
;	find the folder where the variable values are stored...
	hpath = "c:\Program Files\HTMLExtender\"
	filename = strcat(hpath, "hvarvaluelist.txt")
	Terminate(!fileexist(filename), "Debug", "hGetVarValue...Variable Value File Missing.")
	hvarnamelist = hGetVarNames()
;	build the list and process it...
	hvarvaluelist = ""
	ih = fileopen(filename, "read")
	while @true
		line = fileread(ih)
		if line == "*EOF*" then break
		hvarvaluelist = strcat(hvarvaluelist, line)
	endwhile
	fileclose(ih)
;	check to see if the variable specified is in the var list...
	hpos = itemlocate(variable, hvarnamelist, @tab)
;	if it is, get it's value, otherwise return the default value specified...
	if hpos
		hval = strtrim(itemextract(hpos, hvarvaluelist, @tab))
		return(hval)
	else
;	note: that if your variable is present, but has no value (eg, "") it will return "",
;	not the default text, as per the original function.
		return(default)
	endif
#EndFunction

#DefineFunction hProcessHTML(browser, option)
;	internal function that "inventories" the HTML page...

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;  There's no way for me to plan for every possibilty in HTML so
;  if you find an input or variable that this function can't retrieve please
;	let me know. Post your issue on the Winbatch BBS and I'll look into it.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;	first make sure the arguments are valid...
	Terminate(option <> 1 && option <> 2, "Debug", "hProcessHTML...OPTION argument invalid.")
;	setup a reference to the document object...
	browserdoc = browser.document
;	initialize the list vars...
	hvlist = ""
	hvvalues = ""
;	list of tags that users can supply input to on an HTML page...
	htaglist = "INPUT,SELECT,TEXTAREA"
	for y = 1 to itemcount(htaglist, ",")
		tag = itemextract(y, htaglist, ",")
;	check for this tag in the document...
		vartags = browserdoc.getElementsByTagName(tag)
		for x = 0 to vartags.length-1
;	loop thru them one by one and get their corresponding values
			thistag = vartags.item(x)
			if thistag > 0
;	avoid "nameless" objects and those already on the list...
				if thistag.name <> "" && !itemlocate(thistag.name, hvlist, @tab)
;	check the tag types...
					select @true
						case tag == "INPUT" || tag == "TEXTAREA"
;	look for a radio or checkbox...
							if thistag.type == "radio" || thistag.type == "checkbox"
								if thistag.checked
;	don't place it in the variable list until checked, if it's left unchecked
;	it won't appear in the list and hGetVar() will return the default value...
									hvlist = iteminsert(thistag.name, -1, hvlist, @tab)
									hvvalues = iteminsert(thistag.value, -1, hvvalues, @tab)
								endif
							else
;	normal INPUT object, get the value...
								hvlist = iteminsert(thistag.name, -1, hvlist, @tab)
								if thistag.value == "" 
;	have to insert a SPACE for blank INPUTs, don't worry the return value gets STRTRIM() in hGetVar() and returns ""...
									hvvalues = iteminsert(" ", -1, hvvalues, @tab)
								else
									data = strreplace(thistag.value, @tab, "     ")
									hvvalues = iteminsert(data, -1, hvvalues, @tab)
								endif
							endif
						break
;	SELECT tags are special...
						case tag == "SELECT" 
							hvlist = iteminsert(thistag.name, -1, hvlist, @tab)
							selOptions = thistag.Options
							hvvalues = iteminsert(selOptions.selectedIndex, -1, hvvalues, @tab)
							objectclose(selOptions)
						break
					endselect
				endif
			objectclose(thistag)
			endif
		next
		objectclose(vartags)
	next
	objectclose(browserdoc)
;	return either a list of variable names, or the list of values based on the argument...
	if option == 1
		return(hvlist)
	else
		return(hvvalues)
	endif
#EndFunction


HTML DIALOG SAMPLE SCRIPT

#Include "sample html extender functions.wbt"

;   use the sample html provided below...
webpage = "C:\Test\xml3\hGetVarNames.html"
;   open the browser and show the page...
br = hBrowse(2, webpage, "2")
;
;   loop while the user inputs data...
While hGetURL(br)
   TimeDelay(0.5)
EndWhile
;
;   close the browser...
hEndBrowse(br)
;
;   get the values needed...
myformvarlist = hGetVarNames()
;
shoesize      = hGetVarValue("shoesize", "99")
txtarea       = hGetVarValue("TextBox",  "Wasn't on the page")
manual        = hGetVarValue("manual",   "99")
first         = hGetVarValue("first",    "None")
last          = hGetVarValue("last",     "None")
age           = hGetVarValue("under21",  "0")
;
msgtxt = StrCat("First: ", first, @CRLF, "Last: ", last, @CRLF, "Shoe Size: ", shoesize, @CRLF, "Under 21?: ", age, @CRLF, "Manual: ", manual, @CRLF, "Text Area: ", txtarea)
;   display what was retrieved...
Message(myformvarlist, msgtxt)
;
Return
Exit

HTML DIALOG SAMPLE HTML

;<html>
;<head>
;    <title>hGetVarValue Example</title>
;    <!--This is the hGetVarNames.html file for the hGetVarNames Example-->
;</head>
;
;<body>
;
;<h1 align="center">hGetVarValue Example</h1>
;
;<h2 align="center">Please enter the following information:</h2>
;
;<form action="ignored" method="POST">
;    <div align="center"><center><table border="5" cellpadding="5"
;    cellspacing="5">
;        <tr>
;            <th align="right">First Name:</th>
;            <td><input type="text" size="20" name="first"></td>
;        </tr>
;        <tr>
;            <th align="right">Last Name:</th>
;            <td><input type="text" size="20" name="last"></td>
;        </tr>
;        <tr>
;            <th align="right">Shoe size:</th>
;            <td><select name="shoesize" size="1">
;                <option>1</option>
;                <option>1 1/2</option>
;                <option>2</option>
;                <option>2 1/2</option>
;                <option>3</option>
;                <option>3 1/2</option>
;                <option>4</option>
;                <option>4 1/2</option>
;                <option>5</option>
;                <option>5 1/2</option>
;                <option>6</option>
;                <option>6 1/2</option>
;                <option>7</option>
;                <option>7 1/2</option>
;                <option selected>8</option>
;                <option>8 1/2</option>
;                <option>9</option>
;                <option>9 1/2</option>
;                <option>10</option>
;                <option>10 1/2</option>
;                <option>11</option>
;                <option>11 1/2</option>
;                <option>12</option>
;                <option>12 1/2</option>
;                <option>13</option>
;                <option>13 1/2</option>
;            </select></td>
;        </tr>
;        <tr>
;            <th align="right">Age Check:</th>
;            <td><input type="checkbox" name="under21" value="1">Under 21?</td>
;        </tr>
;        <tr>
;            <th align="right">Manual Check:</th>
;            <td><input type="radio" checked name="manual" value="1">Read manual<br>
;            <input type="radio" name="manual" value="2">Scanned manual<br>
;            <input type="radio" name="manual" value="3">Remember seeing manual<br>
;            <input type="radio" name="manual" value="4">Lost manual</td>
;        </tr>
;        <tr>
;            <th align="right">Manual Check:</th>
;            <td><textarea name="TextBox" cols=55, rows=6 style="overlow:auto;" wrap="physical"></textarea></td>
;        </tr>
;    </table>
;    </center></div><p align="center">
;         <input type="submit" name="ignored" value="Submit">
;         <input type="reset" name="Clear" value="Clear">    </p>
;</form>
;</body>
;</html>

Article ID:   W16122
File Created: 2004:08:02:11:09:00
Last Updated: 2004:08:02:11:09:00