Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Get the URL of the current (active) browser tab


  • Please log in to reply
34 replies to this topic
atnbueno
  • Members
  • 91 posts
  • Last active: Feb 16 2016 07:04 PM
  • Joined: 24 Mar 2007

Another thing that is problematic is copying the url that is under the mouse cursor, with chrome at least.

 

I answered this a few weeks ago here. HTH.


Regards,
Antonio

Bob Munck
  • Members
  • 5 posts
  • Last active: Oct 03 2014 02:57 PM
  • Joined: 26 Sep 2014

I want to remap keys on the number pad when I'm accessing feedly (feedly.com/index.html) with the Chrome browser. I have your code followed by the following:

sURL := GetActiveBrowserURL()
IfInString, sURL, %feedly%
{
NumpadDot::send {PgDn}
^NumpadDot::send .
NumpadEnter::send A
^NumpadEnter::send {Enter}
Numpad0::send ^w
^Numpad0::send 0
.
.
.
}

It seems to do the mapping for all pages in all windows, though I may have been positioned on the feedly page when I activated autohotkey. It's likely that I'm not understanding the basics of how Autohotkey executes. Is there a way to do what I want?

 



Coco
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012
@Bob Munck, to create context-sensitive hotkeys you would need to use the #If directives.
e.g.(for feedly's homepage):
#If WinActive("feedly - Google Chrome") ;// you can make the hotkeys behave differently under different scenarios
	NumpadDot::send {PgDn}
	^NumpadDot::send .
	NumpadEnter::send A
	^NumpadEnter::send {Enter}
	Numpad0::send ^w
	^Numpad0::send 0
#If ;// disable context-sensitivity, subsequent hotkeys defined below this line will work on all windows


Bob Munck
  • Members
  • 5 posts
  • Last active: Oct 03 2014 02:57 PM
  • Joined: 26 Sep 2014

 



Bob Munck
  • Members
  • 5 posts
  • Last active: Oct 03 2014 02:57 PM
  • Joined: 26 Sep 2014

@Bob Munck, to create context-sensitive hotkeys you would need to use the #If directives.
e.g.(for feedly's homepage):

#If WinActive("feedly - Google Chrome") ;// you can make the hotkeys behave differently under different scenarios

That doesn't work, I think because IfWinActive (is it one word or two?) looks at the window title, not the URL.  The feedly page has a different title depending on which RSS feed I'm looking at. For example, if I've selected Slashdot, Window Spy shows:

 

>>>>>>>>>>( Window Title & Class )<<<<<<<<<<<
Slashdot - Google Chrome
ahk_class Chrome_WidgetWin_1
 
If I stay on that same page and select the Gizmodo feed, it shows:
 
>>>>>>>>>>( Window Title & Class )<<<<<<<<<<<
Gizmodo - Google Chrome
ahk_class Chrome_WidgetWin_1
 
But the URL always starts with "feedly.com".
 


Coco
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012
My example is just for the homepage. Of course you can change the expression after #If other than WinActive() Try this(works for me):
#If InStr( sURL := GetActiveBrowserURL(), "feedly.com" )
    NumpadDot::send {PgDn}
    ^NumpadDot::send .
    NumpadEnter::send A
    ^NumpadEnter::send {Enter}
    Numpad0::send ^w
    ^Numpad0::send 0
#If
NOTE: When you #Inlcude GetActiveBrowserURL() in your script, comment out OR remove the following lines at the top:
Menu, Tray, Icon, % A_WinDir "\system32\netshell.dll", 86 ; Shows a world icon in the system tray

#u::
    nTime := A_TickCount
    sURL := GetActiveBrowserURL()
    WinGetClass, sClass, A
    If (sURL != "")
        MsgBox, % "The URL is """ sURL """`nEllapsed time: " (A_TickCount - nTime) " ms (" sClass ")"
    Else If sClass In IEFrame,MozillaWindowClass,OperaWindowClass,Chrome_WidgetWin_1,Chrome_WidgetWin_0,Maxthon3Cls_MainFrm
        MsgBox, % "The URL couldn't be determined (" sClass ")"
    Else
        MsgBox, % "Not a browser or browser not supported (" sClass ")"
Return
Or use this (inlcudes your hotkeys at the bottom):
; AutoHotkey Version: AutoHotkey 1.1
; Language:           English
; Platform:           Win7 SP1 / Win8.1
; 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
; Last Mod:           2014-07-05

GetActiveBrowserURL() {
    WinGetClass, sClass, A
    If sClass In Chrome_WidgetWin_1,Chrome_WidgetWin_0,Maxthon3Cls_MainFrm
        Return GetBrowserURL_ACC(sClass)
    Else
        Return GetBrowserURL_DDE(sClass) ; empty string if DDE not supported (or not a browser)
}

; "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, % """"
    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 == "") {
        sURL := accAddressBar.accDescription(0) ; Origin Chip support
        If (sURL == "") {
            WinGet, nWindows, List, % "ahk_class " sClass ; In case of a nested browser window as in CoolNovo
            If (nWindows > 1) {
                accAddressBar := GetAddressBar(Acc_ObjectFromWindow(nWindows2))
                sURL := accAddressBar.accValue(0)
            }
        }
    }
    If ((sURL != "") and (SubStr(sURL, 1, 4) != "http")) ; Chromium-based browsers omit "http://"
        sURL := "http://" sURL
    Return sURL
}

