Re-worked and cleaned up Chrome Tab indexing and selecting

pull/2/head
Yuriy 2 years ago
parent e7d4b140e0
commit 232e3baf6c

@ -4,12 +4,13 @@
; -------------------------------Variables------------------------------- ; -------------------------------Variables-------------------------------
; Declare global variables here so they don't have to be declared in each script ; Declare global variables here so they don't have to be declared in each script
global DriverURLArray global ChromeTabsURLArray
global DriverTitleArray global DriverTitleArray
global LastWebsitePostURL global LastWebsitePostURL
global ScreenshotsTaken global ScreenshotsTaken
global ChromeFilepath global ChromeFilepath
global CurrentTabURL global CurrentTabURL
global URLOfLastErrorPage
; #Include %A_ScriptDir%\ChromeAutomationFunctions.ahk ; #Include %A_ScriptDir%\ChromeAutomationFunctions.ahk
; #Include %A_ScriptDir%\RunCMD.ahk ; #Include %A_ScriptDir%\RunCMD.ahk
@ -127,6 +128,9 @@ SaveOrPostProgress(Message:="",PostType:=""){
if(InStr(PostType, "DiscordErrorLogging")){ if(InStr(PostType, "DiscordErrorLogging")){
PostToDiscordChannel(Message,DiscordErrorLoggingWebhookBotURL) PostToDiscordChannel(Message,DiscordErrorLoggingWebhookBotURL)
} }
if(InStr(PostType, "DevModeMsgBox")){
DevModeMsgBox(Message)
}
if(InStr(PostType, "DiscordVideos")){ if(InStr(PostType, "DiscordVideos")){
Message := MessageBU Message := MessageBU
PostToDiscordChannel(Message,DiscordVideosWebhookURL) PostToDiscordChannel(Message,DiscordVideosWebhookURL)
@ -766,6 +770,11 @@ CheckSeleniumDriver(){
Return "Failed" Return "Failed"
} }
DriverStatus := 1 DriverStatus := 1
if(ReuseTabs)
CreateArrayOfChromeTabs()
} }
SeleniumConnectToActiveTab(IP_Port := "127.0.0.1:9222"){ SeleniumConnectToActiveTab(IP_Port := "127.0.0.1:9222"){
@ -781,15 +790,17 @@ SeleniumConnectToActiveTab(IP_Port := "127.0.0.1:9222"){
return Driver return Driver
} }
CreateArrayOfTabs() { CreateArrayOfChromeTabs() {
Message = Creating an Array of All Chrome Tabs Message = Creating an Array of All Chrome Tabs
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile") SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
; Create an arrays ; Create an arrays
DriverTitleArray := [] DriverTitleArray := []
DriverURLArray := [] ChromeTabsURLArray := []
try TotalTabsFound := Driver.Windows.Count try TotalTabsFound := Driver.Windows.Count
; Message = TotalTabsFound: %TotalTabsFound%
; DevModeMsgBox(Message)
; DevModeMsgBox(TotalTabsFound) ; DevModeMsgBox(TotalTabsFound)
/* ; if only 1 tab exists, grab info, push to array and return /* ; if only 1 tab exists, grab info, push to array and return
@ -800,7 +811,7 @@ CreateArrayOfTabs() {
if(Title != "") if(Title != "")
DriverTitleArray.Push(Title) DriverTitleArray.Push(Title)
if(URL != "") if(URL != "")
DriverURLArray.Push(URL) ChromeTabsURLArray.Push(URL)
return return
} }
@ -820,14 +831,21 @@ CreateArrayOfTabs() {
; 5 is arbitrary number to do some extra loops to make sure all tabs are gone through ; 5 is arbitrary number to do some extra loops to make sure all tabs are gone through
; most likely not necessary because unloaded tabs cannot be activated ; most likely not necessary because unloaded tabs cannot be activated
TotalLoops := TotalTabsFound + 5 TotalTabLoops := TotalTabsFound + 5
Loop % TotalLoops { ; Message = TotalTabLoops: %TotalTabLoops%
Message = Creating an Array of All Chrome Tabs`nCurrent Loop: %A_index%/%TotalLoops% (Cannot check on unloaded tabs)`nSometimes might get stuck for a short while if there is something loading in active tab ; DevModeMsgBox(message)
Loop % TotalTabLoops {
Message = Creating an Array of All Chrome Tabs`nCurrent Loop: %A_index%/%TotalTabLoops% (Cannot check on unloaded tabs)`nSometimes might get stuck for a short while if there is something loading in active tab
TooltipThis(Message) TooltipThis(Message)
; exit after looping through 1 tab, if only 1 tab found in count ; exit after looping through 1 tab, if only 1 tab found in count
if(TotalTabsFound = 1 AND A_index > TotalTabsFound) if(TotalTabsFound = 1 AND A_index > TotalTabsFound){
return Message = Exiting Early after 1 loop
DevModeMsgBox(message)
}
TabEndTime := A_TickCount - TabFoundStartTime TabEndTime := A_TickCount - TabFoundStartTime
TabEndTimeArray .= TabEndTime . "," TabEndTimeArray .= TabEndTime . ","
@ -838,8 +856,6 @@ CreateArrayOfTabs() {
TabFoundStartTime := A_TickCount TabFoundStartTime := A_TickCount
if(TotalTabsFound < 4 AND A_index = 4)
break
; TimeToGrabCurrentTab := A_TickCount - StartTime ; TimeToGrabCurrentTab := A_TickCount - StartTime
; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab ; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab
@ -851,30 +867,36 @@ CreateArrayOfTabs() {
; TimeToGrabCurrentTab := A_TickCount - StartTime ; TimeToGrabCurrentTab := A_TickCount - StartTime
; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab ; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab
; VAR := HasVal(DriverURLArray, STRINGVAR) ; returns position of value in array ; VAR := HasVal(ChromeTabsURLArray, STRINGVAR) ; returns position of value in array
if(HasVal(DriverURLArray, URL)){ ; returns position of value in array /*
if(HasVal(ChromeTabsURLArray, URL)){ ; returns position of value in array
DevModeMsgBox("Skipping rest of loop and continuing")
Continue Continue
} }
*/
if(Title != "") if(Title != "")
DriverTitleArray.Push(Title) DriverTitleArray.Push(Title)
if(URL != "") if(URL != "")
DriverURLArray.Push(URL) ChromeTabsURLArray.Push(URL)
try driver.SwitchToNextWindow ;Focuses Selenium on the newly opened/next window. try driver.SwitchToNextWindow ;Focuses Selenium on the newly opened/next window.
; DevModeMsgBox("switching to next tab")
} ; end of loop } ; end of loop
; TimeToLoopThroughAllTabs := A_TickCount - StartTime ; TimeToLoopThroughAllTabs := A_TickCount - StartTime
; Message = Time to Loop through all tabs: %TimeToLoopThroughAllTabs% ; Message = Time to Loop through all tabs: %TimeToLoopThroughAllTabs%
; SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging") ; SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
; DevModeMsgBox(Message) ; DevModeMsgBox(Message)
return return
} ; /FUNC
}
InputFilePathIntoOpenWindow(Filepath){ InputFilePathIntoOpenWindow(Filepath){
; Msgbox % "Filepath: " Filepath ; Msgbox % "Filepath: " Filepath
@ -903,68 +925,50 @@ CreateArrayOfTabs() {
} }
; -------------------------------NavigateFromBaseURLTo------------------------------- ; -------------------------------NavigateFromBaseURLTo-------------------------------
NavigateFromBaseURLTo(URL,PageTitle := "TodoDeleteme"){ ExtractBaseURL(URL){
if(!DriverStatus){ ; if not connected to selenium chrome, then re-connect
Status := CheckSeleniumDriver()
if(Status)
Return "Failed"
CreateArrayOfTabs()
}
; Msgbox % CreateArrayOfTabs.Length() ; Display total number of items in the array
ArrayLenght := DriverURLArray.Length() ; Save total number of items in the array
; Msgbox % "ArrayLenght: " ArrayLenght
; Iterate from 1 to the end of the array:
/*
Loop % DriverURLArray.Length(){
; ArrayItem := CreateArrayOfTabs[A_Index]
MsgBox % DriverURLArray[A_Index]
}
*/
; message = URL: %URL%
; DevModeMsgBox(message)
if(ReuseTabs){
URLBase := StrReplace(URL,"https://","") ; remove beginning of URL URLBase := StrReplace(URL,"https://","") ; remove beginning of URL
URLBase := StrReplace(URLBase,"http://","") ; remove beginning of URL
URLBase := StrReplace(URLBase, "www.","") ; remove www if there 1
URLBase := StrSplit(URLBase, "/") ; remove text after url base URLBase := StrSplit(URLBase, "/") ; remove text after url base
URLBase := URLBase[1] URLBase := URLBase[1]
URLBase := StrReplace(URLBase, "www.","") ; remove www if there 1
return URLBase
} }
; Message = URLbase after cleaning: %URLBase%`nOriginal URL: %URL%
; DevModeMsgBox(message)
; check if URLBase is a substring within any of the values in the array ActivateChromeTab(URL){
if(!HasSubstringVal(DriverURLArray, URLBase)) ; convert to baseUR
{ if(InStr(URL, "/")){
; DevModeMsgBox("Creating new tab") URLBase := ExtractBaseURL(URL)
try, run "%ChromeFilepath%" "%URL%"
DriverURLArray.Push(URL) ; Append the new url to the array
NewTabCreated := 1
sleep, 1000
} }
/*
else, { else, {
Message = %URLBase% is the urlbase and it's within the DriverURLArray URLBase := URL
DevModeMsgBox(Message)
} }
*/
; msgbox,
Message = Finding Tab
Message = Finding Tab with URL base: %URLBase%
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile") SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
DevModeMsgBox(Message) DevModeMsgBox(Message)
; if not connected to selenium chrome, then re-connect
if(!DriverStatus){
Status := CheckSeleniumDriver()
if(Status)
Return "Failed"
; CreateArrayOfChromeTabs() ; moved to the end of checkseleniumdriver()
}
TabFoundSuccessfully := TabFoundSuccessfully :=
NumberOfTabLoops := DriverURLArray.Length() NumberOfTabActivationLoops := ChromeTabsURLArray.Length()
Message := "NumberOfTabActivationLoops: " NumberOfTabActivationLoops
DevModeMsgBox(Message)
; NumberOfTabLoops := Driver.Windows.Count + 2 ; NumberOfTabLoops := Driver.Windows.Count + 2
loop % NumberOfTabLoops { ; loop through tabs to find matching tab loop % NumberOfTabActivationLoops { ; loop through tabs to find matching tab
try driver.SwitchToNextWindow() try driver.SwitchToNextWindow()
; sleep, 1000 ; sleep, 1000
@ -972,7 +976,7 @@ CreateArrayOfTabs() {
try CurrentTabTitle := driver.window.title try CurrentTabTitle := driver.window.title
try CurrentTabURL := driver.Url try CurrentTabURL := driver.Url
if(CurrentTabTitle = PageTitle OR InStr(CurrentTabURL, URLBase)){ if(InStr(CurrentTabURL, URLBase)){
Message = Found Tab:.`n%CurrentTabTitle% = %PageTitle%`n%CurrentTabURL% = %URLBase% Message = Found Tab:.`n%CurrentTabTitle% = %PageTitle%`n%CurrentTabURL% = %URLBase%
DevModeMsgBox(message) DevModeMsgBox(message)
@ -980,11 +984,79 @@ CreateArrayOfTabs() {
TabFoundSuccessfully := 1 TabFoundSuccessfully := 1
; msgbox %CurrentTabTitle% = %PageTitle% ; msgbox %CurrentTabTitle% = %PageTitle%
; msgbox %CurrentTabURL% = %URL% ; msgbox %CurrentTabURL% = %URL%
Break return
} }
; / loop through tabs ; / loop through tabs
} }
return "Failed"
}
/*ActivateChromeTab(URL){
; ChromeTabsURLArray
if(!HasSubstringVal(ChromeTabsURLArray, URLBase))
{
try, run "%ChromeFilepath%" "%URL%"
ChromeTabsURLArray.Push(URL) ; Append the new url to the array
NewTabCreated := 1
sleep, 1000
}
}
*/
NavigateFromBaseURLTo(URL,PageTitle := "TodoDeleteme"){
if(!DriverStatus){ ; if not connected to selenium chrome, then re-connect
Status := CheckSeleniumDriver()
if(Status)
Return "Failed"
; CreateArrayOfChromeTabs() ; moved to the end of checkseleniumdriver()
}
; ArrayOfOpenTabsLength := ChromeTabsURLArray.Length() ; Save total number of items in the array
; message = URL: %URL%
; DevModeMsgBox(message)
URLBase := ExtractBaseURL(URL)
/* if(ReuseTabs){
URLBase := StrReplace(URL,"https://","") ; remove beginning of URL
URLBase := StrSplit(URLBase, "/") ; remove text after url base
URLBase := URLBase[1]
URLBase := StrReplace(URLBase, "www.","") ; remove www if there 1
}
*/
; Message = URLbase after cleaning: %URLBase%`nOriginal URL: %URL%
; DevModeMsgBox(message)
; if URLBase is NOT a substring within any of the values in the array
if(!HasSubstringVal(ChromeTabsURLArray, URLBase))
{
; DevModeMsgBox("Creating new tab")
try, run "%ChromeFilepath%" "%URL%"
ChromeTabsURLArray.Push(URL) ; Append the new url to the array
NewTabCreated := 1
sleep, 1000
}
message = NewTabCreated: %NewTabCreated%
DevModeMsgBox(message)
status := ActivateChromeTab(URLBase)
message = ActivateChromeTab Status: %status%
DevModeMsgBox(message)
if(!NewTabCreated){ ; if re-using a tab then we want to re-navigate to URL because it might be the wrong page if(!NewTabCreated){ ; if re-using a tab then we want to re-navigate to URL because it might be the wrong page
@ -1018,6 +1090,10 @@ CreateArrayOfTabs() {
Return Return
} }
; End of Function ; End of Function
; -------------------------------/NavigateFromBaseURL------------------------------- ; -------------------------------/NavigateFromBaseURL-------------------------------
@ -1040,7 +1116,7 @@ SChrome_Get(URL := "", Profile := "Profile 1", IP_Port := "127.0.0.1:9222"){
} }
if (Port=""){ if (Port=""){
MsgBox, 36, ,Chrome Needs to be started in debugging mode in order for Autohotkey to connect to it.`nIs it ok to restart Chrome in debugmode to enable a connection? MsgBox, 36, ,Chrome Needs to be started in debugging mode in order for the Uploader to control it with Selenium.`nAutomatically restart Chrome in debug mode to enable a connection?
IfMsgBox, Yes IfMsgBox, Yes
{ {
Message = Restarting Chrome in Debug Mode Message = Restarting Chrome in Debug Mode
@ -1052,7 +1128,7 @@ SChrome_Get(URL := "", Profile := "Profile 1", IP_Port := "127.0.0.1:9222"){
Process, WaitClose, chrome.exe Process, WaitClose, chrome.exe
} }
Else{ Else{
Msgbox, 4096, Error, Cannot connect to Chrome profile if it is Not running in debug mode. Script Terminating Msgbox, 4096, Error, Cannot connect to Chrome profile if it is Not running in debug mode. Program Exiting
ExitApp ExitApp
; @todo: Make this error out to the script result screen ; @todo: Make this error out to the script result screen
} }
@ -1404,22 +1480,3 @@ SaveDriverURLOFErrorPage(){ ; save the url of the result page. That way if a tab
try URLOfLastErrorPage := driver.URL try URLOfLastErrorPage := driver.URL
} }
FindAndActivateTab(TabURL){
Loop, {
if(A_index = 30)
break
try CurrentTabTitle := driver.window.title
try CurrentTabURL := driver.Url
if(CurrentTabURL = TabURL)
Break
try driver.SwitchToNextWindow()
}
}

Loading…
Cancel
Save