Get the URL of the current (active) browser tab

Post your working scripts, libraries and tools for AHK v1.1 and older
Thierry

Re: Get the URL of the current (active) browser tab

15 Dec 2016, 08:56

Hello
It seems it does not work properly if I have many tabs opened. It does not return the url of the current active tab.
uhuhseo

Re: Get the URL of the current (active) browser tab

13 Mar 2017, 01:50

Thanks for the code atnbueno. I have a simple implementation to get the chrome URL. What do you think?

Code: Select all

GetChromeURL()
{
        WinGetClass, ActWinClass, A
        if (ActWinClass = "Chrome_WidgetWin_1")
        {
                tempclip1:=clipboard
                sleep,12 ;8eg3w
                blockinput, on
                
                send, !{d}
                sleep,12 ;8eg3w
                send, ^{c}
                sleep,12 ;8eg3w
                tempclip2:=clipboard
                sleep,12 ;8eg3w
                clipboard:=tempclip1
                sleep,12 ;8eg3w
                
                blockinput, off
                return tempclip2
        }
}
anidealworld
Posts: 4
Joined: 17 Feb 2017, 13:07

Re: Get the URL of the current (active) browser tab

30 Mar 2017, 12:06

Thierry wrote:Hello
It seems it does not work properly if I have many tabs opened. It does not return the url of the current active tab.
Yes, sadly it grabs a specific tab/window and insists on returning that one instead of the currently active tab/window URL or last used.
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Get the URL of the current (active) browser tab

31 Mar 2017, 17:08