; "GetAddressBar" based in code by uname
; Found at http://autohotkey.com/board/topic/103178-/?p=637687

GetAddressBar(accObj) {
    Try If ((accObj.accName(0) != "") and IsURL(accObj.accValue(0)))
        Return accObj
    Try If ((accObj.accName(0) != "") and IsURL("http://" accObj.accValue(0))) ; Chromium omits "http://"
        Return accObj
    Try If (InStr(accObj.accDescription(0), accObj.accName(0)) and IsURL(accObj.accDescription(0))) ; Origin Chip support
        Return accObj
    For nChild, accChild in Acc_Children(accObj)
        If IsObject(accAddressBar := GetAddressBar(accChild))
            Return accAddressBar
}

IsURL(sURL) {
    Return RegExMatch(sURL, "^(?<Protocol>https?|ftp)://(?:(?<Username>[^:]+)(?::(?<Password>[^@]+))[email protected])?(?<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"
    }
}

#If InStr( sURL := GetActiveBrowserURL(), "feedly.com" )
    NumpadDot::send {PgDn}
    ^NumpadDot::send .
    NumpadEnter::send A
    ^NumpadEnter::send {Enter}
    Numpad0::send ^w
    ^Numpad0::send 0
#If


LarryC
  • Members
  • 68 posts
  • Last active: Aug 14 2016 07:53 PM
  • Joined: 28 Oct 2012

Tremendous work, Atnbueno:

You wrote, 

 

 

"even got it to work on maxthon"

 

Well, I have not been able to get it to work on maxthon, (others yes, excellent)

Could you indicate anything different you had to do to get it to work.

All it does for  me  is return a number along with my Email address?



atnbueno
  • Members
  • 91 posts
  • Last active: Feb 16 2016 07:04 PM
  • Joined: 24 Mar 2007

Sorry LarryC. I tested Maxthon without any change in configuration.

 

I'm guessing there is something different in your setup (installed extension, logged in Maxthon Passport, ...) that, when travelling through the window components (the GetAddressBar function), something matches an URL (probably your email address) before the real one.

 

Just to see if I'm in the right track, try this (it's a test, not a solution): Delete or comment out the following lines (85-86 in my script)

	Try If ((accObj.accName(0) != "") And IsURL("http://" accObj.accValue(0))) ; Chromium omits "http://"
		Return accObj

If that works I may have a way to fix your problem :-) If not, I have no clue what else to try :-P


