HTMLFile object, getElementsByTagName reports 0 initially Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

HTMLFile object, getElementsByTagName reports 0 initially

02 May 2017, 21:11

I saved the following large htm file (2MB) to the disk:
WinApi
https://hotkeyit.github.io/v2/docs/commands/WinApi.htm

Note: it is big enough that it slows down iWB2 Learner.

I opened it and put it into an HTMLFile object to retrieve information from it. Applying getElementsByTagName once returned 0, the same operation again gave the correct value. Oddly it only gave the correct value a second time if I used MsgBox. A long Sleep delay did not seem to work, nor did a loop repeating getElementsByTagName without using the MsgBox.

Code: Select all

q:: ;get dll function info from WinApi.htm
;WinApi
;https://hotkeyit.github.io/v2/docs/commands/WinApi.htm

vPath = %A_ScriptDir%\Help\WinApi.htm
oHTML := ComObjCreate("HTMLFile")
FileRead, vHtml, % vPath
oHTML.write(vHtml)

;didn't work:
if 0
{
	ToolTip, LOADING
	Loop
	{
		if (oHTML.getElementsByTagName("tbody").length = 28)
			break
		Sleep 10
	}
	ToolTip, READY
	Sleep 1000
	ToolTip
}

;if use MsgBox, it seems to work:
MsgBox, % oHTML.getElementsByTagName("tbody").length
MsgBox, % oHTML.getElementsByTagName("tbody").length
MsgBox, % oHTML.getElementsByTagName("tr").length

vOutput := ""
VarSetCapacity(vOutput, 1000000*2)
Loop, % oHTML.getElementsByTagName("tbody").length
{
	if (A_Index = 1)
		continue
	vDll := oHTML.getElementsByTagName("h2")[A_Index-2].innerText
	oElt := oHTML.getElementsByTagName("tbody")[A_Index-1]

	if (SubStr(vDll, StrLen(vTemp)+1-4) = ".dll")
		vDll := SubStr(vDll, 1, -4)

	Loop, % oElt.getElementsByTagName("tr").length
	{
		oElt2 := oElt.getElementsByTagName("tr")[A_Index-1]
		vOutput .= vDll "\" oElt2.all[2].all[0].innerText "=" oElt2.all[0].innerText "`r`n"
	}

}
Clipboard := vOutput
oHTML := ""
MsgBox, % "done"
return
Note: in that list, many of the parameters listed as unsigned are actually signed, this is because in some contexts you can state that the parameter was either signed or unsigned and it does not affect the outcome.

[800th post!]
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: HTMLFile object, getElementsByTagName reports 0 initially

22 May 2017, 10:41

2 other issues I've had when writing to an HTMLFile object from saved htm files:
- a cookies warning prompt
- webpages opened in Internet Explorer

In case anybody knows how to prevent that. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: HTMLFile object, getElementsByTagName reports 0 initially

25 Aug 2017, 18:11

Bump. I'm still having problems with this. I've tried some new ideas but it's still not working without a redundant MsgBox or InputBox line.

Without a prior MsgBox/InputBox call:
This line: MsgBox, % oHTML.getElementsByTagName("tbody").length returns 0.
With a prior MsgBox/InputBox call:
The line returns the correct value.

Code: Select all

q::
;e.g. url for a large htm file
;WinApi
;https://hotkeyit.github.io/v2/docs/commands/WinApi.htm

vPath = %A_ScriptDir%\Help\WinApi.htm
oHTML := ComObjCreate("HTMLFile")
FileRead, vHtml, % vPath
oHTML.write(vHtml)

;doesn't work:
;Sleep 3000
;SetTimer, EmptySub, -1
;Sleep 3000

;busy causes an error
;readyState always says 'loading' even after a MsgBox call
;while oHTML.busy || !(oHTML.readyState = 4)
;while !(oHTML.readyState = 4)
;{
;	MsgBox, % oHTML.readyState
;	Sleep 10
;}

MsgBox
;InputBox, vInput ;also works
;ToolTip, % "hello" ;didn't work
;Progress, zh0 b1, % "hello" ;didn't work
;Sleep 1000
;Progress, Off

;this works:
;DllCall("MessageBox", "Int", "0", "Str", "Press Yes or No", "Str", "Title of box", "Int", 4)

;doesn't work:
;hFont := DllCall("gdi32\CreateFont", Int,vFontHeight, Int,0, Int,0, Int,0
;, Int,vFontWeight, UInt,0, UInt,0 ,UInt,0
;, UInt,0, UInt,0, UInt,0, UInt,0
;, UInt,0, Str,vFontName, Ptr)

MsgBox, % oHTML.getElementsByTagName("tbody").length
MsgBox, % oHTML.getElementsByTagName("tr").length
MsgBox, % oHTML.readyState
oHTML := ""
return

EmptySub:
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: HTMLFile object, getElementsByTagName reports 0 initially  Topic is solved

30 Oct 2017, 05:07

I have a fix for the problem. Tested on Windows 7.
This worked:
<meta http-equiv="X-UA-Compatible" content="IE=9">
This didn't work:
<meta http-equiv="X-UA-Compatible" content="IE=edge">
This works but I don't know why.
MsgBox,,,, 0.001

Code: Select all

q:: ;count tbody elements - 3 workaround attempts
;vUrl := "https://hotkeyit.github.io/v2/docs/commands/WinApi.htm"
vUrl := "https://hotkeyit.github.io/v2/docs/commands/DllCall.htm"
SplitPath, vUrl, vName
vPath := A_Desktop "\" vName
if !FileExist(vPath)
	UrlDownloadToFile, % vUrl, % vPath

Loop, 3
{
	oHTML := ComObjCreate("HTMLFile")

	;adding this didn't fix the problem
	if (A_Index = 1)
	{
		vHtml = <meta http-equiv="X-UA-Compatible" content="IE=edge">
		oHTML.write(vHtml)
	}

	;adding this did fix the problem
	if (A_Index = 2)
	{
		vHtml = <meta http-equiv="X-UA-Compatible" content="IE=9">
		oHTML.write(vHtml)
	}

	FileRead, vHtml, % vPath
	oHTML.write(vHtml)

	;reasonably efficient workaround
	if (A_Index = 3)
		MsgBox,,,, 0.001

	MsgBox, % "this should be non-zero: " oHTML.getElementsByTagName("tbody").length
	oHTML := ""
}
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada and 331 guests