Internet Explorer: jump to fragment identifier (element ID) 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

Internet Explorer: jump to fragment identifier (element ID)

20 Aug 2018, 10:51

- I want to be able to jump to fragment identifiers, e.g. the 'Related' section on AHK help pages.
- If I do something like:
oWB.Navigate("https://autohotkey.com/docs/commands/Run.htm#Related")
That will work the first time. But if I scroll away from that point and execute that line again, it won't jump back.
- I have a workaround below, but I wondered if there was a more direct approach. Thanks.

Code: Select all

;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

;e.g. url
;Run / RunWait - Syntax & Usage | AutoHotkey
;https://autohotkey.com/docs/commands/Run.htm

q:: ;internet explorer - jump to fragment identifier
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
vUrl := RegExReplace(oWB.document.url, "#.*")
if InStr(oWB.document.url, "#Related")
{
	oWB.Navigate(vUrl "#")
	while oWB.busy || !(oWB.ReadyState = 4)
		Sleep, 10
}
oWB.Navigate(vUrl "#Related")
oWB := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Internet Explorer: jump to fragment identifier (element ID)  Topic is solved

20 Aug 2018, 19:24

simulate a click on the heading:

Code: Select all

; ahk v2
#SingleInstance Force

global WB := WBGet("ahk_id" WinExist("ahk_class IEFrame ahk_exe iexplore.exe"))

Esc::ExitApp()
q::goToRelated() {
	try WB.document.parentWindow.frames[0].document.getElementById("Related").children[0].click()
}

WBGet(WinTitle:="ahk_class IEFrame", Svr:=1)
{
	static msg := DllCall("user32\RegisterWindowMessage", Str,"WM_HTML_GETOBJECT", UInt)
	, IID := "{0002DF05-0000-0000-C000-000000000046}" ;IID_IWebBrowserApp
	;, IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ;IID_IHTMLWindow2
	lResult := SendMessage(msg, 0, 0, "Internet Explorer_Server" Svr, WinTitle)
	if (lResult != "")
	{
		VarSetCapacity(GUID,16,0)
		if (DllCall("ole32\CLSIDFromString", WStr,"{332C4425-26CB-11D0-B483-00C04FD90119}", Ptr,&GUID) >= 0)
		{
			DllCall("oleacc\ObjectFromLresult", Ptr,lResult, Ptr,&GUID, UPtr,0, PtrP,pdoc)
			obj := ComObject(9,ComObjQuery(pdoc,IID,IID),1)
			ObjRelease(pdoc)
			return obj
		}
	}
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: jump to fragment identifier (element ID)

20 Aug 2018, 19:30

- Oh yeah. That's really great, much obliged.
- It's working very smoothly now. And it doesn't ever jump up to the top of the page like in the last script.
- New script:

Code: Select all

;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

;e.g. url
;Run / RunWait - Syntax & Usage | AutoHotkey
;https://autohotkey.com/docs/commands/Run.htm

q:: ;internet explorer - jump to fragment identifier
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
try oWB.document.parentWindow.frames[0].document.getElementById("Related").children[0].click()
oWB := ""
return
- (I forgot that the new version of the website uses frames, although it looks like in this case no fiddly code for handling frames was required.)
- If anyone has any other ideas, I would still be interested, because it's good to know a range of techniques. Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Internet Explorer: jump to fragment identifier (element ID)

21 Aug 2018, 16:01

The scrollIntoView method can be used to scroll the element on which it's called into the visible area of the browser window - for example:

Code: Select all

#NoEnv
#Singleinstance force
#Warn

GUI, Margin, 0, 0
GUI, Add, ActiveX, w700 h700 vWB, Shell.Explorer
WB.navigate("https://fr.wikipedia.org/wiki/Wikip%C3%A9dia:Accueil_principal")
while (WB.busy or WB.readyState <> 4)
	sleep, 100
WB.document.getElementById("Actualités").scrollIntoView(true) ;  ; if alignToTop parameter is set to false the TOP of the element will be aligned to the TOP of the view
GUI, Show, AutoSize
return

!i::
	WB.document.getElementById("Actualités").scrollIntoView(false) ; if alignToTop parameter is set to false the BOTTOm of the element will be aligned to the BOTTOm of the view
return
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 253 guests