Get the URL of the current (active) browser tab

Post your working scripts, libraries and tools for AHK v1.1 and older
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

05 Aug 2018, 16:14

AHKStudent wrote:in chrome 67 and 68 if you have a new tab window as the main window this wont work

to test click to get a new tab in chrome and then run to get list of windows, u will get blank even if you have 5 other regular websites opne
@AHKStudent: If we are talking of the same function GetActiveBrowserURL(), this function retrieves the active tab's URL. What you describe is not an error since there is no URL to retrieve in a blank tab.

@atnbueno: I hope you or another wizard here will be able to look into the Firefox issue...
: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

09 Aug 2018, 02:35

No atnbueno, no solution. Where is atnbueno?
Johnny R
Posts: 348
Joined: 03 Oct 2013, 02:07

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

18 Aug 2018, 08:09

I've reinstalled the newest version of the script and the problem was solved. The script works fine with Firefox v61.0.2. Thank You all!
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

19 Aug 2018, 13:22

I confirm. This works with Firefox v61.0.2:
https://autohotkey.com/boards/viewtopic ... 81#p182981
: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
hotkeyguy
Posts: 170
Joined: 11 Oct 2014, 12:22

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

22 Oct 2018, 08:20

I'm using JnLlnds code. With Firefox 62.0.3, I get sometimes two error messages
1. An error occurred in the secure channel support
followed by
2. The data necessary to complete this operation is not yet available.

The affected lines are objWebRequest.Send() ; and return (objWebRequest.StatusText() = "OK" ? objWebRequest.ResponseText() : "") in function Url2Var(strUrl):

Code: Select all

;------------------------------------------------------------
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() ; <== ERROR 1

	return (objWebRequest.StatusText() = "OK" ? objWebRequest.ResponseText() : "") ; <== ERROR 2
}
;------------------------------------------------------------
That happens e. g. with GitHub pages, like GitHub - nshalabi/SysmonTools: Utilities for Sysmon.
I assume that's an certificate validation issue? I tried Sleep instructions without success.


Many thanks for all your efforts and any hints
hotkeyguy
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

22 Oct 2018, 08:41

Hi,

> I'm using JnLlnds code. With Firefox 62.0.3, I get sometimes two error messages

I tried only a few times but it worked with the same Firefox version.

BTW, I worked on in but I'm not the one who wrote the "core" of the code. It is from atnbueno.

Jean
: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
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

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

21 Feb 2019, 09:43

Spanish version just because…
¡Hola atnbueno!
Gracias por el post con todo lujo de detalles.
Por casualidad ¿tendrías alguna sugerencia para Pale Moon?
De costumbre lo hago con JS dentro de Dorando o GreaseMonkey, pero me resultaría sumamente útil hacerlo en ahk.
Gracias y hasta pronto.

English because that's this board's language
Hello atnbueno!
Thanks for the post full of details.
Do you by any chance have any suggestions for Pale Moon?
I usually do it with JS inside Dorando or GreaseMonkey, but it would be extremely useful to do it in ahk.
Dest938572
Posts: 3
Joined: 28 Dec 2015, 06:02

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

02 Jul 2019, 08:36

atnbueno wrote:
08 Jun 2014, 13:19

Short version: The code below finds out the URL of the current (active) browser tab for most of the modern browsers.
Awesome script, thank you Antonio.
It works perfect more than 5 years.
The shape and solutions of the future depend totally on the collective effort of all people working together.
© Jacque Fresco
User avatar
tdalon
Posts: 40
Joined: 21 Apr 2017, 07:19
Location: Germany
Contact:

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

18 Nov 2019, 04:00

Hi Antonio
I have noticed an issue (Win10) with Internet Explorer.
If I have multiple windows opened, it returns the url of the first opened window and not the one where I have the focus on.
Last edited by tdalon on 13 Feb 2020, 09:55, edited 1 time in total.
User avatar
tdalon
Posts: 40
Joined: 21 Apr 2017, 07:19
Location: Germany
Contact:

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

