Get the URL of the current (active) browser tab

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
rommmcek
Posts: 1502
Joined: 15 Aug 2014, 15:18

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

09 Dec 2020, 03:01

@tpitera: You probably don't use it correctly! Try:
Spoiler
Haswell
Posts: 98
Joined: 21 Feb 2016, 17:11

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

20 Dec 2020, 09:19

rommmcek wrote:
09 Dec 2020, 03:01
@tpitera: You probably don't use it correctly!
Hi rommmcek.
Thanks for your solution.
I use your code in Firefox but it looks unreliable.
Sometimes it works quite perfect but sometimes it gives me URL of a previously closed tab.
Do you notice the issue?
I failed to figure out how to fix it.
I suppose the issue is presented with Chrome too.
Focusing our efforts on non-productive and non-creative endeavours wastes lives as surely as war.
Jacque Fresco / The best that money can't buy
User avatar
rommmcek
Posts: 1502
Joined: 15 Aug 2014, 15:18

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

20 Dec 2020, 12:00

[Edit]: I rarely use FF and Chrome. So I didn't notice such issue lately, but I noticed it in IE long time ago under specific conditions. Now I analysed it a bit closer and yeah, under harsh conditions FF gives terrible results and Chrome not much better either.
Here is my attempt tested on IE, FF (not tested on Pale Moon, but eager for feedback) and Chromium based browsers (Chrome, Edge, Vivaldi..).
Attachments
GetAccData 6.45.ahk
(3.21 KiB) Downloaded 1444 times
Last edited by rommmcek on 10 Apr 2021, 10:09, edited 11 times in total.
User avatar
rommmcek
Posts: 1502
Joined: 15 Aug 2014, 15:18

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

05 Jan 2021, 10:36

GetAccData() v6.45 - function to retrieve Title and Url of the desired browser tab
Spoiler
P.s.: Sorry for that much quotes in the Spoiler, but I want to get as much feed back as possible!
Last edited by rommmcek on 10 Apr 2021, 12:58, edited 16 times in total.
burque505
Posts: 1747
Joined: 22 Jan 2017, 19:37

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

05 Jan 2021, 12:10

@rommmcek, thank for this, should come in very handy. I just tried it with 5 tabs open in Firefox 84.0.1 64-bit and 7 tabs open in Chrome Version 87.0.4280.88 (Official Build) (64-bit). I set a hotkey as shown below, randomly went from tab to tab, switching browsers randomly, not a single failure or incorrectly reported tab. Win7 SP1 64-bit.

Code: Select all

^!#f::
GetAccData() ; parameter "WinTitle" - Ahk like syntax e.g. "A" (default), "ahk_class IEFrame", "ahk_exe google.exe", etc.
MsgBox % accData.1 "`n`n" accData.2
Return
Regards,
burque505
User avatar
rommmcek
Posts: 1502
Joined: 15 Aug 2014, 15:18

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

05 Jan 2021, 14:02

Good to hear it! Yeah, switching Tabs is crucial and I tested closing some of them randomly (which causes switching too). Now it came to me it should be tested with opening new tabs likewise, but the true test will be, when people will be using it for the prolonged period of time under all sorts of unpredictable conditions!

P.s.: After posting the function and signing out (in IE) I got some weird results (again in IE), but then I couldn't reproduce the issue, so I don't know the real reason...

P.p.s.: Limitation in Chrom - Search box may not be opened (Ctrl + f as native Chrom shortcut).
[Edit]: Simple workaround included in the updated code above!
Dinarid
Posts: 2
Joined: 11 Jan 2021, 18:38

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

11 Jan 2021, 18:49

My question is only tangentially related to this, and I'm not sure if there's a better subforum for it -- if there is one, I don't mind if my post is moved there, of course.

