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
@ -40,10 +41,10 @@ CheckForUpdates(GitReleasesAPIURL){
if(ScriptVersion = UpdateVersionNumber OR ScriptVersion > UpdateVersionNumber){ if(ScriptVersion = UpdateVersionNumber OR ScriptVersion > UpdateVersionNumber){
ToolTip ToolTip
return return
} }
else, { else, {
UpdateAvailable := 1 UpdateAvailable := 1
; msgbox, update found! ; msgbox, update found!
Message = Program Update Found Message = Program Update Found
@ -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)
@ -149,11 +153,11 @@ TakeScreenshotOfPage(SaveFilepath := ""){
; sleep, 5000 ; sleep, 5000
if(SaveFilepath = ""){ if(SaveFilepath = ""){
SaveFilepath := ErrorLoggingDirectory SaveFilepath := ErrorLoggingDirectory
} }
; if(!FileExist(SaveFilepath)) ; if(!FileExist(SaveFilepath))
FileCreateDir, %SaveFilepath% FileCreateDir, %SaveFilepath%
/* if(!TakeScreenshotsOfErrors) /* if(!TakeScreenshotsOfErrors)
Return Return
@ -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,140 +867,196 @@ 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(ChromeTabsURLArray, URL)){ ; returns position of value in array
DevModeMsgBox("Skipping rest of loop and continuing")
Continue
}
if(HasVal(DriverURLArray, URL)){ ; returns position of value in array */
Continue
}
if(Title != "")
DriverTitleArray.Push(Title)
if(URL != "")
ChromeTabsURLArray.Push(URL)
if(Title != "")
DriverTitleArray.Push(Title)
if(URL != "")
DriverURLArray.Push(URL)
try driver.SwitchToNextWindow ;Focuses Selenium on the newly opened/next window.
; DevModeMsgBox("switching to next tab")
} ; end of loop
try driver.SwitchToNextWindow ;Focuses Selenium on the newly opened/next window. ; TimeToLoopThroughAllTabs := A_TickCount - StartTime
} ; end of loop ; Message = Time to Loop through all tabs: %TimeToLoopThroughAllTabs%
; SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
; DevModeMsgBox(Message)
; TimeToLoopThroughAllTabs := A_TickCount - StartTime return
; Message = Time to Loop through all tabs: %TimeToLoopThroughAllTabs%
; SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
; DevModeMsgBox(Message)
return
} ; /FUNC
InputFilePathIntoOpenWindow(Filepath){
; Msgbox % "Filepath: " Filepath
Message = Waiting for "Open" window to appear to input filepath into
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
; Tooltip,Waiting for "Open" window to appear to input filepath into,850,0
; WinWait, Open
WinWait,Open,,5 ; Wait for 10 seconds for window
if(ErrorLevel)
{
; msgbox, failed to find window.
Return "Failed"
}
WinActivate, Open
sleep, 1000
ControlSetText, Edit1, %Filepath%, Open
sleep, 1000
ControlSend, Edit1, {Enter}, Open
sleep, 1000
; Do an extra check in case the Open window is still open. }
OpenWindowExist := WinExist("Open")
if(OpenWindowExist) InputFilePathIntoOpenWindow(Filepath){
ControlSend, Edit1, {Enter}, Open ; Msgbox % "Filepath: " Filepath
ToolTip, Message = Waiting for "Open" window to appear to input filepath into
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
; Tooltip,Waiting for "Open" window to appear to input filepath into,850,0
; WinWait, Open
WinWait,Open,,5 ; Wait for 10 seconds for window
if(ErrorLevel)
{
; msgbox, failed to find window.
Return "Failed"
} }
WinActivate, Open
sleep, 1000
ControlSetText, Edit1, %Filepath%, Open
sleep, 1000
ControlSend, Edit1, {Enter}, Open
sleep, 1000
; Do an extra check in case the Open window is still open.
OpenWindowExist := WinExist("Open")
if(OpenWindowExist)
ControlSend, Edit1, {Enter}, Open
ToolTip,
}
; -------------------------------NavigateFromBaseURLTo------------------------------- ; -------------------------------NavigateFromBaseURLTo-------------------------------
NavigateFromBaseURLTo(URL,PageTitle := "TodoDeleteme"){ ExtractBaseURL(URL){
if(!DriverStatus){ ; if not connected to selenium chrome, then re-connect URLBase := StrReplace(URL,"https://","") ; remove beginning of URL
Status := CheckSeleniumDriver() URLBase := StrReplace(URLBase,"http://","") ; remove beginning of URL
if(Status) URLBase := StrReplace(URLBase, "www.","") ; remove www if there 1
Return "Failed" URLBase := StrSplit(URLBase, "/") ; remove text after url base
URLBase := URLBase[1]
CreateArrayOfTabs() return URLBase
} }
; 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]
}
ActivateChromeTab(URL){
; convert to baseUR
if(InStr(URL, "/")){
URLBase := ExtractBaseURL(URL)
}
else, {
URLBase := URL
}
*/
; message = URL: %URL% Message = Finding Tab with URL base: %URLBase%
; DevModeMsgBox(message) SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
DevModeMsgBox(Message)
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% ; if not connected to selenium chrome, then re-connect
; DevModeMsgBox(message) if(!DriverStatus){
Status := CheckSeleniumDriver()
if(Status)
Return "Failed"
; check if URLBase is a substring within any of the values in the array ; CreateArrayOfChromeTabs() ; moved to the end of checkseleniumdriver()
if(!HasSubstringVal(DriverURLArray, URLBase)) }
{
; DevModeMsgBox("Creating new tab")
try, run "%ChromeFilepath%" "%URL%"
DriverURLArray.Push(URL) ; Append the new url to the array
NewTabCreated := 1
sleep, 1000
}
/*
else, {
Message = %URLBase% is the urlbase and it's within the DriverURLArray
DevModeMsgBox(Message)
}
*/
; msgbox,
Message = Finding Tab
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
DevModeMsgBox(Message)
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
; msgbox, looping through tabs ; msgbox, looping through tabs
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%
DevModeMsgBox(message)
TabFoundSuccessfully := 1
; msgbox %CurrentTabTitle% = %PageTitle%
; msgbox %CurrentTabURL% = %URL%
return
}
; / loop through tabs
}
return "Failed"
}
/*ActivateChromeTab(URL){
Message = Found Tab:.`n%CurrentTabTitle% = %PageTitle%`n%CurrentTabURL% = %URLBase%
DevModeMsgBox(message)
TabFoundSuccessfully := 1
; msgbox %CurrentTabTitle% = %PageTitle%
; msgbox %CurrentTabURL% = %URL%
Break
}
; / loop through tabs ; 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