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.
video-uploader/Tools/ConnectToActiveChromeTab.ahk

149 lines
5.3 KiB
Plaintext

; Information
;----------------------------------------------------------------------------------
; This script can be used to connect to your currently active Chrome tab and for quick debugging purposes
;---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
;---VARIABLES-----------------------------------------------------------------------
global driver
global DevMode
DevMode := 1
; Get Title of current chrome window so Selenium can find and connect to it
WinActivate, ahk_class Chrome_WidgetWin_1
WinGetTitle, Chrome_Window_Title, A
Chrome_Window_Title := StrSplit(Chrome_Window_Title, " - Chromium")
Chrome_Window_Title := Chrome_Window_Title[1]
Driver := ConnectToChromeTab("", ConnectActiveTab := 1, ActiveTabTitle := Chrome_Window_Title) ; open new tab page with with specified profile
;---MAIN SCRIPT---------------------------------------------------------------------
ExitApp
Escape::ExitApp
; Libraries - All are included because they call on functions from each other
;-----------------------------------------------------------------------------------
#include %A_ScriptDir%\..\Lib\Freedomain-Posters-Shared-Functions\StdOutToVar.ahk
#Include %A_ScriptDir%\..\Lib\Freedomain-Posters-Shared-Functions\General-Functions.ahk
#Include %A_ScriptDir%\..\Lib\Freedomain-Posters-Shared-Functions\Chrome-Functions.ahk
#Include %A_ScriptDir%\..\Lib\Freedomain-Posters-Shared-Functions\Selenium-Functions.ahk
#Include %A_ScriptDir%\..\Lib\Freedomain-Posters-Shared-Functions\Gitea-Functions.ahk
#Include %A_ScriptDir%\..\Lib\Freedomain-Posters-Shared-Functions\API-Functions.ahk
#Include %A_ScriptDir%\..\Lib\Freedomain-Posters-Shared-Functions\JSON.ahk
#Include %A_ScriptDir%\..\Lib\Freedomain-Posters-Shared-Functions\RunCMD.ahk
ConnectToChromeTab(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")
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
}
}
}
; 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")
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-------------------------------