02: Connecting & starting a page in Chrome and AutoHotkey

Chrome and AutoHotkeyConnecting & starting a page in Chrome and AutoHotkey

In this session GeekDude shows me several ways to connect to an existing Chrome page.


donate to GeekDudeIf you’re loving this, please consider donating to GeekDude!

Connecting & starting a page in Chrome and AutoHotkey


#SingleInstance,Force
#Include  ;Remember to put Chrome in your library folder
;**************************************
;~ page:=Chrome.GetPage() ;Connects to page, but not a specific one
;~ page :=Chrome.GetPageByTitle("Joe","startswith") ;This will connect to a specific tab
page:=Chrome.GetPageByTitle("Joe","startswith",2) ;This will connect to the second index of a specific tab
;~ page.Evaluate("alert('hi');") 
;~ page:=Chrome.GetPageByURL("https://www.google.com","startswith") ;This will connect to the second index of a specific tab
;~ page:=Chrome.GetPageByURL("https://www.google.com","contains") ;This will connect to the second index of a specific tab
If !IsObject(page){
	MsgBox % "That wasn' t object / the page wasn't found"
	ExitApp
}

Connect to tab and page

1:00        Chrome.ahk was developed as a way to connect to Chrome w/o any external tools (unlike Selenium)

1:20        Don’t use Selenium, Use the Chrome.ahk class from GeekDude

1:43        All you need is Chrome and AutoHotkey.  Selenium install can be difficult

2:40        Chrome.ahk talks to Chrome using Chrome’s developer tools protocol.  Meaning if you open-up the page inspector (Control+Shift+I) or “inspect” an element. Anything you can do with the page inspector / debugger, you can do with AutoHotkey on Chrome.

3:20        Because AutoHotkey is external to Chrome you have to start Chrome with a special command line flag.  chrome.exe –remote-debugging-port=9222 . The easiest way to do this is to find your shortcut for launching Chrome (taskbar, Start menu, etc.) go to the shortcut properties and in the “target” add above code.

4:28        Every time you open Chrome, it will allow you to open the devtools window but instead of using the Control+Shift+I shortcut, it uses a network connection which allows you to connect to your Chrome window.

5:07        GeekDude thinks this is a “local” connection (meaning localhost) so it is probably just your computer.

6:16        Close out of any Chrome windows you have open. Be sure you inspect your system tray, Chrome might stay running in the background as will hangouts.  Both of these need to be closed!!!)

6:20        When you reopen, Chrome will be listening for the debugging connections (listening for AutoHotkey to speak to it).

7:10        Create a new AutoHotkey script with your favorite editor.  GeekDude  uses his CodeQuickTester AHK script

7:35        #include your Chrome.ahk class / library.  (I.e.   #Include C:\AutoHotkey_L\Lib\Chrome.ahk)

8:00        Many different ways to start the Chrome Class.  This video we’re connecting to an existing window

8:19        Create a variable Page:=Chrome.GetPage()

9:15        When you’re connecting to Chrome, Chrome separates different items (tabs, Extensions, etc.) into something called a “page”.  Each item has a different logical page.

9:47        Here GeekDude explains how a method in a class works.  Because Chrome is a class and GetPage() is a method in that class, you need to use both to use it  (I was confused because there was no “chrome” variable.    This is called a static Method or static function.  You don’t have to do anything with it, you can just call it.

10:45     Check to confirm the class did find a page.  Use msgbox % IsObject(page).  You should get  1 which means it successfully connected to a page.

12:35     Chrome library on Github has a lot of examples.  We borrow from it to get the page.

13:08     page.Evaluate(“alert(‘hi’);”) ;Call Evaluate on the page variable.

14:01     A method is just a function that acts on an object / class.  You can use Method and function interchangeably here.

14:20     The Evaluate function is sending JavaScript to the debugger console

14:46     The Popup worked, but it was on the “wrong” page.  We wanted the New Tab page

15:02     Go look inside Chrome.ahk and you’ll see several ways to connect to a page.

15:24     GetPage() function but also GetPageByTitle(), GetPageByURL() and GetPageBy()

15:40     The other GetPageBy() methods are less useful for automation.  If you can’t use GetPageByTitle() or GetPageByURL then reach out to him via Discord, email, his website https://www.philipt.net/.

16:31     Change from GetPage to use GetPageByTitle()

17:00     If you have more than 1 tab with the same name, it looks like it gets them in the order they are in (this is in flux)

17:50     Add parameters to the GetPageByTitle to tell it which tab you connect to.  page:=Chrome.GetPageByTitle(“New Tab”, “startswith”,2)

18:49     GetPageByURL – make sure you click several times in Chrome to get the full URL

19:20     You should only need to connect to the page once.  If you connect when it starts, just keep the pointer to that page for the rest of the time.  If you restart your script, you’ll need to reconnect to that page if the title or URL changed

19:52     Currently using the “startswith” matching option.  There is also “contains”  (it can be anywhere in the URL)  e.g. https://www.reddit.com/r/google/ will still be found when searching for “google”.

20:45     “exact” matching is case insensitive.

21:00     Use the various matching to adapt to your needs.

22:11     Change the program to only alert us when it is not an object (!)

 

 

 

Comments are closed.