For PaleMoon 27.2.1 (32-bit) on Win7-x64, 64-bit AHK, I get ("The URL couldn't be determined"(MozillaWindowClass")).
Chrome works, reports (Chrome_WinWidget_1).
Firefox is the fastest of the one's I've tried so far, 437ms.
Interestingly, when Thunderbird is open, if you select a message with the mouse, it gives you a URL for the message and also reports MozillaWindowClass, which I guess is no surprise.
Thanks for the script!
Cardoso

Re: Get the URL of the current (active) browser tab

16 Apr 2017, 11:18

Please excuse my ignorance in this type of script, this is seeing for the first time, how can I get the data of this script in an external program, developed in vb.net, I want to capture the active url of this script. thank you.
User avatar
atnbueno
Posts: 89
Joined: 12 Oct 2013, 04:45
Contact:

Re: Get the URL of the current (active) browser tab

28 May 2017, 03:48

Thierry wrote:Hello
It seems it does not work properly if I have many tabs opened. It does not return the url of the current active tab.
(Sorry for not seeing your message before)

That shouldn't happen :think: Which browser/version are you using? Are you using the script from the first page unmodified?
Cardoso wrote:Please excuse my ignorance in this type of script, this is seeing for the first time, how can I get the data of this script in an external program, developed in vb.net, I want to capture the active url of this script. thank you.
Excuse my ignorance in vb.net :) but this script uses the Microsoft Active Accessibility (MSAA) API. You can find links to the relevant Microsoft documentation in https://autohotkey.com/board/topic/7730 ... -09272012/ If you are able to understand it, you will have no problem in replicating the functionality.


Regards,
Antonio
DanielToward13
Posts: 74
Joined: 18 May 2017, 10:56

Re: Get the URL of the current (active) browser tab

29 May 2017, 06:28

Thanks atnbueno, great script. There is a bug and I don't know how to fix the problem. If I opened two Chrome windows (each with different tabs), the script works well but once I add a new blank tab and press the ^+!u then the script will return the previous active tab URL and after that it get stuck on that URL and couldn't identify the current tab URL anymore. Can this problem be solved?

I am using the script from the first page unmodified. Chrome browser version 58.0.3029.110 (64-bit)
hotkeyguy
Posts: 170
Joined: 11 Oct 2014, 12:22

Re: Get the URL of the current (active) browser tab

29 May 2017, 18:21

Hello atnbueno,

I'm looking for a way to get (additionally) the title of the page shown in the current (active) browser tab - in my case for Firefox. Any hints are highly appreciated!


Many thanks and greetings
hotkeyguy
Guest

Re: Get the URL of the current (active) browser tab

10 Sep 2017, 12:48

Hi

The problem with this lib is that its very slow once i have many (like 200) tabs opened.

Retrieving url time goes down to 1541ms on Chrome.

When i open fresh Edge browser with just one tab, the retrieval time is about 100ms.

So is there something that can be done to speed up url retrieval?
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Get the URL of the current (active) browser tab

15 Nov 2017, 18:01

Danny wrote:If anyone had this script fail with the Firefox version 50 release, here was my solution:
Just put Mozilla with the Legacy Browsers, so it uses DDE instead of ACC.
I think this worked for some time. But, now, with Firefox v56.0.2, both Modern and Legacy functions return an empty string. Could someone one of the browsers guru here investigate this code ?

Many thanks.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Get the URL of the current (active) browser tab

15 Nov 2017, 18:51

- This quick fix is working for me.
- If your locale isn't English, you may like to try the numeric version of vAccPath that is currently commented out.
- It requires the Acc library.
- It may be that it can be simplified, I haven't looked at it in a long time.
- If the 'http://' bit is hidden, it assumes 'http://'.

Code: Select all

;[Acc functions]
;Acc library (MSAA) and AccViewer download links - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=26201

q:: ;mozilla firefox - get url
WinGet, hWnd, ID, A
vUrl := JEE_FirefoxGetUrl(hWnd)
MsgBox, % vUrl
return

JEE_FirefoxGetUrl(hWnd, vOpt:="")
{
	Loop, 10
	{
		vIndex := A_Index
		vAccPath := "application.tool_bar3.combo_box1.editable_text"
		;vAccPath := "4.25.3.2"
		if InStr(vOpt, "p") ;(pop-up window)
			vAccPath := "application.tool_bar1.combo_box1.editable_text"
		oAcc := Acc_Get("Object", vAccPath, 0, "ahk_id " hWnd)
		if !ErrorLevel
			break
		Sleep, 100
	}
	if (vIndex = 10)
		return

	vUrl := ""
	try vUrl := oAcc.accValue(0)
	if !(vUrl = "") && !InStr(vUrl, "://")
		vUrl := "http://" vUrl
	return vUrl
}
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
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Get the URL of the current (active) browser tab

15 Nov 2017, 19:25

Thanks jeeswg. I'll look at it soon.

Just FYI, yes I'm my locale is not English (it is French-Canadian) but both functions work well with IE (legacy) or Chrome (modern). The issue is just with Firefox, AKAIK.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Get the URL of the current (active) browser tab

16 Nov 2017, 09:35

@jeeswg: I tried to fit your code in mine but I think our codes are based on the same source in this thread. There has been some variations/branched from the OP code in this thread and I'l a little lost ;-)

But, using the GetAddressBar(accObj, accPath:="") function as found on page 4 https://autohotkey.com/boards/viewtopic ... 48#p109548 (second piece of code in it), it made my script work again with Firefox 56.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
Bunny the Dummy

Re: Get the URL of the current (active) browser tab

16 Nov 2017, 14:08

Is there a way for it to just check every 10 seconds and write to a file the active url instead of me needing to do it with a hotkey?

I know how to write to a file just need to know how to get it to check without hotkey, i tried removing the hotkey but then it doesnt work
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Get the URL of the current (active) browser tab

16 Nov 2017, 14:31

Bunny the Dummy wrote:Is there a way for it to just check every 10 seconds and write to a file the active url instead of me needing to do it with a hotkey?

I know how to write to a file just need to know how to get it to check without hotkey, i tried removing the hotkey but then it doesnt work
This thread is about getting the URL out of the current browser. Repeating it every 10 seconds is another topic. I suggest you start an new topic for this. It could be helpful for users having anything else to execute repeatedly.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Get the URL of the current (active) browser tab

16 Nov 2017, 14:54

If this can help, here is my full script for getting the URL and getting the web page title. Most of the code for getting the URL comes from this thread but code formatting and variables naming were adapted to my own style.

Tested with IE 11, Chrome and Firefox 57 and Edge 40. Alt-Q to test it.

Code: Select all

#SingleInstance force ; Skips the dialog box and replaces the old instance automatically

g_strModernBrowsers := "ApplicationFrameWindow,Chrome_WidgetWin_0,Chrome_WidgetWin_1,Maxthon3Cls_MainFrm,Slimjet_WidgetWin_1,MozillaWindowClass"
g_strLegacyBrowsers := "IEFrame,OperaWindowClass"

;------------------------------------------------------------
!q::
;------------------------------------------------------------
strURL := ""

WinGetClass, strClass, A

if InStr(g_strModernBrowsers, strClass)
	strURL := GetCurrentUrlAcc(strClass)
else if InStr(g_strLegacyBrowsers, strClass)
	strURL := GetCurrentUrlDDE(strClass) ; empty string if DDE not supported (or not a browser)

if StrLen(strURL)
	MsgBox, % "Class: " . strClass
		. "`nURL: " . strURL
		. "`nTitle: " . GetWebPageTitle(strURL)
else
	MsgBox, Current window is not a browser
	

return
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlDDE(strClass)
; "GetCurrentUrlDDE" adapted from DDE code by Sean, (AHK_L version by maraskan_user) (via Joe Glines)
; Found at http://autohotkey.com/board/topic/17633-/?p=434518
;------------------------------------------------------------
{
	WinGet, strServer, ProcessName, % "ahk_class " . strClass
	StringTrimRight, strServer, strServer, 4
	intCodePage := (A_IsUnicode ? 0x04B0 : 0x03EC) ; 0x04B0 = CP_WINUNICODE, 0x03EC = CP_WINANSI
	
	DllCall("DdeInitialize", "UPtrP", idInst, "Uint", 0, "Uint", 0, "Uint", 0)
	
	hServer := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", strServer, "int", intCodePage)
	hTopic := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", "WWW_GetWindowInfo", "int", intCodePage)
	hItem := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", "0xFFFFFFFF", "int", intCodePage)
	hConv := DllCall("DdeConnect", "UPtr", idInst, "UPtr", hServer, "UPtr", hTopic, "Uint", 0)
	hData := DllCall("DdeClientTransaction", "Uint", 0, "Uint", 0, "UPtr", hConv, "UPtr", hItem, "UInt", 1, "Uint", 0x20B0, "Uint", 10000, "UPtrP", nResult) ; 0x20B0 = XTYP_REQUEST, 10000 = 10s timeout
	sData := DllCall("DdeAccessData", "Uint", hData, "Uint", 0, "Str")
	
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hServer)
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hTopic)
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hItem)
	DllCall("DdeUnaccessData", "UPtr", hData)
	DllCall("DdeFreeDataHandle", "UPtr", hData)
	DllCall("DdeDisconnect", "UPtr", hConv)
	DllCall("DdeUninitialize", "UPtr", idInst)
	
	csvWindowInfo := StrGet(&sData, "CP0")
	StringSplit, strWindowInfo, csvWindowInfo, `" ;"; comment to avoid a syntax highlighting issue in autohotkey.com/boards
	
	Return strWindowInfo2
}
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlAcc(strClass)
; Found at https://autohotkey.com/boards/viewtopic.php?f=6&t=3702&start=60
;------------------------------------------------------------
{
	; static or global?
	global nWindow
	global accAddressBar
	
	If (nWindow != WinExist("ahk_class " strClass)) ; reuses accAddressBar if it's the same window
	{
		nWindow := WinExist("ahk_class " strClass)
		accAddressBar := GetAddressBar(GetCurrentUrlAccObjectFromWindow(nWindow))
	}
	Try sURL := accAddressBar.accValue(0)
		If (sURL == "")
		{
			WinGet, nWindows, List, % "ahk_class " strClass ; In case of a nested browser window as in the old CoolNovo (TO DO: check if still needed)
			If (nWindows > 1)
			{
				accAddressBar := GetAddressBar(GetCurrentUrlAccObjectFromWindow(nWindows2))
				Try sURL := accAddressBar.accValue(0)
			}
		}
	If ((sURL != "") and (SubStr(sURL, 1, 4) != "http")) ; Modern browsers omit "http://"
		sURL := "http://" . sURL
	If (sURL == "")
		nWindow := -1 ; Don't remember the window if there is no URL
	Return sURL
}
;------------------------------------------------------------


;------------------------------------------------------------
GetAddressBar(accObj, accPath:="")
; "GetAddressBar" based in code by stealzy
; Found at https://autohotkey.com/boards/viewtopic.php?p=109548#p109548
; IsUrl in this functions above replaced by my own code LocationIsHttp
;------------------------------------------------------------
{
	n := 0
	Try If ((accObj.accRole(0) == 42) and StrLen(accObj.accValue(0)) and LocationIsHttp(accObj.accValue(0)))
		Return accObj
	Try If ((accObj.accRole(0) == 42) and StrLen(accObj.accValue(0)) and LocationIsHttp("http://" . accObj.accValue(0))) ; Modern browsers omit "http://"
		Return accObj
	For nChild, accChild in GetCurrentUrlAccChildren(accObj)
	{
		n++
		currentPath := accPath . n . "."
		If IsObject(accAddressBar := GetAddressBar(accChild, currentPath))
			Return accAddressBar
	}
}
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlAccInit()
; Part of the Acc.ahk Standard Library by Sean (updated by jethrow) (via Joe Glines)
; Found at http://autohotkey.com/board/topic/77303-/?p=491516
;------------------------------------------------------------
{
	static h
	If Not h
		h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")
}
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlAccObjectFromWindow(hWnd, idObject = 0)
; Part of the Acc.ahk Standard Library by Sean (updated by jethrow) (via Joe Glines)
; Found at http://autohotkey.com/board/topic/77303-/?p=491516
;------------------------------------------------------------
{
	GetCurrentUrlAccInit()
	If DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject&=0xFFFFFFFF, "Ptr"
		, -VarSetCapacity(IID, 16) + NumPut(idObject == 0xFFFFFFF0 ? 0x46000000000000C0 : 0x719B3800AA000C81
		, NumPut(idObject == 0xFFFFFFF0 ? 0x0000000000020400 : 0x11CF3C3D618736E0, IID, "Int64"), "Int64"), "Ptr*", pacc) = 0
		Return ComObjEnwrap(9, pacc, 1)
}
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlAccQuery(objAcc)
; Part of the Acc.ahk Standard Library by Sean (updated by jethrow) (via Joe Glines)
; Found at http://autohotkey.com/board/topic/77303-/?p=491516
;------------------------------------------------------------
{
	Try Return ComObj(9, ComObjQuery(objAcc, "{618736e0-3c3d-11cf-810c-00aa00389b71}"), 1)
}
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlAccChildren(objAcc)
; Part of the Acc.ahk Standard Library by Sean (updated by jethrow) (via Joe Glines)
; Found at http://autohotkey.com/board/topic/77303-/?p=491516
;------------------------------------------------------------
{
	If ComObjType(objAcc,"Name") != "IAccessible"
		ErrorLevel := "Invalid IAccessible Object"
	Else
	{
		GetCurrentUrlAccInit()
		cChildren := objAcc.accChildCount
		Children := []
		If DllCall("oleacc\AccessibleChildren", "Ptr", ComObjValue(objAcc), "Int", 0, "Int", cChildren, "Ptr"
			, VarSetCapacity(varChildren, cChildren * (8 + 2 * A_PtrSize), 0) * 0 + &varChildren, "Int*", cChildren) = 0
		{
			Loop, %cChildren%
			{
				i := (A_Index - 1) * (A_PtrSize * 2 + 8) + 8
				child := NumGet(varChildren, i)
				Children.Insert(NumGet(varChildren, i - 8) = 9 ? GetCurrentUrlAccQuery(child) : child)
				(NumGet(varChildren, i - 8) = 9 ? ObjRelease(child) : "")
			}
			Return (Children.MaxIndex() ? Children : "")
		}
		Else
			ErrorLevel := "AccessibleChildren DllCall Failed"
	}
}
;------------------------------------------------------------


;------------------------------------------------------------
LocationIsHTTP(strLocation)
;------------------------------------------------------------
{
	return SubStr(strLocation, 1, 7) = "http://" or SubStr(strLocation, 1, 8) = "https://"
}
;------------------------------------------------------------


;------------------------------------------------------------
GetWebPageTitle(strLocation)
;------------------------------------------------------------
{
	ToolTip, %lToolTipRetrievingWebPageTitle%
	strHTML := Url2Var(strLocation)
	ToolTip
	
	RegExMatch(strHTML, "is)<title>(.*?)</title>", strTitle)
	
	StringReplace, strTitle, strTitle, <title>
	StringReplace, strTitle, strTitle, </title>
	StringReplace, strTitle, strTitle, `r, , A
	StringReplace, strTitle, strTitle, `t, %A_Space%, A
	StringReplace, strTitle, strTitle, `n, %A_Space%, A
	
	return NumDecode(Trim(strTitle, Chr(160))) ; Chr(160) to also trim non-breaking spaces
}
;------------------------------------------------------------