13 Feb 2020, 09:51

Would be great if it could support Vivaldi.
My workaround is to have:

Code: Select all

If WinActive("ahk_exe vivaldi.exe") {
		Send ^l
		sURL := GetSelection()
		return sURL
		Send {Esc}
	}
I forgot to post:

Code: Select all

GetSelection(){
; Syntax:
; 	sSelection:=GetSelection()


OldClipboard:= ClipboardAll                         ;Save existing clipboard.

Clipboard:=""
while(Clipboard){
  Sleep,10
}
Send,^c                                          ;Copy selected text to clipboard
ClipWait
  sSelection := clipboard

; Restore Clipboard
Clipboard:= OldClipboard 
return sSelection
} 
Last edited by tdalon on 15 Feb 2020, 14:59, edited 1 time in total.
User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

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

14 Feb 2020, 10:39

Recently is saw this beautiful way, I use often too.
If you still have problems just repost!
garry
Posts: 3720
Joined: 22 Dec 2013, 12:50

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

14 Feb 2020, 15:54

rommmcek, thank you , and I used ctr+l for firefox and chrome ( maybe also for other browsers ( ? ) )
User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

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

14 Feb 2020, 17:45

@garry: AFAIK we have already history... For FF works ^l as well as !d. For most browsers does, although the first one seems to be more universal.
garry
Posts: 3720
Joined: 22 Dec 2013, 12:50

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

15 Feb 2020, 10:14

for firefox also alt+s
User avatar
tdalon
Posts: 40
Joined: 21 Apr 2017, 07:19
Location: Germany
Contact:

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

04 Apr 2020, 12:57

The workaround to focus / select the omnibox/ adress bar is a poor workaround: I don't want to move my cursor/position of my mouse when calling the function.
User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

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

07 Apr 2020, 08:27

Did you try to run Vivaldi with --force-renderer-accessibility switch?
BNOLI
Posts: 548
Joined: 23 Mar 2020, 03:55

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

01 May 2020, 02:52

Would someone mind to PayPal Antonio AKA @atnbueno a beer (or two! :shh:) to do an annual check on all of these different browsers?
And thx 4 sharing this marvelous piece of code :thumbup:
Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !
thomastthai
Posts: 18
Joined: 12 Mar 2020, 01:51

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

14 May 2020, 18:20

Thank you @atnbueno for this excellent function. On a Windows 10 64-bit system (with latest updates as of 2020-05-14) running both Chrome (Version 81.0.4044.138) and Internet Explorer 11, AHK Version 1.1.32.0. With both Chrome and IE running, even if I click on IE last to make it active, or with all instances of Chrome gone, the script defaults to checking Chrome when running from Visual Studio Code.

Window Spy reports VS Code as having Chrome_WidgetWin_1 as ahk_class. I'm unsure if that caused issues, but that was the class the script kept getting. [UPDATE: I found out VS Code uses Electron which is based on Chromium.] I needed it to get the ahk_class IEFrame. I don't fully understand all the code, but I'm unsure if it checks against ahk_exe and combines with ahk_class.

I am curious why the variables LegacyBrowsers and ModernBrowsers sit outside the function instead of inside?

I added a parameter and supporting code to "force" it to check the legacy browser first. Comments and notes are included in the code.

Code: Select all

; AutoHotkey Version:	AutoHotkey 1.1
; Language:				English
; Platform:				Win7 SP1 / Win8.1 / Win10
; Author:				Antonio Bueno <user atnbueno of Google's popular e-mail service>
; Short description:	Gets the URL of the current (active) browser tab for most modern browsers
; Change log:			
; 		2020-05-13		1) Added legacy parameter and code to "force" checking legacy 
;						browser first. 2) Replaced "Last mod:" field in the description
;						with "Change log:" to track changes. 3) Commented out and added instruction
;						in the demo section so current users can do an in-place update to their lib.
;						Thomas T. Thai <[email protected]>
;		2016-05-19		Last update by Antonio Bueno <[email protected]>

