Jump to content

Trying to read URL from Chrome (yes, still and again)


4Eyes
 Share

Recommended Posts

Folks,

I've been trying to read the current URL from Chrome for some time and tried a few different approaches but with little success, however, I see that Au3Info.exe (the window info tool) does this. I.E. with Chrome open and displaying say www.google.com, start Au3Info, choose the 'Visible Text' tab in Au3Info and you see the title of the tab and on the 2nd line, the url.

My latest attempt is this:

$Title = WinGetTitle("[CLASS:Chrome_WidgetWin_0]", "")
ConsoleWrite("Tab title = '" & $Title & "'" & @CRLF)
ConsoleWrite("Text = " & ControlGetText($Title, "", "[CLASS:Chrome_AutocompleteEditView]") & @CRLF)
ConsoleWrite("Text = " & ControlGetText($Title, "", "[CLASS:Chrome_WidgetWin_0]") & @CRLF)

but it doesn't work. It occasionally shows an incorrect title, but never any text.

Before anybody asks, I want this specifically for Chrome, not Firefox or IE.

Any help would be appreciated as I'd love to put this to bed once and for all. I have such a love/hate relationship with Chrome. Grrr! It's an odd beast to be sure.

4Eyes

Link to comment
Share on other sites

This should get you started.

AutoItSetOption("WinTitleMatchMode", 2)
If @error Then Exit
$1 = WinWaitActive(ControlGetText("[CLASS:Chrome_WidgetWin_0]", " ", WinGetTitle("[CLASS:Chrome_WidgetWin_0]")))

If @error Then
    MsgBox(0,"","FAIL!!!!")
Else
    MsgBox(0, "", ControlgetText($1, "", "Chrome_AutocompleteEditView1" ))
    EndIf

The code gives you the tab URL when you activate chrome.

Edited by xJSLRx
Link to comment
Share on other sites

Try this...Chrome has one blank window and if you select the tab that is not focused, you cannot get the text from the address bar. Test by switching the active tab and run the script

$List = WinList("[REGEXPCLASS:Chrome_WidgetWin_0]")
If Not IsArray($List) Then Exit ;Exit if Chrome is not open

For $i = 1 To $List[0][0]
    If Not WinGetTitle($List[$i][1]) Then ContinueLoop  ;skip balnk Chrome Window
    If Not ControlGetText($List[$i][1], "", "[CLASS:Chrome_AutocompleteEditView]") Then ContinueLoop    ;if Active Tab is not seleceted, Text will be blank so skip
    $Title = WinGetTitle($List[$i][1])
    $TabTitle = ControlGetText($Title, "", "[CLASS:Chrome_WidgetWin_0]")
    $TabURL = ControlGetText($Title, "", "[CLASS:Chrome_AutocompleteEditView]")
    ExitLoop
Next

ConsoleWrite("Window Title = " & $Title & @CRLF)
ConsoleWrite("Tab Title = " & $TabTitle & @CRLF)
ConsoleWrite("Tab URL = " & $TabURL & @CRLF)
Edited by Varian
Link to comment
Share on other sites

Here is a much better example. This is a fluid display

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Result Window", 600, 184, -1, -1, -1, BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE))
$Edit = GUICtrlCreateEdit("", 0, 0, 600, 201, $ES_WANTRETURN)
GUICtrlSetFont(-1, 13)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
    If GUICtrlRead($Edit) <> _GetChromeText() Then GUICtrlSetData($Edit, _GetChromeText())
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _GetChromeText()
    Local $Title, $TabTitle, $TabURL
    Local $List = WinList("[REGEXPCLASS:Chrome_WidgetWin_0]")
    If Not IsArray($List) Then Exit
    For $i = 1 To $List[0][0]
        If Not WinGetTitle($List[$i][1]) Then ContinueLoop
        If Not ControlGetText($List[$i][1], "", "[CLASS:Chrome_AutocompleteEditView]") Then ContinueLoop
        $Title = WinGetTitle($List[$i][1])
        $TabTitle = ControlGetText($Title, "", "[CLASS:Chrome_WidgetWin_0]")
        $TabURL = ControlGetText($Title, "", "[CLASS:Chrome_AutocompleteEditView]")
        ExitLoop
    Next
    Return "Window Title = " & $Title & @CRLF & @CRLF & "Tab Title = " & $TabTitle & @CRLF & @CRLF & "Tab URL = " & $TabURL
EndFunc   ;==>_GetChromeText

EDIT:I forgot to declare variables in the function..the script would break on a blank tab or the "New Tab" page. Fixed that now.

Edited by Varian
Link to comment
Share on other sites

xJSLRx,

Thanks for your reply although it didn't help.

Varian,

That hits the spot. Thank you. I tried getting window titles with WinList() and comparing PIDs obtained via ProcessList() but it was a nightmare and didn't work well at all. It looks like I was poking around the edges but just couldn't make it happen. Thanks for your efforts, much appreciated. Now, if I could just win 1 game of MineSweeper in expert mode I could die a happy man.

Regards,

4Eyes

Link to comment
Share on other sites

xJSLRx,

Thanks for your reply although it didn't help.

Varian,

That hits the spot. Thank you. I tried getting window titles with WinList() and comparing PIDs obtained via ProcessList() but it was a nightmare and didn't work well at all. It looks like I was poking around the edges but just couldn't make it happen. Thanks for your efforts, much appreciated. Now, if I could just win 1 game of MineSweeper in expert mode I could die a happy man.

Regards,

4Eyes

No problem, It helped me!

This is an interesting subject and the code I wrote will be pretty useful, especially the navigation part.