;------------------------------------------------------------
Url2Var(strUrl)
;------------------------------------------------------------
{
	objWebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	/*
	if (A_LastError)
		; an error occurred during ComObjCreate (A_LastError probably is E_UNEXPECTED = -2147418113 #0x8000FFFFL)
		BUT DO NOT ABORT because the following commands will be executed even if an error occurred in ComObjCreate (!)
	*/
	objWebRequest.Open("GET", strUrl)
	objWebRequest.Send()

	return (objWebRequest.StatusText() = "OK" ? objWebRequest.ResponseText() : "")
}
;------------------------------------------------------------


;------------------------------------------------------------
NumDecode(str)
; Extracted from Dec_XML() https://autohotkey.com/board/topic/29866-encoding-and-decoding-functions-v11/
; converts "&#233;" or "&#xE9;" to "é"
;------------------------------------------------------------
{
	Loop
		If RegexMatch(str, "S)(&#(\d+);)", dec) ; matches: &#[dec];
			StringReplace, str, str, %dec1%, % Chr(dec2), All
		Else If RegexMatch(str, "Si)(&#x([\da-f]+);)", hex) ; matches: &#x[hex];
			StringReplace, str, str, %hex1%, % Chr("0x" . hex2), All
		Else
			Break
	
	return str
} 
;------------------------------------------------------------
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
Bunny the Dummy

