Get the URL of the current (active) browser tab

Post your working scripts, libraries and tools
yar_man
Posts: 3
Joined: 08 Jun 2016, 10:51

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

08 Jun 2016, 13:17

atnbueno,

Thanks so much for this code. I have it working successfully as you have implemented above (triggered by the user) but I am trying another implementation without success. I'm hoping somebody can help.

I want to use the code as a function within an expression after #if similar to the way you might use, #IfWinActive. The web window I want to work in has a changing title so it doesn't play nice with #IfWinActive.

I am close but not quite there. Here is an example you can try by adding it to your code:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


The function works perfectly when fired with ^!Numpad0. Feel free to try it at on google.com. However it does not work when trying it with #if as a requisite for the hotstring "hh".

Here is a link to the output: https://www.dropbox.com/s/8ag8dae1ws8aing/IS_google_output.txt?dl=0

It looks like the significant part is " ErrorLevel := "Invalid IAccessible Object" " any time ACC tries to do its thing (FireFox or Chrome).

I think this implementation could be very useful if we can sort it out. Any Ideas?
User avatar
atnbueno
Posts: 83
Joined: 12 Oct 2013, 04:45

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

08 Jun 2016, 14:23

Hello yar_man.

I'm sorry but I'm not familiar with #If. But before that, your IS_google() function always returns 1 :wtf:

This one works for me (the function, not the #if):

Code: [Select all] [Download] GeSHi © Codebox Plus

isGoogle() {
Return InStr(GetActiveBrowserURL(), "google") > 0
}

I've tried to tweak your #if code and the 'hh' hotkey sometimes works and sometimes doesn't :crazy:

But this always works :)

Code: [Select all] [Download] GeSHi © Codebox Plus

if_in_URL_send(fragment, text) {
If InStr(GetActiveBrowserURL(), fragment)
SendRaw % text
}

::hh::
if_in_URL_send("google", "Huzza!")
Return


Regards,
Antonio B.
yar_man
Posts: 3
Joined: 08 Jun 2016, 10:51

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

08 Jun 2016, 17:05

Antonio,

Thanks for responding. The first post had a broken function in it. Sorry about that. I realized it but couldn't delete the post so I re-posted with a corrected example function and example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



The #if directive is a condition as an alternative to making a hotkey window specific with #Ifwinactive. I have a few hundred hotkeys that perform other functions more complicated than text replacement. HH to Huzzah was just a simplified example. In my use case, the example hh hotkey does something different in a few windows. I can easily write a single example to have a few if's and perform properly. The purpose of using the #If directive instead is to have a few hundred hotkeys listed below it, that are window specific based on the url, without having the "if's" listed out in each one. (I would have to add it to a few hundred hotkeys and any time it needs editing it would be the same)

I could do that creatively and get it done quickly enough but I'm interested in solving this directive riddle because I use functions in directives a lot and I think this will be valuable regardless of application.