I'm trying to use this to copy the URL to the clipboard, and it works fine, but the resulting URL always has quote marks (as in "www.url.com"). I'd like to remove those quote marks, but I have no idea how (I've tried removing a pair of quote marks in the script, but then AHK always returns " sURL " instead of the actual URL). Could anyone help?

Here's my code:

Code: Select all

#c::
	nTime := A_TickCount
	sURL := GetActiveBrowserURL()
	WinGetClass, sClass, A
	If (sURL != "")
		Clipboard:= % """" sURL """"
	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
User avatar
gregster
Posts: 9224
Joined: 30 Sep 2013, 06:48

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

11 Jan 2021, 19:21

Dinarid wrote:
11 Jan 2021, 18:49
My question is only tangentially related to this, and I'm not sure if there's a better subforum for it -- if there is one, I don't mind if my post is moved there, of course.

I'm trying to use this to copy the URL to the clipboard, and it works fine, but the resulting URL always has quote marks (as in "www.url.com"). I'd like to remove those quote marks, but I have no idea how (I've tried removing a pair of quote marks in the script, but then AHK always returns " sURL " instead of the actual URL). Could anyone help?
[...]

Code: Select all

		Clipboard:= % """" sURL """"
That would generally fit better into 'Ask For Help', imo (and we might move it). But I don't understand your use of quote marks in this case.
Why not just

Code: Select all

Clipboard := sURL
if I understand you correctly?!
A look into expressions might be beneficial.
Dinarid
Posts: 2
Joined: 11 Jan 2021, 18:38

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

11 Jan 2021, 20:21

Thank you! That solved it. I realize it was a basic question, I just started using AHK a few days ago and I'm still learning.
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

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

20 Oct 2021, 04:14

great script!
but i have a problem with a redirected URLs
Please help me!:
viewtopic.php?f=76&t=95810&p=425931#p425931
Marco N
Posts: 18
Joined: 30 Oct 2021, 16:17

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

12 Nov 2021, 11:53

Hello from a true novice. I don't know any programming languages other than a few small macros in VBA with Excel. I took my first steps in this context last week with Pulover macro creator and I need something that reads the active browser page link and insert it in the clipboard. Can anyone help me?
Thanks in advance
carno
Posts: 266
Joined: 20 Jun 2014, 16:48

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

04 Dec 2021, 15:38

Works great. Tested in Windows 7.
BoBo
Posts: 6563
Joined: 13 May 2014, 17:15

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

19 Feb 2022, 11:44

Marco N wrote:
12 Nov 2021, 11:53
Hello from a true novice. I don't know any programming languages other than a few small macros in VBA with Excel. I took my first steps in this context last week with Pulover macro creator and I need something that reads the active browser page link and insert it in the clipboard. Can anyone help me?
Thanks in advance
Late, so this is JFTR. This in fact, is a stripped-down :arrow: @jeeswg function.

Code: Select all

#SingleInstance, Force
SetWorkingDIr, A_ScriptDir
SetBatchLines,-1
#Include Acc.ahk																	; https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/Acc.ahk

F11::MsgBox % clipboard := GetChromeUrl()											; copying the address bar content of the TAB that's on focus. Remove 'MsgBox %' after testing. 
F12::MsgBox % clipboard := GetChromeUrl(WinExist("ahk_class Chrome_WidgetWin_1"))	; this will extract an URL even if the browser itself isn't on focus.

GetChromeUrl(hWnd:="") {
	local
	hWnd := (hWnd = "") ? WinExist("A") : hwnd
	oAcc := Acc_Get("Object", "4.1.1.2.1.2.5.3", 0, "ahk_id " hWnd)		; "4.1.1.2.1.2.5.3" valid for Chrome (v98). Please update accordingly - using ACCViewer.ahk
	if !IsObject(oAcc) || !(oAcc.accName(0) = "Address and search bar")
		oAcc := Acc_Get("Object", "4.1.1.2.1.2.5.3", 0, "ahk_id " hWnd)	; see above.
	vUrl := oAcc.accValue(0),  oAcc := ""
	return vUrl
	}
dbareis
Posts: 39
Joined: 04 Feb 2022, 17:57

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

19 Feb 2022, 14:39

Someone responded to the same question I recently asked, not the clipboard part though.
User avatar
submeg
Posts: 346
Joined: 14 Apr 2017, 20:39
Contact:

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

19 Feb 2022, 20:42

Hi all,

Found this gem of a thread, but I just tried this in Chrome (Version 98.0.4758.102 (Official Build) (64-bit)) on Windows 10, but received the following error - Not a browser or browser not supported (Chrome_WidgetWin_1)

Is this code no longer current?
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
brunokitano
Posts: 3
Joined: 30 Mar 2022, 16:26

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

30 Mar 2022, 16:30

@submeg https://gist.github.com/anonymous1184/7cce378c9dfdaf733cb3ca6df345b140 try to use this one. It's pretty recent. Don't forget to #include the Acc.ahk
Descolada
Posts: 1431
Joined: 23 Dec 2021, 02:30

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

29 Apr 2022, 03:06

Tested on Chrome and Edge, and no need to include Acc.

Code: Select all

#SingleInstance, Force
SetTitleMatchMode, 2

F10::
	MsgBox, % GetURL("Google Chrome")
	return

GetURL(wTitle*) {
	ErrorLevel := 0
	if !(wId := WinExist(wTitle*)) {
		ErrorLevel := 1
		return
	}
	IUIAutomation := ComObjCreate(CLSID_CUIAutomation := "{ff48dba4-60ef-4201-aa87-54103eef594e}", IID_IUIAutomation := "{30cbe57d-d9d0-452a-ab13-7ac5ac4825ee}")
	DllCall(NumGet(NumGet(IUIAutomation+0)+6*A_PtrSize), "ptr", IUIAutomation, "ptr", wId, "ptr*", elementMain)   ; IUIAutomation::ElementFromHandle
	NumPut(addressbarStrPtr := DllCall("oleaut32\SysAllocString", "wstr", "Address and search bar", "ptr"),(VarSetCapacity(addressbar,8+2*A_PtrSize)+NumPut(8,addressbar,0,"short"))*0+&addressbar,8,"ptr")
	DllCall("oleaut32\SysFreeString", "ptr", addressbarStrPtr)
	if (A_PtrSize = 4) {
		DllCall(NumGet(NumGet(IUIAutomation+0)+23*A_PtrSize), "ptr", IUIAutomation, "int", 30005, "int64", NumGet(addressbar, 0, "int64"), "int64", NumGet(addressbar, 8, "int64"), "ptr*", addressbarCondition)   ; IUIAutomation::CreatePropertyCondition
	} else {
		DllCall(NumGet(NumGet(IUIAutomation+0)+23*A_PtrSize), "ptr", IUIAutomation, "int", 30005, "ptr", &addressbar, "ptr*", addressbarCondition)   ; IUIAutomation::CreatePropertyCondition
	}
	DllCall(NumGet(NumGet(elementMain+0)+5*A_PtrSize), "ptr", elementMain, "int", TreeScope_Descendants := 0x4, "ptr", addressbarCondition, "ptr*", currentURLElement) ; IUIAutomationElement::FindFirst
	DllCall(NumGet(NumGet(currentURLElement+0)+10*A_PtrSize),"ptr",currentURLElement,"uint",30045,"ptr",(VarSetCapacity(currentURL,8+2*A_PtrSize)+NumPut(0,currentURL,0,"short")+NumPut(0,currentURL,8,"ptr"))*0+&currentURL) ;IUIAutomationElement::GetCurrentPropertyValue
	ObjRelease(currentURLElement)
	ObjRelease(elementMain)
	ObjRelease(IUIAutomation)
	return StrGet(NumGet(currentURL,8,"ptr"),"utf-16")
}
Last edited by Descolada on 30 Apr 2022, 00:20, edited 1 time in total.
SundayProgrammer
Posts: 143
Joined: 25 Dec 2020, 12:26

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

29 Apr 2022, 04:28

Descolada wrote:
29 Apr 2022, 03:06
Tested on Chrome and Edge, and no need to include Acc.

Code: Select all

#SingleInstance, Force
SetTitleMatchMode, 2

F10::
	MsgBox, % GetURL("Google Chrome")
	return

GetURL(wTitle) {
:thumbup:
User avatar
tdalon
Posts: 44
Joined: 21 Apr 2017, 07:19
Location: Germany
Contact:

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

29 Apr 2022, 15:26

Descolada wrote:
29 Apr 2022, 03:06
Tested on Chrome and Edge, and no need to include Acc.

Code: Select all

#SingleInstance, Force
SetTitleMatchMode, 2

F10::
	MsgBox, % GetURL("Google Chrome")
	return

GetURL(wTitle) {
	ErrorLevel := 0
	if !(wId := WinExist(wTitle)) {
		ErrorLevel := 1
		return
	}
	IUIAutomation := ComObjCreate(CLSID_CUIAutomation := "{ff48dba4-60ef-4201-aa87-54103eef594e}", IID_IUIAutomation := "{30cbe57d-d9d0-452a-ab13-7ac5ac4825ee}")
	DllCall(NumGet(NumGet(IUIAutomation+0)+6*A_PtrSize), "ptr", IUIAutomation, "ptr", wId, "ptr*", elementMain)   ; IUIAutomation::ElementFromHandle
	NumPut(addressbarStrPtr := DllCall("oleaut32\SysAllocString", "wstr", "Address and search bar", "ptr"),(VarSetCapacity(addressbar,8+2*A_PtrSize)+NumPut(8,addressbar,0,"short"))*0+&addressbar,8,"ptr")
	DllCall("oleaut32\SysFreeString", "ptr", addressbarStrPtr)
	if (A_PtrSize = 4) {
		DllCall(NumGet(NumGet(IUIAutomation+0)+23*A_PtrSize), "ptr", IUIAutomation, "int", 30005, "int64", NumGet(addressbar, 0, "int64"), "int64", NumGet(addressbar, 8, "int64"), "ptr*", addressbarCondition)   ; IUIAutomation::CreatePropertyCondition
	} else {
		DllCall(NumGet(NumGet(IUIAutomation+0)+23*A_PtrSize), "ptr", IUIAutomation, "int", 30005, "ptr", &addressbar, "ptr*", addressbarCondition)   ; IUIAutomation::CreatePropertyCondition
	}
	DllCall(NumGet(NumGet(elementMain+0)+5*A_PtrSize), "ptr", elementMain, "int", TreeScope_Descendants := 0x4, "ptr", addressbarCondition, "ptr*", currentURLElement) ; IUIAutomationElement::FindFirst
	DllCall(NumGet(NumGet(currentURLElement+0)+10*A_PtrSize),"ptr",currentURLElement,"uint",30045,"ptr",(VarSetCapacity(currentURL,8+2*A_PtrSize)+NumPut(0,currentURL,0,"short")+NumPut(0,currentURL,8,"ptr"))*0+&currentURL) ;IUIAutomationElement::GetCurrentPropertyValue
	ObjRelease(currentURLElement)
	ObjRelease(elementMain)
	ObjRelease(IUIAutomation)
	return StrGet(NumGet(currentURL,8,"ptr"),"utf-16")
}
Tested on this post with Chrome. Seems to work but returned URL does not include https

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 125 guests