From ad4565bdc7cbd91ac33adbfcc7cd7b05e8eed710 Mon Sep 17 00:00:00 2001 From: yuriy Date: Tue, 28 Feb 2023 00:26:43 -0500 Subject: [PATCH] Added functions from Social Media Poster Shared Functions file so SharedFunctions.ahk can be replaced by this submodule --- .gitmodules | 3 + Lib/Freedomain-Posters-Shared-Functions | 1 + Lib/SharedFunctions.ahk | 318 ++++++++++++------------ 3 files changed, 165 insertions(+), 157 deletions(-) create mode 100644 .gitmodules create mode 160000 Lib/Freedomain-Posters-Shared-Functions diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c4b7322 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Lib/Freedomain-Posters-Shared-Functions"] + path = Lib/Freedomain-Posters-Shared-Functions + url = https://git.freedomainplaylists.com/yuriy/Freedomain-Posters-Shared-Functions.git diff --git a/Lib/Freedomain-Posters-Shared-Functions b/Lib/Freedomain-Posters-Shared-Functions new file mode 160000 index 0000000..694c440 --- /dev/null +++ b/Lib/Freedomain-Posters-Shared-Functions @@ -0,0 +1 @@ +Subproject commit 694c440a6a89b0b84fee0c49370d95150667c7a9 diff --git a/Lib/SharedFunctions.ahk b/Lib/SharedFunctions.ahk index 213d184..dca99c4 100644 --- a/Lib/SharedFunctions.ahk +++ b/Lib/SharedFunctions.ahk @@ -15,6 +15,7 @@ global CurrentTabURL ; #Include %A_ScriptDir%\RunCMD.ahk +; @todo: add error checks for all winhttp requests. DevModeMsgBox(Message){ @@ -26,7 +27,7 @@ DevModeMsgBox(Message){ } -CheckForUpdates(){ +CheckForUpdates(GitReleasesAPIURL){ ; msgbox, checking for updates Message = Checking For Updates SaveOrPostProgress(Message,PostType:="ErrorLoggingTextFile") @@ -39,10 +40,10 @@ CheckForUpdates(){ UpdateVersionNumber := parsed.1.name if(ScriptVersion = UpdateVersionNumber OR ScriptVersion > UpdateVersionNumber){ - ToolTip - return - } - else, { + ToolTip + return + } + else, { UpdateAvailable := 1 ; msgbox, update found! Message = Program Update Found @@ -148,24 +149,24 @@ TakeScreenshotOfPage(SaveFilepath := ""){ ; sleep, 5000 if(SaveFilepath = ""){ - SaveFilepath := ErrorLoggingDirectory - } + SaveFilepath := ErrorLoggingDirectory + } - ; if(!FileExist(SaveFilepath)) - FileCreateDir, %SaveFilepath% + ; if(!FileExist(SaveFilepath)) + FileCreateDir, %SaveFilepath% - if(!TakeScreenshotsOfErrors) - Return + if(!TakeScreenshotsOfErrors) + Return - ; Take a screenshot of the page and save it. - FormatTime, TodayDate , YYYYMMDDHH24MISS, yyyyMMdd_hhmmss + ; Take a screenshot of the page and save it. + FormatTime, TodayDate , YYYYMMDDHH24MISS, yyyyMMdd_hhmmss - ; Msgbox % "ErrorLoggingDirectory: " ErrorLoggingDirectory - savepath = %SaveFilepath%\%TodayDate%.jpg - ; Msgbox % "savepath: " savepath - try driver.TakeScreenshot().SaveAs(savepath) + ; Msgbox % "ErrorLoggingDirectory: " ErrorLoggingDirectory + savepath = %SaveFilepath%\%TodayDate%.jpg + ; Msgbox % "savepath: " savepath + try driver.TakeScreenshot().SaveAs(savepath) - ; ScreenshotsTaken := 1 + ; ScreenshotsTaken := 1 } @@ -174,6 +175,7 @@ TakeScreenshotOfPage(SaveFilepath := ""){ ; -------------------------------TooltipThis------------------------------- TooltipThis(String){ + ; msgbox ; Xposition := StrLen(String) StringFirstLine := StrSplit(String, "`n") StringFirstLine := StringFirstLine[1] @@ -922,173 +924,175 @@ if(TotalTabsFound = 1){ ; / END OF 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) - ControlSend, Edit1, {Enter}, Open - ToolTip, - } +/* +*/ +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) + ControlSend, Edit1, {Enter}, Open + ToolTip, +} - CheckCurrentTabForCurrentSite(){ - try CurrentTabURL := driver.url +CheckCurrentTabForCurrentSite(){ + try CurrentTabURL := driver.url - if(!InStr(CurrentTabURL, CurrentSite)){ - Message = Chromedriver failed to switch to %CurrentSite%. Please see errorlog for website it got stuck on. - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") - return "Failed" - } + if(!InStr(CurrentTabURL, CurrentSite)){ + Message = Chromedriver failed to switch to %CurrentSite%. Please see errorlog for website it got stuck on. + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + return "Failed" + } - ; msgbox % InStr(CurrentTabURL,CurrentSite) + ; msgbox % InStr(CurrentTabURL,CurrentSite) - } +} - ; -------------------------------NavigateFromBaseURLTo------------------------------- - NavigateFromBaseURLTo(URL){ - if(!DriverStatus){ ; if not connected to selenium chrome, then re-connect - Status := CheckSeleniumDriver() - if(Status) - Return "Failed" +; -------------------------------NavigateFromBaseURLTo------------------------------- +NavigateFromBaseURLTo(URL, Title:="REmoveMe"){ + if(!DriverStatus){ ; if not connected to selenium chrome, then re-connect + Status := CheckSeleniumDriver() + if(Status) + Return "Failed" - CreateArrayOfTabs() ; store all open tabs to memory for later usage - } + CreateArrayOfTabs() ; store all open tabs to memory for later usage + } - if(ReuseTabs){ ; pull out base of URL and check if it's within the array of tabs - URLBase := StrReplace(URL,"https://","") - URLBase := StrSplit(URLBase, "/") - URLBase := URLBase[1] - URLBase := StrReplace(URLBase, "www.","") - } + if(ReuseTabs){ ; pull out base of URL and check if it's within the array of tabs + URLBase := StrReplace(URL,"https://","") + URLBase := StrSplit(URLBase, "/") + URLBase := URLBase[1] + URLBase := StrReplace(URLBase, "www.","") +} - ; if base url is not in tab array, create new tab and push url to array - if(!HasSubstringVal(DriverURLArray, URLBase)) - { - 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 +; if base url is not in tab array, create new tab and push url to array +if(!HasSubstringVal(DriverURLArray, URLBase)) +{ + 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 + } - } +} - Message = Finding Tab - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile") +Message = Finding Tab +SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile") - TabFoundSuccessfully := +TabFoundSuccessfully := - NumberOfTabLoops := DriverURLArray.Length() +NumberOfTabLoops := DriverURLArray.Length() - ; loop through tabs to find matching tab - loop % NumberOfTabLoops { - try driver.SwitchToNextWindow() +; 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 + ; sleep, 1000 + ; msgbox, looping through tabs + try CurrentTabTitle := driver.window.title + try CurrentTabURL := driver.Url - ; DevModeMsgBox(CurrentTabURL) - ; DevModeMsgBox(CurrentTabTitle) + ; 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) + ; caused by tab being manually closed. Not sure if this is the only cause though. + if(CurrentTabTitle = "") { + Message = CurrentTabTitle: %CurrentTabTitle% + ; DevModeMsgBox(Message) - Message = CurrenttabURL: %CurrentTabURL% - ; DevModeMsgBox(Message) + Message = CurrenttabURL: %CurrentTabURL% + ; DevModeMsgBox(Message) - ; try driver.SwitchToNextWindow - Continue - } + ; try driver.SwitchToNextWindow + Continue +} - if(InStr(CurrentTabURL, URLBase)){ - Message = CurrentTabURL is %CurrentTabURL% and it contains %URLBase% - SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile") +if(InStr(CurrentTabURL, URLBase)){ + Message = CurrentTabURL is %CurrentTabURL% and it contains %URLBase% + SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile") - ; msgbox, found it.`n%CurrentTabTitle% = %PageTitle%`n%CurrentTabURL% = %URLBase% - TabFoundSuccessfully := 1 - ; message = %CurrentTabTitle%: %PageTitle% - ; DevModeMsgBox(message) - ; msgbox %CurrentTabURL% = %URL% - Break - } - } + ; msgbox, found it.`n%CurrentTabTitle% = %PageTitle%`n%CurrentTabURL% = %URLBase% + TabFoundSuccessfully := 1 + ; 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 - } +; 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) +; 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 +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 + 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 - 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" - } + Message = Failed to Navigate to %URL%: Please Check for Any Open Dialogue Boxes + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + Return "Failed" } } - ; msgbox, no error caught - ; } +} +; msgbox, no error caught +; } - Return +Return } ; End of Function ; -------------------------------/NavigateFromBaseURL------------------------------- @@ -1165,19 +1169,19 @@ SChrome_Get(URL := "", Profile := "Profile 1", IP_Port := "127.0.0.1:9222"){ try Driver.Start() catch e { ShowSeleniumErrorMsgbox() - } + } - return Driver - } - ; -------------------------------/SChrome_Get------------------------------- + return Driver + } + ; -------------------------------/SChrome_Get------------------------------- - ShowSeleniumErrorMsgbox(){ - GetChromeVersionCommand = powershell (Get-Item '%ChromeFilepath%').VersionInfo.ProductVersion - Chromeversion := RunCMD(GetChromeVersionCommand) - ChromeVersion := StrReplace(ChromeVersion, "`n", "") + ShowSeleniumErrorMsgbox(){ + GetChromeVersionCommand = powershell (Get-Item '%ChromeFilepath%').VersionInfo.ProductVersion + Chromeversion := RunCMD(GetChromeVersionCommand) + ChromeVersion := StrReplace(ChromeVersion, "`n", "") - Message = Failed to connect to Chrome - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + Message = Failed to connect to Chrome + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") /* ; check for alert popup