With your knowledge of functions, do you know why the GetActiveBrowserURL() would be able to use the ACC library successfully when triggered directly by a hotkey but not when triggered by a directive? Or, do you get a different output than I did? (I'm referring to the errors in the output I linked when ACC fails and you set errorlevel. " ErrorLevel := "Invalid IAccessible Object" ")

Thanks again for the script. I'm going to keep researching. If you think you can shed some light on the ACC error, I'm interested.

Cheers,

Stan
capeably
Posts: 31
Joined: 10 Jul 2014, 14:38

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

10 Jun 2016, 01:58

Thanks for your work on this!

How could I adapt the script to copy both the current tab page title and url to the clipboard with a line break like this:

This is the Page Title
https://autohotkey.com

Thanks in advance!
User avatar
atnbueno
Posts: 83
Joined: 12 Oct 2013, 04:45

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

10 Jun 2016, 12:07

Stan, this problem reminds me the conversation with mc-lemons from last January. I'm guessing that #If calls the function too frequently, everytime there's a window event, and my code is not ready for that. The particular error you mention is typical when the code tries to access an object that doesn't exists anymore.

I'm testing a slightly more sophisticated version of the code that should be a bit more robust, but I'm afraid it's still too far from being able to process a few hundred calls per second :( I'll try to keep this case in mind, though.

capeably, it's very easy. Replace/compare this block in/to the original code:

Code: [Select all] [Download] GeSHi © Codebox Plus

^+!u::
sURL := GetActiveBrowserURL()
WinGetClass, sClass, A
WinGetTitle, sTitle, A
If (sURL != "")
Clipboard := sTitle "`n" sURL "`n"
Else If sClass In % DDE_Browsers "," ACC_Browsers
MsgBox, % "The URL couldn't be determined (" sClass ")"
Else
MsgBox, % "Not a browser or browser not supported (" sClass ")"
Return


Regards,
Antonio B.
capeably
Posts: 31
Joined: 10 Jul 2014, 14:38

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

10 Jun 2016, 13:02

Thanks so much!

I adapted the hotkey actions so that it removes "Mozilla Firefox" from the title, and I added a splash message showing the clipboard contents

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

yar_man
Posts: 3
Joined: 08 Jun 2016, 10:51

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

13 Jun 2016, 14:56

Antonio,

#If does not call the function over and over like a loop would. It only evaluates the expression after #If when a hotkey below that #If is called. That said, when looking at the output from testing this method, it is apparent that the ACC library features you use are not able to operate with the same authority or permissions when called as part of an #If directive vs. being called by a hotkey.

I was thinking about trying to convert a few key functions to subroutines in order to fix this but I haven't gotten to it yet. I also read something about putting certain functions inside a subroutine and using settimer as a workaround to activate them. Somehow that made a difference in how they performed. I need to research that more.

Thanks for reading. Let me know if you have any thoughts.
ToKa
Posts: 26
Joined: 23 Apr 2016, 04:37

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

21 Jun 2016, 23:10

Thanks for the routine, works great running Chrome on Windows XP
JoeSchmoe
Posts: 24
Joined: 08 Dec 2014, 08:58

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

05 Jul 2016, 19:37

I just want to say that I use some code based on this code every day. It works wonderfully!
doctorMe
Posts: 8
Joined: 07 Jul 2016, 03:31

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

13 Aug 2016, 07:24

The code is not getting the url from Internet Explorer 11 under x64 Windows 8.1 version. With x86 Windows works fine.
stealzy
Posts: 47
Joined: 01 Nov 2015, 13:43

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

11 Sep 2016, 03:22

Acc method search control with url too long for firefox (2600 ms - GetAddressBar() is called 360! times) vs DDE (31 ms function).
For modern Opera GetAddressBar() is called 47 times.
Maybe we can to narrow the search, using AccPath?

Regex in IsURL() is overhead. Also, you can't detect "chrome://extensions" and similar.
I turn off IsURL() completely, and still have no mistake in Firefox 47 (ofcourse, using acc method) and Opera 39. Did you have any mistakes?

Not work with Edge 10.10240 on Windows 10x32.
For Edge in my case sometimes works this code:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Also I modify your code to get tooltip with url acc path. Opera 39 - 4.1.2.1.1.5.1.3, Firefox 47 - 4.55.8.2.
What path do you have?
Maybe we should search from the end of this path, based on this information? Try search 4.1.2.1.1.5.1.x; if not, then try search 4.1.2.1.1.5.x; 4.1.2.1.1.x; 4.1.2.1.x; 4.1.2.x; 4.1.x; 4.x.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by stealzy on 11 Sep 2016, 07:44, edited 5 times in total.
stealzy
Posts: 47
Joined: 01 Nov 2015, 13:43

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

11 Sep 2016, 07:23

Common wishes: don't use global variables; remove from function WinGetClass, sClass, A - this generate questions like: "how get url in not active browser window".
iseahound
Posts: 98
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

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

11 Sep 2016, 13:30

I'm enjoying this script so far. Two points though:

1) Could you explain how the speed-up works? Is it being loaded into memory?