Regards,
Antonio

LarryC
  • Members
  • 68 posts
  • Last active: Aug 14 2016 07:53 PM
  • Joined: 28 Oct 2012

Thank you for replying atnbueno:

I commented out those lines, but did not have any positive actions.

However, closely reading your comments made me do a complete uninstall, and reinstall. 

Now it works as advertised. 

Thank you very much, and it is very generous of you to share your skills and knowledge with us.



LarryC
  • Members
  • 68 posts
  • Last active: Aug 14 2016 07:53 PM
  • Joined: 28 Oct 2012

Ok, it is "logged in Maxthon Passport" that prevents getting the URL and returns http:// [a number][myemail]gmail.com, instead.

 

Again to quote you;

 

 

If that works I may have a way to fix your problem :-)

If your words were referring to Maxthon Password, what would be the fix?

 

Android Maxthon and PC Maxthon seem to integrate much smoother and better than Android Chrome and PC Chrome.

For that reason I prefer Maxthon.  I currently use a work around to get Maxthon URL by using send ^!u to capture Maxthon address bar. Not too happy with that.

Regardless thx Atnbueno



atnbueno
  • Members
  • 91 posts
  • Last active: Feb 16 2016 07:04 PM
  • Joined: 24 Mar 2007

I've registered at Maxthon Passport and the scripts works fine here. There's not much I can do if I can't reproduce the problem.

 

But if you don't mind trying things, replace the IsURL function with this one:

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

Regards,
Antonio

LarryC
  • Members
  • 68 posts
  • Last active: Aug 14 2016 07:53 PM
  • Joined: 28 Oct 2012

Well, that works perfect., thanks again, you sure know what you are doing atubueno. 



atnbueno
  • Members
  • 91 posts
  • Last active: Feb 16 2016 07:04 PM
  • Joined: 24 Mar 2007

Thanks, but that was a hack at most. That change implies you won't be able to detect URLs with usernames and/or passwords like ftp://foo:[email protected]

 

If that's ok with you, perfect. If not, we'll have to try something else.


Regards,
Antonio

Bob Munck
  • Members
  • 5 posts
  • Last active: Oct 03 2014 02:57 PM
  • Joined: 26 Sep 2014

My example is just for the homepage. Of course you can change the expression after #If other than WinActive() Try this(works for me):

 

I'm afraid neither of your suggestions works for me. Are you logged on as an actual feedly user, or just going to their home page? You get very different pages in the two situations. When I'm on feedly.com and select the Slashdot feed, I get this URL: 

 

http://feedly.com/in...ashdot/slashdot

 

When I select Google News, I get this:

 

http://feedly.com/in...ic=h&output=rss

 

Using the code you suggested, I do not get my num pad redefined.

 

(I'm also at a complete loss about when a command starts with a pound sign.  You do so for the IF command, but the online documentation never shows anything like that.)



Bob Munck
  • Members
  • 5 posts
  • Last active: Oct 03 2014 02:57 PM
  • Joined: 26 Sep 2014

My example is just for the homepage. Of course you can change the expression after #If other than WinActive() Try this(works for me):

 

I'm afraid neither of your suggestions works for me. Are you logged on as an actual feedly user, or just going to their home page? You get very different pages in the two situations. When I'm on feedly.com and select the Slashdot feed, I get this URL: 

http://feedly.com/index.html#subscription%2Ffeed%2Fhttp%3A%2F%2Frss.slashdot.org%2FSlashdot%2Fslashdot

When I select Google News, I get this:

http://feedly.com/index.html#subscription%2Ffeed%2Fhttp%3A%2F%2Fnews.google.com%2Fnews%3Fned%3Dus%26topic%3Dh%26output%3Drss

Using the code you suggested, I do not get my num pad redefined.

 

(I'm also at a complete loss about when a command starts with a pound sign.  You do so for the IF command, but the online documentation never shows anything like that.)