07: How to Create and connect to new Chrome profiles with AutoHotkey

Automating Chrome with AutoHotkeyIn this short video GeekDude walks us through why we need to consider Chrome profiles and how to create / use them.

Create and connect to new Chrome profiles with AutoHotkey

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

Notes from Create and connect to new Chrome profiles with AutoHotkey

00:09     There’s been a lot of confusion on Chrome profiles.  What they are, why you should concern yourself with them when using AutoHotkey.

00:23     Your Chrome profile is what keeps you logged into websites, connected to Google, etc.  Most of the time you won’t need access to your entire Chrome profile.  But you might want to start with a blank slate if you’re distributing your code to people.  Or you want to create a new instance of Chrome that you don’t want attached to an existing Chrome tab.  For any of those, you need to have a Chrome profile

01:17     This is because most people don’t have the remote debugging flag on their default shortcut.  If you launch chrome with debugging code, it will automatically group it with the current process window.

02:38     So instead of spawning a new Chrome window that is listening to the debug window, it will open a new page on the existing Chrome instance without the debugging access (even though you specified debugging)

02:52     So in order to get Chrome to open a new instance, you need to use the Chrome profile.

03:51     Create a folder  (name it what you want) and tell Chrome to use it the profile flag.  “–user data-dir” with your directory  i.e. “–user data-dir-C:\temp\newProfile”

05:18     Looking in the profile folder, you can see Chrome has generated a bunch of files.  Things like Cookies, browser history, etc.  Everything Chrome remembers…

05:47     If you’re targeting portable Chrome, making sure you have this profile set correctly can be a big deal!  If you use AutoHotkey to launch portable Chrome, it might still load the default profile.  Make sure you specify the Chrome profile!

07:32     Everywhere you would have used Chrome. In your script, use ChromeInst.  (i.e. Instead of Chrome.GetPage use ChromeInst.GetPage.

07:32     That tells Chrome to look for this new / specific instance of Chrome instead of the default version.  Remember, it’s only “new” right after you make it.

08:55     Chrome builds the sub folders / content

Thanks again GeekDude!!!

 

06: Chrome and AutoHotkey: Getting lists from page

Automating and ChromeHere we continue with GeekDude working with Chrome and AutoHotkey extracting data from a webpage.  This session we focus on getting lists and leverage JSON, Chrome.Jxon_Dump, JSON.stringify, Chrome.Jxon_Load and jQuery.

The great news is that GeekDude explained how we can see the Reddit site the way it was the below video!

By logging into https://old.reddit.com/r/AutoHotkey/, the HTML will be the same as in the video!

Chrome and AutoHotkey: Getting lists from page

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

Notes: Chrome and AutoHotkey: Getting lists from page

00:05     Sometimes you want multiple items from a page.  Maybe all post titles, or all links form comments.

00:20     Looking in HTML we see there’s a lot going on.  We need to look at the structure BEFORE we get working on it.

Continue reading

04: Automating Chrome to Set Text & Click a button

Automating Chrome with AutoHotkey

In the fourth session with GeekDude we look at out to Chrome and AutoHotkeyautomate setting text in a search field and then hitting the button to submit the search.

Automating Chrome to Set Text & Click a button


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

AutoHotkey script for Automating Chrome to Set Text & Click a button


#Include   ;Remember to put Chrome in your library folder

#SingleInstance,Force
;**************************************
page:=Chrome.GetPageByTitle("AutoHotkey Community","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
}
page.Evaluate("document.querySelector('#keywords').value ='Chrome.ahk'")
Variable =document.querySelector('#keywords').value ='Chrome.ahk'
page.Evaluate(Variable)
var:="duh"
page.Evaluate("document.querySelector('#keywords').value ='" var "'")
page.Evaluate("document.querySelector('#search > fieldset > button').value ='Chrome.ahk'")

Notes for Automating Chrome to Set Text & Click a button

00:36     Go to AutoHotkey.com/boards/

00:44     Connect to tab using Chrome.GetPageByTitle(“AutoHotkey Community”) ;the default matchtype is “starts with”

01:23     Look at page structure using right-click and Inspect.  This opends Devtools with that element selected.

01:46     It has an ID of “keywords”, copy js path.  Which will give you queryselector(“#keywords”)

02:26     Use the .value to set some text in that box.

03:00     page.Evaluate(“document.querySelector(‘#keywords’).value =’Chrome.ahk'”)

04:01     Make sure inside the JavaScript you use the “=”, not “:=”

04:15     Some people don’t want to have to learn JavaScript.  When using Chrome, you’re going to have to learn JavaScript.

04:56     When using Chrome.ahk, we’re injecting JavaScript.  So best to learn

05:54     The button is right next to the input.  You can go back to the page and right-click the button, then hit Inspect

06:13     Test the new js path.  Instead of using .value, use .click

06:42     Test in Chrome developer tool

07:18     When running an Evaluate method, it waits for the previous Evaluate to finish (so no need to sleep between them).

07:44     If you run into a problem where you think it is happening too quickly, check the forum for some solutions

08:40     Sometimes what you want to input won’t always be a static string.  If you’re trying to reference a variable, you need to use the expression syntax.  In an expression, you’re not just assigning text, you’re doing math or making function calls.

Variable =document.querySelector(‘#keywords’).value =’Chrome.ahk’

page.Evaluate(Variable)

page.Evaluate(“document.querySelector(”#keywords ‘).value ='” variablevar:=”duh”

page.Evaluate(“document.querySelector(‘#keywords’).value ='” var “‘”) “‘”)

10:48     This works because AutoHotkey splits everything up on a given line.   First is a name of a function, then says this is inside the function, then this is text inside a function.  Then builds from left to right as to the string that will be used.

12:15     AutoHotkey proceeds left to right when evaluating an expression

12:40     when you use := you’re in expression assignment mode.

13:25     With just single = you’re in plain-text mode.  It reads it as text

15:00     When automating a site, you don’t know what kind of buffer’s they have to prevent scraping / botting.

15:49     When you start automating, you might start seeing Captcha’s everywhere

16:04     Sites get really good at looking like a normal site to a user, but looking like an impenetrable fortress to code

16:36     If your variable contains a single quote or other special charachters, JavaScript will interpret it as code instead of text.

17:13     JavaScript string escape sequence will replace characters with special escape sequences

Not mentioned in Video but GeekDude wrote me after

You can escape JavaScript code using Coco’s JSON library does actually do that escaping that we discussed when talking about putting data on the page. The syntax for invoking it looks like this:

variable = 123`r`n456’quote”quote

page.Evaluate(“document.querySelector(‘#whatever’).value = ” Chrome.Jxon_Dump(variable))

The dump function will automatically escape anything that needs escaped and add quotes to anything that needs quotes.

 

 

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 (!)