AutoItSetOption("WinTitleMatchMode", 2)
If @error Then Exit
While 1
$0 = ControlGetText("[CLASS:Chrome_WidgetWin_0]", " ", WinGetTitle("[CLASS:Chrome_WidgetWin_0]"))
If $0 = "" Then
    ContinueLoop
Else
    $T = ControlgetText($0, "", "Chrome_AutocompleteEditView1")
    IniWrite(@ScriptDir & "\URL.ini","Chrome","URL",$T)
    _1()
EndIf
WEnd

Func _1();This is supposed to check if the url from the ini file differs to the current url
        While 1
        $x = ControlgetText($0, "", "Chrome_AutocompleteEditView1")
        $TT = IniRead(@ScriptDir & "\URL.ini","Chrome","URL",$T)
            MsgBox(0, "", $T)
        If $x = $TT Then
        ContinueLoop
    Else
        ExitLoop
        EndIf
    WEnd
EndFunc

What Im aiming for is recording every url I visit but It still has some bugs, the code above doesn't work but should tell you exactly what I am doing. I'll update it when I finish cause i'm sure some one's gonna get here later via Google and having a larger variety per post is good isn't it?

Edit: No, stick with varian's code or you can edit mine to actually work if my first post's code does not suit you need's. I'm currently encrypting a script and am too into it. I'll look into Chrome's url logging later :)

Edited by xJSLRx
Link to comment
Share on other sites

xJSLRx,

Certainly, variety is the spice of life and there always seems to be more than one way to skin a cat.

If you need to get url's from IE etc as well then there's been some good examples posted here recently.

4Eyes

Link to comment
Share on other sites

  • 2 years later...

Sorry to necro this thread but this method, using [CLASS:Chrome_AutocompleteEditView], doesn't seem to work anymore. I don't want to resort to control sending an F6 or !d to highlight the URL bar and clipget to read it - that seems like it would be prone to failure in certain instances.

Is there any other way to do it besides the ghetto way I've mapped out above?

Thanks guys (and sorry again for the necro but this is the most promising thread I've found).

Link to comment
Share on other sites

Link to comment
Share on other sites

  • 5 years later...

@Skitty

 i know its been a long time but does this code still works for you ?

AutoItSetOption("WinTitleMatchMode", 2)
If @error Then Exit
While 1
$0 = ControlGetText("[CLASS:Chrome_WidgetWin_0]", " ", WinGetTitle("[CLASS:Chrome_WidgetWin_0]"))
If $0 = "" Then
    ContinueLoop
Else
    $T = ControlgetText($0, "", "Chrome_AutocompleteEditView1")
    IniWrite(@ScriptDir & "\URL.ini","Chrome","URL",$T)
    _1()
EndIf
WEnd

Func _1();This is supposed to check if the url from the ini file differs to the current url
        While 1
        $x = ControlgetText($0, "", "Chrome_AutocompleteEditView1")
        $TT = IniRead(@ScriptDir & "\URL.ini","Chrome","URL",$T)
            MsgBox(0, "", $T)
        If $x = $TT Then
        ContinueLoop
    Else
        ExitLoop
        EndIf
    WEnd
EndFunc

I tried using it but it seems it cant read my chrome

Link to comment
Share on other sites

  1. Use simplespy to see base hierarchy
    1. Observation which can be hard is that you do not get the editbox
      This is normally where inspect.exe shows you a better hierarchy
  2. Copy paste the code
    1. Run it step by step by adding exit after a _UIA_Action line and if its not highlighting your recognition string is incorrect
      1. Hard observation is here that you need to use a pane by index as there is not title
        This is normally where inspect.exe helps you out
  3. Get your value of the edit box
  4. Clean up the code by 
    1. remove empty properties
    2. shorten controltype
    3. remove highlighting steps

example code

;~ *** Standard code maintainable ***
#include "UIAWrappers.au3"
AutoItSetOption("MustDeclareVars", 1)

_UIA_setVar("oP1","Title:=.*Google Chrome;controltype:=Pane;class:=Chrome_WidgetWin_1") ;Find control with ID and other property - AutoIt General Help and Support - AutoIt Forums - Google Chrome
_UIA_setVar("oP2","Title:=Google Chrome;controltype:=Pane") ;Google Chrome
_UIA_setVar("oP3","controltype:=Pane;index:=2") ;
_UIA_setVar("oP4","controltype:=Pane")  ;
_UIA_setVar("oP5","controltype:=Pane")  ;
_UIA_setVar("oP6","controltype:=Group;class:=") ;

;~ $oUIElement=_UIA_getObjectByFindAll(".mainwindow", "title:=;ControlType:=UIA_PaneControlTypeId", $treescope_subtree)
;~ _UIA_setVar("oUIElement","Title:=;controltype:=UIA_PaneControlTypeId;class:=") ;ControlType:=UIA_PaneControlTypeId;classname:=")
_UIA_setVar("oUIElement","controltype:=Edit") ;ControlType:=UIA_PaneControlTypeId;classname:=")

;~ Actions split away from logical/technical definition above can come from configfiles

_UIA_Action("oP1","highlight")
_UIA_Action("oP1","setfocus")
_UIA_Action("oP2","highlight")
_UIA_Action("oP2","setfocus")
_UIA_Action("oP3","highlight")
_UIA_Action("oP3","setfocus")
_UIA_Action("oP4","highlight")
_UIA_Action("oP4","setfocus")
_UIA_Action("oP5","highlight")
_UIA_Action("oP5","setfocus")
_UIA_Action("oP6","highlight")
_UIA_Action("oP6","setfocus")

_UIA_action("oUIElement","highlight")
_UIA_action("oUIElement","click")
consolewrite(_UIA_action("oUIElement","getvalue"))

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...