2) It's not working with ftp://

Code: [Select all] [Download] GeSHi © Codebox Plus

; ACTUAL:   ftp://ftp.funet.fi/pub/standards/RFC/rfc959.txt
; RESULT http://ftp://ftp.funet.fi/pub/standards/RFC/rfc959.txt
If ((sURL != "") and (SubStr(sURL, 1, 4) != "http")) ; Modern browsers omit "http://"
sURL := "http://" sURL

Fix:

Code: [Select all] [Download] GeSHi © Codebox Plus

	If ((sURL != "") and !(sURL ~= "^(http|ftp)")) ; Modern browsers omit "http:// BUT TEND TO USE HTTPS ANYWAYS
sURL := "http://" sURL


"
stealzy wrote:Common wishes: don't use global variables; remove from function WinGetClass, sClass, A - this generate questions like: "how get url in not active browser window".

Just make them static.

Code: [Select all] [Download] GeSHi © Codebox Plus

GetActiveBrowserURL() {
static ModernBrowsers := "ApplicationFrameWindow,Chrome_WidgetWin_0,Chrome_WidgetWin_1,Maxthon3Cls_MainFrm,MozillaWindowClass,Slimjet_WidgetWin_1"
static LegacyBrowsers := "IEFrame,OperaWindowClass"
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 ""
}
stealzy
Posts: 47
Joined: 01 Nov 2015, 13:43

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

12 Sep 2016, 00:40

iseahound, thank you, captain obvious!
In my second sample of code you can see static variables and other changes. I wrote this not for me, but for TC.
1) If you run my second code, you can understand, how speed-up works. It search for URL among all Acc controls, then save id of url control in static var accAddressBar.
----
* ThunderBird has the same class "MozillaWindowClass", so if it run at the same time with firefox, script sometimes fails.
iseahound
Posts: 98
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

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

12 Sep 2016, 10:55

It's tedious for me to mentally diff your code as I am not the author. It'd be much easier if you highlighted/cut-out the changes you made to the original code. Or even added comments. Still it was my mistake, I didn't realize you had implemented those changes already.
Danny

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

16 Nov 2016, 17:16

Hello Antonio Bueno! Your script no longer works with Firefox as of release version 50 on Nov, 15, 2016.
Your script is wonderful, and I hope you can fix it for the new release. Thank you!
Johnny R
Posts: 210
Joined: 03 Oct 2013, 02:07

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

16 Nov 2016, 20:05

@Danny, I don't have a problem with the new version 50. Antonio Bueno's script works.
Danny

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

16 Nov 2016, 21:13

Hi Johnny R. Thank you for that information. Maybe it is because I am using Windows Vista.
I made a simple replacement that works fine for my purposes:

Code: [Select all] [Download] GeSHi © Codebox Plus

		WinActivate, ahk_class MozillaWindowClass
WinWaitActive, ahk_class MozillaWindowClass
Send, ^l
Sleep, 80
Clipboard =
Send, ^c
ClipWait, 1
sURL = %Clipboard%
Clipboard =
Danny

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

17 Nov 2016, 03:24

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.

Code: [Select all] [Download] GeSHi © Codebox Plus

ModernBrowsers := "ApplicationFrameWindow,Chrome_WidgetWin_0,Chrome_WidgetWin_1,Maxthon3Cls_MainFrm,Slimjet_WidgetWin_1"
LegacyBrowsers := "IEFrame,MozillaWindowClass,OperaWindowClass"
drdutw
Posts: 5
Joined: 23 Oct 2013, 12:20

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

06 Dec 2016, 00:22

Thank you so much! I've been waiting for this for the last 8.5 years.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 18 guests