Page 8 of 10

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

Posted: 09 Dec 2020, 03:01
by rommmcek
@tpitera: You probably don't use it correctly! Try:
Spoiler

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

Posted: 20 Dec 2020, 09:19
by Haswell
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.

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

Posted: 20 Dec 2020, 12:00
by rommmcek
[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..).

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

Posted: 05 Jan 2021, 10:36
by rommmcek
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!

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

Posted: 05 Jan 2021, 12:10
by burque505
@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

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

Posted: 05 Jan 2021, 14:02
by rommmcek
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!

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

Posted: 11 Jan 2021, 18:49
by Dinarid
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

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

Posted: 11 Jan 2021, 19:21
by gregster
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.

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

Posted: 11 Jan 2021, 20:21
by Dinarid
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.

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

Posted: 20 Oct 2021, 04:14
by Tomer
great script!
but i have a problem with a redirected URLs
Please help me!:
viewtopic.php?f=76&t=95810&p=425931#p425931

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

Posted: 12 Nov 2021, 11:53
by Marco N
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

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

Posted: 04 Dec 2021, 15:38
by carno
Works great. Tested in Windows 7.

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

Posted: 19 Feb 2022, 11:44
by BoBo
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
	}

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

Posted: 19 Feb 2022, 14:39
by dbareis
Someone responded to the same question I recently asked, not the clipboard part though.

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

Posted: 19 Feb 2022, 20:42
by submeg
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?

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

Posted: 30 Mar 2022, 16:30
by brunokitano
@submeg https://gist.github.com/anonymous1184/7cce378c9dfdaf733cb3ca6df345b140 try to use this one. It's pretty recent. Don't forget to #include the Acc.ahk

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

Posted: 29 Apr 2022, 02:20
by tdalon
@brunokitano This https://gist.github.com/anonymous1184/7cce378c9dfdaf733cb3ca6df345b140 does not work for me with Chrome unfortunately.

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

Posted: 29 Apr 2022, 03:06
by Descolada
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")
}

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

Posted: 29 Apr 2022, 04:28
by SundayProgrammer
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:

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

Posted: 29 Apr 2022, 15:26
by tdalon
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