You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
social-media-poster/Tools/ConnecToActiveChromeTab.ahk

120 lines
4.6 KiB
Plaintext

;---ENVIRONMENT---------------------------------------------------------------------
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance, Force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
CoordMode, ToolTip, Screen
CoordMode, Mouse, Screen
FileEncoding, UTF-8-RAW ; Needed for special symbols that are used in video descritions
#Include %A_ScriptDir%\..\Lib\SharedFunctions.ahk
#Include %A_ScriptDir%\..\Lib\Json.ahk
; #Include %A_scriptdir%\RunCMD.ahk
;---VARIABLES-----------------------------------------------------------------------
global driver
; JSVideoDescription = hey there how goes it
WinActivate, ahk_class Chrome_WidgetWin_1
WinGetTitle, Chrome_Window_Title, A
Chrome_Window_Title := StrSplit(Chrome_Window_Title, " - Google Chrome")
Chrome_Window_Title := Chrome_Window_Title[1]
Driver := ConnectToChrome("", ConnectActiveTab := 1, ActiveTabTitle := Chrome_Window_Title) ; open new tab page with with specified profile
;---MAIN SCRIPT---------------------------------------------------------------------
js = return document.querySelector(".header__contain").innerText
try status := driver.executeScript(js)
ExitApp
ConnectToChrome(URL := "", ConnectActiveTab := 0, WinGetActiveChromeTabTitle := ""){
; Msgbox % "WinGetActiveChromeTabTitle: " WinGetActiveChromeTabTitle
IP_Port := "127.0.0.1:9222"
IP_Port_Nr := RegExReplace(IP_Port, ".*:(\d*)", "$1")
if WinExist("ahk_exe Chrome.exe"){
WinGet, pid, PID, ahk_exe chrome.exe
for item in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_Process WHERE ProcessId='" pid "'"){
if RegExMatch(item.CommandLine, "i)--remote-debugging-port=\K\d+", port){
break
}
}
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?
IfMsgBox, Yes
{
Message = Restarting Chrome in Debug Mode
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
While(WinExist("ahk_exe chrome.exe")) {
WinClose, ahk_exe chrome.exe
}
Process, WaitClose, chrome.exe
}
Else{
Msgbox, 4096, Error, Cannot connect to Chrome profile if it is Not running in debug mode. Script Terminating
ExitApp
; @todo: Make this error out to the script result screen
}
}
}
; yuriy's settings
; IniRead, ChromeFilepath, C:\Users\%A_username%\Documents\Autohotkey\Lib\ScriptSettings.ini, Selenium, %A_Computername%, %A_Space%
; Establish Variable with Filepath to be used throughout the script
if(ChromeFilepath = ""){
if(FileExist("C:\Program Files\Google\Chrome\Application\chrome.exe")){
ChromeFilepath = C:\Program Files\Google\Chrome\Application\chrome.exe
}
else if (FileExist("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")){
ChromeFilepath = C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
}
}
if(ChromeFilepath = ""){
Message = Failed to find chrome.exe in the usual locations.
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
MsgBox 0x30, Error!, Unable to find Chrome.exe in the usual locations. `nScript Exiting.
ExitApp
}
if(!winExist("ahk_exe chrome.exe")){
run, %ChromeFilepath% --remote-debugging-port=%IP_Port_Nr% %URL%
}
Driver := ComObjCreate("Selenium.ChromeDriver")
Driver.SetCapability("debuggerAddress", IP_Port)
try Driver.Start()
catch e {
ShowSeleniumErrorMsgbox()
} ; end of catch
if(ConnectActiveTab){
try TotalTabsFound := Driver.Windows.Count
loop, %TotalTabsFound% {
try SeleniumActiveTabTitle := Driver.Title
; Msgbox % "Title: " Title
if(InStr(WinGetActiveChromeTabTitle, SeleniumActiveTabTitle)){
; msgbox, window match found
break
}
try driver.SwitchToNextWindow ;Focuses Selenium on the newly opened/next window.
}
}
; Save current chrome version to ini file
return Driver
}
; -------------------------------/SChrome_Get-------------------------------