Re: Get the URL of the current (active) browser tab

16 Nov 2017, 18:51

thank you for making a great script even more awesome
JnLlnd wrote:If this can help, here is my full script for getting the URL and getting the web page title. Most of the code for getting the URL comes from this thread but code formatting and variables naming were adapted to my own style.

Tested with IE 11, Chrome and Firefox 57 and Edge 40. Alt-Q to test it.

Code: Select all

#SingleInstance force ; Skips the dialog box and replaces the old instance automatically

g_strModernBrowsers := "ApplicationFrameWindow,Chrome_WidgetWin_0,Chrome_WidgetWin_1,Maxthon3Cls_MainFrm,Slimjet_WidgetWin_1,MozillaWindowClass"
g_strLegacyBrowsers := "IEFrame,OperaWindowClass"

;------------------------------------------------------------
!q::
;------------------------------------------------------------
strURL := ""

WinGetClass, strClass, A

if InStr(g_strModernBrowsers, strClass)
	strURL := GetCurrentUrlAcc(strClass)
else if InStr(g_strLegacyBrowsers, strClass)
	strURL := GetCurrentUrlDDE(strClass) ; empty string if DDE not supported (or not a browser)

if StrLen(strURL)
	MsgBox, % "Class: " . strClass
		. "`nURL: " . strURL
		. "`nTitle: " . GetWebPageTitle(strURL)