; Move these to variables to the top so they are automatically defined when this
; file is included and the demo is commented out. @thomastthai
ModernBrowsers := "ApplicationFrameWindow,Chrome_WidgetWin_0,Chrome_WidgetWin_1,Maxthon3Cls_MainFrm,MozillaWindowClass,Slimjet_WidgetWin_1"
LegacyBrowsers := "IEFrame,OperaWindowClass"

; --- <Demo> -------------------------------------------------------------------
;
; Instructions
; ------------
; To run the demo, uncomment the two lines indicated by the <--- arrows below.
;
/* ; <--- remove the */ to uncomment to run the demo
Menu, Tray, Icon, % A_WinDir "\system32\netshell.dll", 86 ; Shows a world icon in the system tray

^+!u:: ; Ctrl-Alt-Shift u
	nTime := A_TickCount
	sURL := GetActiveBrowserURL(True)
	WinGetClass, sClass, A
	If (sURL != "")
		MsgBox, % "The URL is """ sURL """`nEllapsed time: " (A_TickCount - nTime) " ms (" sClass ")"
	Else If sClass In % ModernBrowsers "," LegacyBrowsers
		MsgBox, % "The URL couldn't be determined (" sClass ")"
	Else
		MsgBox, % "Not a browser or browser not supported (" sClass ")"
Return
*/ ; <--- remove the */ to uncomment to run the demo
; --- </Demo> ------------------------------------------------------------------

GetActiveBrowserURL(legacy = False) {
	global ModernBrowsers, LegacyBrowsers

	; @thomastthai - added parameter legacy and added the code below to handle 
	; systems where modern browser and legacy browsers are both running. 
	; My Windows 10 Pro system defaults to checking Chrome instead of 
	; Internet Explorer 11. and we want to check on the legacy active window.
	if legacy {
		WinGetClass, sClass, ahk_class IEFrame
		If (sClass = "")
			WinGetClass, sClass, ahk_class OperaWindowClass
	}
	Else
		WinGetClass, sClass, A

	If sClass In % ModernBrowsers
		Return GetBrowserURL_ACC(sClass)
	Else If sClass In % LegacyBrowsers
		Return GetBrowserURL_DDE(sClass) ; empty string if DDE not supported (or not a browser)
	Else
		Return ""
}

; "GetBrowserURL_DDE" adapted from DDE code by Sean, (AHK_L version by maraskan_user)
; Found at http://autohotkey.com/board/topic/17633-/?p=434518

GetBrowserURL_DDE(sClass) {
	WinGet, sServer, ProcessName, % "ahk_class " sClass
	StringTrimRight, sServer, sServer, 4
	iCodePage := 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", sServer, "int", iCodePage)
	hTopic := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", "WWW_GetWindowInfo", "int", iCodePage)
	hItem := DllCall("DdeCreateStringHandle", "UPtr", idInst, "Str", "0xFFFFFFFF", "int", iCodePage)
	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, sWindowInfo, csvWindowInfo, `" ;"; comment to avoid a syntax highlighting issue in autohotkey.com/boards
	Return sWindowInfo2
}

GetBrowserURL_ACC(sClass) {
	global nWindow, accAddressBar
	If (nWindow != WinExist("ahk_class " sClass)) ; reuses accAddressBar if it's the same window
	{
		nWindow := WinExist("ahk_class " sClass)
		accAddressBar := GetAddressBar(Acc_ObjectFromWindow(nWindow))
	}
	Try sURL := accAddressBar.accValue(0)
	If (sURL == "") {
		WinGet, nWindows, List, % "ahk_class " sClass ; In case of a nested browser window as in the old CoolNovo (TO DO: check if still needed)
		If (nWindows > 1) {
			accAddressBar := GetAddressBar(Acc_ObjectFromWindow(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" based in code by uname
; Found at http://autohotkey.com/board/topic/103178-/?p=637687

GetAddressBar(accObj) {
	Try If ((accObj.accRole(0) == 42) and IsURL(accObj.accValue(0)))
		Return accObj
	Try If ((accObj.accRole(0) == 42) and IsURL("http://" accObj.accValue(0))) ; Modern browsers omit "http://"
		Return accObj
	For nChild, accChild in Acc_Children(accObj)
		If IsObject(accAddressBar := GetAddressBar(accChild))
			Return accAddressBar
}

IsURL(sURL) {
	Return RegExMatch(sURL, "^(?<Protocol>https?|ftp)://(?<Domain>(?:[\w-]+\.)+\w\w+)(?::(?<Port>\d+))?/?(?<Path>(?:[^:/?# ]*/?)+)(?:\?(?<Query>[^#]+)?)?(?:\#(?<Hash>.+)?)?$")
}

; The code below is part of the Acc.ahk Standard Library by Sean (updated by jethrow)
; Found at http://autohotkey.com/board/topic/77303-/?p=491516

Acc_Init()
{
	static h
	If Not h
		h:=DllCall("LoadLibrary","Str","oleacc","Ptr")
}
Acc_ObjectFromWindow(hWnd, idObject = 0)
{
	Acc_Init()
	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)
}
Acc_Query(Acc) {
	Try Return ComObj(9, ComObjQuery(Acc,"{618736e0-3c3d-11cf-810c-00aa00389b71}"), 1)
}
Acc_Children(Acc) {
	If ComObjType(Acc,"Name") != "IAccessible"
		ErrorLevel := "Invalid IAccessible Object"
	Else {
		Acc_Init(), cChildren:=Acc.accChildCount, Children:=[]
		If DllCall("oleacc\AccessibleChildren", "Ptr",ComObjValue(Acc), "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?Acc_Query(child):child), NumGet(varChildren,i-8)=9?ObjRelease(child):
			Return Children.MaxIndex()?Children:
		} Else
			ErrorLevel := "AccessibleChildren DllCall Failed"
	}
}
conracer
Posts: 6
Joined: 26 Jul 2020, 11:51

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

17 Aug 2020, 18:37

I have read all the code in this thread and here is my solution (caution - thank you tdalon: from system to system the Acc-path can vary!):

Code: Select all

#Include AccV2.ahk ; https://github.com/sancarn/ACC.AHK

F7::MsgBox % GetURL()

GetUwpAppName() {
	; https://www.autohotkey.com/boards/viewtopic.php?p=73137#p73137
    WinGet name,ProcessName,A
    if(name="ApplicationFrameHost.exe") {
        ControlGet hWnd,Hwnd,,Windows.UI.Core.CoreWindow1,A
        if(hWnd)
            WinGet name,ProcessName,ahk_id %hWnd%
    }
    return name
}

GetURL() {
	if(WinActive("ahk_exe chrome.exe"))
		return Acc_Get("Object","4.1.2.2.2",0,"A").accValue(0)
	if(WinActive("ahk_exe firefox.exe"))
		return Acc_Get("Object","4.22.8.1",0,"A").accValue(0)
	if(WinActive("ahk_class IEFrame"))
		return Acc_Get("Object","4.3.4.1.4.3.4.2.2",0,"A").accValue(0)
	if(GetUwpAppName()="MicrosoftEdge.exe") {
		items:=Acc_Children(Acc_Get("Object","4.2.4",0,"A"))
		Loop % items.length()
			if(items[A_Index].accRole(0)=ACC_ROLE.TEXT)
				return items[A_Index].accValue(0)
	}
	ClipSaved:=ClipboardAll
	Clipboard=
	Sleep 100
	Send ^l^c ; Ctrl+l would also work in ALL four browsers from above. But nevertheless: this line is untested!
	ClipWait 0.1
	url:=Clipboard
	Clipboard:=ClipSaved
	ClipSaved=
	return url
}
BTW: It is not that much work to go through the AccViewer.ahk-tree to find what you want
tpitera
Posts: 31
Joined: 27 Oct 2020, 15:56

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

08 Dec 2020, 10:31

@atnbueno I just tried this on Chrome, did not work, please advise

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 87 guests