From 03a93f6570cdb4b7c598985a33c3b4036d8c4219 Mon Sep 17 00:00:00 2001 From: Yuriy Date: Sun, 29 Jan 2023 15:05:08 -0500 Subject: [PATCH] Reworked Gab.com Login check and small rework of activating of newly created tabs --- Freedomain Social Media Poster.ahk | 61 +++++++++-------------- Lib/SharedFunctions.ahk | 78 ++++++++++++++++++++++-------- 2 files changed, 83 insertions(+), 56 deletions(-) diff --git a/Freedomain Social Media Poster.ahk b/Freedomain Social Media Poster.ahk index 6867364..f004699 100644 --- a/Freedomain Social Media Poster.ahk +++ b/Freedomain Social Media Poster.ahk @@ -2643,51 +2643,38 @@ if(InStr(CurrentTabURL, "/login")){ SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile") - try CurrentTabURL := driver.Url - if(!InStr(CurrentTabURL, "/compose")){ - Message = Login Cookies Expired. Trying to Re-Login - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile") - - ; navigate to login page - url = https://gab.com/auth/sign_in - try driver.Get(url) ;Open selected URL - try driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding - sleep, 1500 ; sleep long enough for password manager to input password - Xpath = //button[normalize-space()='Log in'] - Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000) - if(Status){ - SaveOrPostProgress(Message:="Failed to Login. Please Login Manually",PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") - SaveDriverURLOFErrorPage() - Return - } - ; navigate to post input page - URLAttempt := NavigateFromBaseURLTo(GabPostURL) - if(URLAttempt = "Failed") - Return - driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding - } - ; Message = Inputting Post ; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile") - SaveOrPostProgress(Message:="Inputting Post Content",PostType:="Tooltip,ErrorLoggingTextFile") - - Xpath = //body/div[@id='gabsocial']/div/div/div/div/div/div[2]/a[1] - Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=1,SleepLength:=500) - if(!Status){ ; if successfully clicked on login button, we are logged out - Message = Login Cookies Expired. Trying to Re-Login - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile") + SaveOrPostProgress(Message:="Checking Login Status",PostType:="Tooltip,ErrorLoggingTextFile") - try driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding - Xpath = //button[normalize-space()='Log in'] - Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000) - sleep, 2000 - URLAttempt := NavigateFromBaseURLTo("https://gab.com/compose") + ; New post input box + Xpath = //div[@data-block='true']//div + Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000) + if(Status){ + SaveOrPostProgress(Message:="Login Expired",PostType:="Tooltip,ErrorLoggingTextFile") + driver.Get("https://gab.com/auth/sign_in") ;go to login page + driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding + if(AutoLogin){ + SaveOrPostProgress(Message:="Checking if chrome input email address",PostType:="Tooltip,ErrorLoggingTextFile") + js = return document.querySelector("#user_email").value; + status := driver.executeScript(js) + if(StrLen(Status) > 0){ + SaveOrPostProgress(Message:="Clicking Login Button",PostType:="Tooltip,ErrorLoggingTextFile") + js = document.querySelector("button[name='button']").click() + status := driver.executeScript(js) + return + } + } + else, { + SaveOrPostProgress(Message:="Login Expired. Please log back in",PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + SaveDriverURLOFErrorPage() + Return + } } - Message = Inputting Post SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile") diff --git a/Lib/SharedFunctions.ahk b/Lib/SharedFunctions.ahk index 55bc0e4..f4a3d46 100644 --- a/Lib/SharedFunctions.ahk +++ b/Lib/SharedFunctions.ahk @@ -788,6 +788,7 @@ CreateArrayOfTabs() { DriverURLArray := [] try TotalTabsFound := Driver.Windows.Count + ; DevModeMsgBox(TotalTabsFound) ; if only 1 tab exists, grab info, push to array and exit early if(TotalTabsFound = 1){ @@ -813,7 +814,7 @@ CreateArrayOfTabs() { StartTime := A_TickCount Loop, { - Message = Creating an Array of All Chrome Tabs`nCurrent Loop: %A_index%/%TotalTabsFound% (Cannot loop through unloaded tabs)`nSometimes might get stuck for a short while if there is something loading in active tab + Message = Creating an Array of All Chrome Tabs`nCurrent Loop: %A_index%/%TotalTabsFound%`nSometimes might get stuck for a short while if there is something loading in active tab TooltipThis(Message) ; SaveOrPostProgress(Message:=URL,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar") @@ -851,6 +852,9 @@ CreateArrayOfTabs() { try Title := Driver.Title try URL := Driver.URL + ; DevModeMsgBox(Title) + ; DevModeMsgBox(URL) + ; TimeToGrabCurrentTab := A_TickCount - StartTime ; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab ; VAR := HasVal(DriverURLArray, STRINGVAR) ; returns position of value in array @@ -915,7 +919,7 @@ if(TotalTabsFound = 1){ SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging") ; DevModeMsgBox(Message) return - ; / END OF FUNC + ; / END OF FUNC } InputFilePathIntoOpenWindow(Filepath){ @@ -962,14 +966,37 @@ if(TotalTabsFound = 1){ URLBase := StrReplace(URLBase, "www.","") } - if(!HasSubstringVal(DriverURLArray, URLBase)) ; if base url is not in tab array, create new tab and push url to array + ; if base url is not in tab array, create new tab and push url to array + if(!HasSubstringVal(DriverURLArray, URLBase)) { - try, run "%ChromeFilepath%" "%URL%" - DriverURLArray.Push(URL) ; Append an item to the array - NewTabCreated := 1 - sleep, 2000 + try CurrentTabTitle := driver.window.title + + ; if currently in new tab navigate to the site in the current tab + if(CurrentTabTitle = "New Tab"){ + try driver.Get(URL) ;Open selected URL + try driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding + catch e { + ; msgbox, error caught. trying something + try driver.switchToalert().accept() + try driver.Get(URL) ;Open selected URL + catch e { + Message = Failed to Navigate to %URL%: Please Check for Any Open Dialogue Boxes + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + Return "Failed" + } + } + return + } + else, { + try, run "%ChromeFilepath%" "%URL%" + DriverURLArray.Push(URL) ; Append an item to the array + NewTabCreated := 1 + ; sleep, 2000 + } + + + } - ; msgbox, Message = Finding Tab SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile") @@ -978,37 +1005,50 @@ if(TotalTabsFound = 1){ NumberOfTabLoops := DriverURLArray.Length() - ; NumberOfTabLoops := Driver.Windows.Count + 2 - loop % NumberOfTabLoops { ; loop through tabs to find matching tab + ; loop through tabs to find matching tab + loop % NumberOfTabLoops { try driver.SwitchToNextWindow() ; sleep, 1000 ; msgbox, looping through tabs try CurrentTabTitle := driver.window.title + try CurrentTabURL := driver.Url + + ; DevModeMsgBox(CurrentTabURL) + ; DevModeMsgBox(CurrentTabTitle) ; caused by tab being manually closed. Not sure if this is the only cause though. if(CurrentTabTitle = "") { Message = CurrentTabTitle: %CurrentTabTitle% - DevModeMsgBox(Message) + ; DevModeMsgBox(Message) Message = CurrenttabURL: %CurrentTabURL% - DevModeMsgBox(Message) + ; DevModeMsgBox(Message) ; try driver.SwitchToNextWindow Continue } - try CurrentTabURL := driver.Url - if(InStr(CurrentTabURL, URLBase)){ ; msgbox, found it.`n%CurrentTabTitle% = %PageTitle%`n%CurrentTabURL% = %URLBase% TabFoundSuccessfully := 1 - ; msgbox %CurrentTabTitle% = %PageTitle% + ; message = %CurrentTabTitle%: %PageTitle% + ; DevModeMsgBox(message) ; msgbox %CurrentTabURL% = %URL% Break } - } + } + + ; if failed to switch to tab and a new tab was not created, then chrome failed to switch to the tab + ; this happens sometimes. most likely to a chromedriver bug or maybe tab being offloaded? + if(!TabFoundSuccessfully AND !NewTabCreated){ + ; @todo : not sure how to best solve this problem currently. It doesn't happen very often. + ; last time it happened was when there were only 2 chrome tabs open, gab + subscribestar. chrome was stuck on subscribestar page and wouldn't nextwindow() to gab + } + + ; Message = TabFoundSuccessfully: %TabFoundSuccessfully% + ; DevModeMsgBox(Message) if(!NewTabCreated){ ; if re-using a tab then we want to re-navigate to URL because it might be the wrong page try driver.Get(URL) ;Open selected URL @@ -1088,8 +1128,8 @@ SChrome_Get(URL := "", Profile := "Profile 1", IP_Port := "127.0.0.1:9222"){ if(!winExist("ahk_exe chrome.exe")){ message = %ChromeFilepath% --remote-debugging-port=%IP_Port_Nr% %URL% - Clipboard := Message - DevModeMsgBox(message) + ; Clipboard := Message + ; DevModeMsgBox(message) run, %ChromeFilepath% --remote-debugging-port=%IP_Port_Nr% %URL% } @@ -1432,7 +1472,7 @@ FindAndActivateTab(TabURL){ ; DevModeMsgBox(CurrentTabTitle) try CurrentTabURL := driver.Url - DevModeMsgBox(CurrentTabURL) + ; DevModeMsgBox(CurrentTabURL) if(CurrentTabURL = TabURL) Break