else
	MsgBox, Current window is not a browser
	

return
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlDDE(strClass)
; "GetCurrentUrlDDE" adapted from DDE code by Sean, (AHK_L version by maraskan_user) (via Joe Glines)
; Found at http://autohotkey.com/board/topic/17633-/?p=434518
;------------------------------------------------------------
{
	WinGet, strServer, ProcessName, % "ahk_class " . strClass
	StringTrimRight, strServer, strServer, 4
	intCodePage := (A_IsUnicode ? 0x04B0 : 0x03EC) ; 0x04B0 = CP_WINUNICODE, 0x03EC = CP_WINANSI
	
	DllCall("DdeInitialize", "UPtrP", idInst, "Uint", 0, "Uint", 0, "Uint", 0)
	
	hServer := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", strServer, "int", intCodePage)
	hTopic := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", "WWW_GetWindowInfo", "int", intCodePage)
	hItem := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", "0xFFFFFFFF", "int", intCodePage)
	hConv := DllCall("DdeConnect", "UPtr", idInst, "UPtr", hServer, "UPtr", hTopic, "Uint", 0)
	hData := DllCall("DdeClientTransaction", "Uint", 0, "Uint", 0, "UPtr", hConv, "UPtr", hItem, "UInt", 1, "Uint", 0x20B0, "Uint", 10000, "UPtrP", nResult) ; 0x20B0 = XTYP_REQUEST, 10000 = 10s timeout
	sData := DllCall("DdeAccessData", "Uint", hData, "Uint", 0, "Str")
	
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hServer)
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hTopic)
	DllCall("DdeFreeStringHandle", "UPtr", idInst, "UPtr", hItem)
	DllCall("DdeUnaccessData", "UPtr", hData)
	DllCall("DdeFreeDataHandle", "UPtr", hData)
	DllCall("DdeDisconnect", "UPtr", hConv)
	DllCall("DdeUninitialize", "UPtr", idInst)
	
	csvWindowInfo := StrGet(&sData, "CP0")
	StringSplit, strWindowInfo, csvWindowInfo, `" ;"; comment to avoid a syntax highlighting issue in autohotkey.com/boards
	
	Return strWindowInfo2
}
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlAcc(strClass)
; Found at https://autohotkey.com/boards/viewtopic.php?f=6&t=3702&start=60
;------------------------------------------------------------
{
	; static or global?
	global nWindow
	global accAddressBar
	
	If (nWindow != WinExist("ahk_class " strClass)) ; reuses accAddressBar if it's the same window
	{
		nWindow := WinExist("ahk_class " strClass)
		accAddressBar := GetAddressBar(GetCurrentUrlAccObjectFromWindow(nWindow))
	}
	Try sURL := accAddressBar.accValue(0)
		If (sURL == "")
		{
			WinGet, nWindows, List, % "ahk_class " strClass ; In case of a nested browser window as in the old CoolNovo (TO DO: check if still needed)
			If (nWindows > 1)
			{
				accAddressBar := GetAddressBar(GetCurrentUrlAccObjectFromWindow(nWindows2))
				Try sURL := accAddressBar.accValue(0)
			}
		}
	If ((sURL != "") and (SubStr(sURL, 1, 4) != "http")) ; Modern browsers omit "http://"
		sURL := "http://" . sURL
	If (sURL == "")
		nWindow := -1 ; Don't remember the window if there is no URL
	Return sURL
}
;------------------------------------------------------------


;------------------------------------------------------------
GetAddressBar(accObj, accPath:="")
; "GetAddressBar" based in code by stealzy
; Found at https://autohotkey.com/boards/viewtopic.php?p=109548#p109548
; IsUrl in this functions above replaced by my own code LocationIsHttp
;------------------------------------------------------------
{
	n := 0
	Try If ((accObj.accRole(0) == 42) and StrLen(accObj.accValue(0)) and LocationIsHttp(accObj.accValue(0)))
		Return accObj
	Try If ((accObj.accRole(0) == 42) and StrLen(accObj.accValue(0)) and LocationIsHttp("http://" . accObj.accValue(0))) ; Modern browsers omit "http://"
		Return accObj
	For nChild, accChild in GetCurrentUrlAccChildren(accObj)
	{
		n++
		currentPath := accPath . n . "."
		If IsObject(accAddressBar := GetAddressBar(accChild, currentPath))
			Return accAddressBar
	}
}
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlAccInit()
; Part of the Acc.ahk Standard Library by Sean (updated by jethrow) (via Joe Glines)
; Found at http://autohotkey.com/board/topic/77303-/?p=491516
;------------------------------------------------------------
{
	static h
	If Not h
		h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")
}
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlAccObjectFromWindow(hWnd, idObject = 0)
; Part of the Acc.ahk Standard Library by Sean (updated by jethrow) (via Joe Glines)
; Found at http://autohotkey.com/board/topic/77303-/?p=491516
;------------------------------------------------------------
{
	GetCurrentUrlAccInit()
	If DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject&=0xFFFFFFFF, "Ptr"
		, -VarSetCapacity(IID, 16) + NumPut(idObject == 0xFFFFFFF0 ? 0x46000000000000C0 : 0x719B3800AA000C81
		, NumPut(idObject == 0xFFFFFFF0 ? 0x0000000000020400 : 0x11CF3C3D618736E0, IID, "Int64"), "Int64"), "Ptr*", pacc) = 0
		Return ComObjEnwrap(9, pacc, 1)
}
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlAccQuery(objAcc)
; Part of the Acc.ahk Standard Library by Sean (updated by jethrow) (via Joe Glines)
; Found at http://autohotkey.com/board/topic/77303-/?p=491516
;------------------------------------------------------------
{
	Try Return ComObj(9, ComObjQuery(objAcc, "{618736e0-3c3d-11cf-810c-00aa00389b71}"), 1)
}
;------------------------------------------------------------


;------------------------------------------------------------
GetCurrentUrlAccChildren(objAcc)
; Part of the Acc.ahk Standard Library by Sean (updated by jethrow) (via Joe Glines)
; Found at http://autohotkey.com/board/topic/77303-/?p=491516
;------------------------------------------------------------
{
	If ComObjType(objAcc,"Name") != "IAccessible"
		ErrorLevel := "Invalid IAccessible Object"
	Else
	{
		GetCurrentUrlAccInit()
		cChildren := objAcc.accChildCount
		Children := []
		If DllCall("oleacc\AccessibleChildren", "Ptr", ComObjValue(objAcc), "Int", 0, "Int", cChildren, "Ptr"
			, VarSetCapacity(varChildren, cChildren * (8 + 2 * A_PtrSize), 0) * 0 + &varChildren, "Int*", cChildren) = 0
		{
			Loop, %cChildren%
			{
				i := (A_Index - 1) * (A_PtrSize * 2 + 8) + 8
				child := NumGet(varChildren, i)
				Children.Insert(NumGet(varChildren, i - 8) = 9 ? GetCurrentUrlAccQuery(child) : child)
				(NumGet(varChildren, i - 8) = 9 ? ObjRelease(child) : "")
			}
			Return (Children.MaxIndex() ? Children : "")
		}
		Else
			ErrorLevel := "AccessibleChildren DllCall Failed"
	}
}
;------------------------------------------------------------


;------------------------------------------------------------
LocationIsHTTP(strLocation)
;------------------------------------------------------------
{
	return SubStr(strLocation, 1, 7) = "http://" or SubStr(strLocation, 1, 8) = "https://"
}
;------------------------------------------------------------


;------------------------------------------------------------
GetWebPageTitle(strLocation)
;------------------------------------------------------------
{
	ToolTip, %lToolTipRetrievingWebPageTitle%
	strHTML := Url2Var(strLocation)
	ToolTip
	
	RegExMatch(strHTML, "is)<title>(.*?)</title>", strTitle)
	
	StringReplace, strTitle, strTitle, <title>
	StringReplace, strTitle, strTitle, </title>
	StringReplace, strTitle, strTitle, `r, , A
	StringReplace, strTitle, strTitle, `t, %A_Space%, A
	StringReplace, strTitle, strTitle, `n, %A_Space%, A
	
	return NumDecode(Trim(strTitle, Chr(160))) ; Chr(160) to also trim non-breaking spaces
}
;------------------------------------------------------------


;------------------------------------------------------------
Url2Var(strUrl)
;------------------------------------------------------------
{
	objWebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	/*
	if (A_LastError)
		; an error occurred during ComObjCreate (A_LastError probably is E_UNEXPECTED = -2147418113 #0x8000FFFFL)
		BUT DO NOT ABORT because the following commands will be executed even if an error occurred in ComObjCreate (!)
	*/
	objWebRequest.Open("GET", strUrl)
	objWebRequest.Send()

	return (objWebRequest.StatusText() = "OK" ? objWebRequest.ResponseText() : "")
}
;------------------------------------------------------------


;------------------------------------------------------------
NumDecode(str)
; Extracted from Dec_XML() https://autohotkey.com/board/topic/29866-encoding-and-decoding-functions-v11/
; converts "&#233;" or "&#xE9;" to "é"
;------------------------------------------------------------
{
	Loop
		If RegexMatch(str, "S)(&#(\d+);)", dec) ; matches: &#[dec];
			StringReplace, str, str, %dec1%, % Chr(dec2), All
		Else If RegexMatch(str, "Si)(&#x([\da-f]+);)", hex) ; matches: &#x[hex];
			StringReplace, str, str, %hex1%, % Chr("0x" . hex2), All
		Else
			Break
	
	return str
} 
;------------------------------------------------------------
Johnny R
Posts: 348
Joined: 03 Oct 2013, 02:07

Re: Get the URL of the current (active) browser tab

06 Dec 2017, 01:32

The script doesn't work with the new Firefox-Version 57 ("Quantum") anymore. Who can help us, me and the script?
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Get the URL of the current (active) browser tab

06 Dec 2017, 10:31

Johnny R wrote:The script doesn't work with the new Firefox-Version 57 ("Quantum") anymore. Who can help us, me and the script?
It re-tested it and it is working with Firefox 57 on some websites (for example, on Twitter.com).... but not on others. I could not tell why. We would need again input from browsers experts here?
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
Johnny R
Posts: 348
Joined: 03 Oct 2013, 02:07

Re: Get the URL of the current (active) browser tab

08 Dec 2017, 07:01

Where is our friend atnbueno? Hallo atnbueno!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 125 guests