hier hast Du Dein Spielzeug. Das Ganze ist soweit 'getestet', dass AHK beim Laden keine Fehler auswirft. Darüberhinaus gibt es keine Gewähr, d.h. Du verwendest das Skript auf eigenes Risiko.
Die im AU3-Skript verwendeten externen Namen und Pfade für die restlichen Komponenten habe ich so gelassen. Die AU3 _Chrome...() Funktionen habe ich in Chrome_...() umgetauft. Ansonsten sollte es aber genau so funktionieren wie in den AU3-Beispielen, wenn ich keine Fehler eingebaut habe. Bei der Anpassung der Funktionsköpfe bin ich irgendwann steckengeblieben.
Code: Select all
; Chrome_Startup("autohotkey.com") ; Funktionstest
; ================================================================================================================================
; Source: www.autoitscript.com/forum/topic/154439-chrome-udf/
;
; Title: Chrome Automation UDF Library for AutoIt3
; Filename: Chrome.au3
; Description: A collection of functions for automating Chrome
; Author: seangriffin
; Version: V0.5
; Last Update: 29/09/13
; Requirements: AutoIt3 3.2 or higher,
; Chrome v29 (earlier versions untested)
; AutoIT for Google Chrome (Chrome extension)
; Changelog: ---------29/09/13---------- v0.5
; Changed _ChromeStartup() to exit search if chrome.exe is found.
; ---------12/09/13---------- v0.4
; Changed _ChromeStartup() to search various folders for chrome.exe.
; ---------11/09/13---------- v0.3
; Changed Chrome_NativeMessagingHostDir to %APPDATA%.
; Changed _ChromeShutdown() to kill all process instances.
; ---------10/09/13---------- v0.2
; Added _ChromeInputClickByName().
; Added _ChromeObjGetHTMLById().
; Added _ChromeObjGetHTMLByName().
; Added _ChromeObjGetValueByName().
; Added _ChromeObjGetPropertyByName().
; ---------09/09/13---------- v0.1
; Initial release.
; ================================================================================================================================
; Global Variables
Global Chrome_NativeMessagingHostDir := A_AppData . "\AutoIt3\Chrome Native Messaging Host"
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_Startup()
; Description ...: Starts the Chrome browser, with optional URL.
; Syntax.........: Chrome_Startup(Url := "about:blank", ChromePath := "C:\Program Files\Google\Chrome\Application\chrome.exe")
; Parameters ....: Url - Optional: a URL to visit on startup.
; ChromePath - The path to "chrome.exe".
; Return values .: On Success - Returns the PID of the chrome process.
; On Failure - Returns False.
; Author ........: seangriffin
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_Startup(Url := "about:blank", ChromePath := "") {
Static LocalAppData := Chrome_GetLocalAppDataDir()
If (StrLen(ChromePath) = 0)
RegRead, ChromePath, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe ; registry check
If (StrLen(ChromePath) = 0) {
If FileExist(A_ProgramFiles . "\Google\Chrome\Application\chrome.exe") ; program files check
ChromePath := A_ProgramFiles . "\Google\Chrome\Application\chrome.exe"
Else If FileExist(A_ProgramFiles . "\Google\Chrome\chrome.exe")
ChromePath := A_ProgramFiles . "\Google\Chrome\chrome.exe"
Else If FileExist(A_AppData . "\Google\Chrome\Application\chrome.exe") ; roaming application data check
ChromePath := A_AppData . "\Google\Chrome\Application\chrome.exe"
Else If FileExist(A_AppData . "\Google\Chrome\chrome.exe")
ChromePath := A_AppData . "\Google\Chrome\chrome.exe"
Else If FileExist(LocalAppData . "\Google\Chrome\Application\chrome.exe") ; local application data check
ChromePath := LocalAppData . "\Google\Chrome\Application\chrome.exe"
Else If FileExist(LocalAppData . "\Google\Chrome\chrome.exe")
ChromePath := LocalAppData . "\Google\Chrome\Application\chrome.exe"
}
If (StrLen(ChromePath) > 0) {
Run, %ChromePath% "%Url%", , , PID
Return PID
}
Else
Return False
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_Shutdown()
; Description ...: Closes the Chrome browser, and the native messaging host.
; Syntax.........: Chrome_Shutdown()
; Return values .: On Success - Returns nothing.
; On Failure - Returns nothing.
; Author ........: seangriffin
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_Shutdown() {
; Close Chrome if it is currently open
If (ChromeWin := Chrome_WinExists())
WinClose, ahk_id %ChromeWin%
; Close the native messaging host (it consumes CPU if left running)
While (NmhPID := Chrome_NmhExists()) {
Process, Close, %NmhPID%
Sleep, 100
}
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_Eval()
; Description ...: Executes a JavaScript "eval" function against the document loaded in Chrome.
; Syntax.........: Chrome_Eval(javascript_expression, timeout := 5)
; Parameters ....: JSExpression - A JavaScript expression to execute.
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns the result of the "eval" function as a String.
; On Failure - Returns "", and:
; sets ErrorLevel = 3 if there was no response from Chrome.
; sets ErrorLevel = 2 if the input file for the NMH couldn't be written.
; sets ErrorLevel = 1 if Chrome was unavailable.
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_Eval(JSExpression, Timeout := 5) {
Response := ""
If Chrome_WinExists() {
If (File := FileOpen(Chrome_NativeMessagingHostDir . "\input.txt", "w")) {
File.Write(JSExpression)
File.Close()
WaitUntil := A_TickCount + (TimeOut * 1000)
While (A_TickCount < WaitUntil) {
If FileExist(Chrome_NativeMessagingHostDir . "\output.txt")
Break
Sleep, 100
}
If FileExist(Chrome_NativeMessagingHostDir . "\output.txt") {
FileRead, Response, %Chrome_NativeMessagingHostDir%\output.txt
FileDelete, %Chrome_NativeMessagingHostDir%\output.txt
}
Else
ErrorLevel := 3
}
Else
ErrorLevel := 2
}
Else
ErrorLevel := 1
ErrorLevel := 0
Return Response
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_DocWaitForReadyStateCompleted()
; Description ...: Waits for the "readyState" of the document loaded in Chrome to be "complete".
; Syntax.........: Chrome_DocWaitForReadyStateCompleted(imeout := 5)
; Parameters ....: Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns True.
; On Failure - Returns False, and sets ErrorLevel = 1.
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_DocWaitForReadyStateCompleted(Timeout := 5) {
Response := ""
Result := True
Loop
Response := Chrome_Eval("document.readyState;", Timeout)
Until (ErrorLevel > 0) || (StrLen(Response) = 0) || (Response = "complete")
If (ErrorLevel > 0) || (StrLen(Response) = 0) {
Result := False
ErrorLevel := 1
}
Return Result
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_InputClickByName()
; Description ...: Clicks an <input> element based on it's "name" attribute.
; Syntax.........: Chrome_InputClickByType(type, index := 0, timeout := 5)
; Parameters ....: ObjName - the value of the "name" attribute
; Index - Optional: the index of the element if many are found.
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns "".
; On Failure - Returns "", ErrorLevel is set by Chrome_Eval().
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_InputClickByName(ObjName, Index := 0, Timeout = 5) {
Return Chrome_Eval("document.getElementsByName('" . ObjName . "')[" . Index . "].click();", Timeout)
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_InputClickByType()
; Description ...: Clicks an <input> element based on it's "type" attribute.
; Syntax.........: Chrome_InputClickByType(type, timeout := 5)
; Parameters ....: Type - the value of the "type" attribute
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns "".
; On Failure - Returns "", ErrorLevel is set by Chrome_Eval().
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_InputClickByType(Type, Timeout := 5) {
Return Chrome_Eval("_ChromeInputClickByType=" . Type, Timeout)
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_DocGetTitle()
; Description ...: Gets the <title> element of the document loaded in Chrome.
; Syntax.........: Chrome_DocGetTitle(Timeout := 5)
; Parameters ....: Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns the value of the <title> element.
; On Failure - Returns "", ErrorLevel is set by Chrome_Eval().
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_DocGetTitle(Timeout := 5) {
Return Chrome_Eval("document.title;", Timeout)
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_DocWaitForExistenceByTitle()
; Description ...: Waits until the <title> element is a specific value.
; Syntax.........: Chrome_DocWaitForExistenceByTitle(Title, Timeout := 5)
; Parameters ....: Title - the value of the <title> element
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns True.
; On Failure - Returns False.
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_DocWaitForExistenceByTitle(Title, Timeout := 5) {
WaitUntil := A_TickCount + (Timeout * 1000)
DocExists := False
While (!DocExists) && (A_TickCount < WaitUntil) {
If (Title = Chrome_DocGetTitle()) {
DocExists := True
Break
}
Sleep, 100
}
Return DocExists
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_ObjGetHTMLById()
; Description ...: Get the "innerHTML" attribute value of a element based on it's "name" property.
; Syntax.........: Chrome_ObjGetHTMLById(ObjID, Timeout := 5)
; Parameters ....: ObjID - the value of the "id" property
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns the "innerHTML" attribute value.
; On Failure - Returns "".
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_ObjGetHTMLById(ObjID, Timeout := 5) {
Return Chrome_Eval("document.getElementById('" . ObjID . "').innerHTML;", Timeout)
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_ObjGetHTMLByName()
; Description ...: Get the "innerHTML" attribute value of a element based on it's "name" property.
; Syntax.........: Chrome_ObjGetHTMLByName(ObjName, Index := 0, Timeout = 5)
; Parameters ....: ObjName - the value of the "name" property
; Index - Optional: the index of the element if many are found.
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns the "innerHTML" attribute value.
; On Failure - Returns "".
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_ObjGetHTMLByName(ObjName, Index := 0, Timeout := 5) {
Return Chrome_Eval("document.getElementsByName('" . ObjName . "')[" . Index . "].innerHTML;", Timeout)
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_ObjGetHTMLByTagName()
; Description ...: Get the "innerHTML" attribute value of a element based on it's "tagName" property.
; Syntax.........: Chrome_ObjGetHTMLByTagName(TagName, Index := 0, Timeout := 5)
; Parameters ....: TagName - the value of the "tagName" property (i.e. "h1", "p")
; Index - Optional: the index of the element if many are found.
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns the "innerHTML" attribute value.
; On Failure - Returns "".
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_ObjGetHTMLByTagName(TagName, Index := 0, Timeout := 5) {
Return Chrome_Eval("document.getElementsByTagName('" . TagName . "')[" . Index . "].innerHTML;", Timeout)
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_ObjGetValueByName()
; Description ...: Gets the value of an element based on it's "name" attribute.
; Syntax.........: Chrome_ObjGetValueByName(ObjName, Index := 0, Timeout := 5)
; Parameters ....: ObjName - the value of the "name" attribute
; Index - Optional: the index of the element if many are found.
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns Value.
; On Failure - Returns ""
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_ObjGetValueByName(ObjName, Index := 0, Timeout := 5) {
Return Chrome_Eval("document.getElementsByName('" . ObjName . "')[" . Index . "].value;", Timeout)
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_ObjSetValueByName()
; Description ...: Sets the "value" attribute of a element based on it's "name" attribute.
; Syntax.........: Chrome_ObjSetValueByName(ObjName, Value, Index := 0, Timeout := 5)
; Parameters ....: ObjName - the value of the "name" attribute
; Value - The text to set the "value" attribute to
; Index - Optional: the index of the element if many are found.
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns $value.
; On Failure - Returns "", and sets @ERROR = 2.
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_ObjSetValueByName(ObjName, Value, Index := 0, Timeout := 5) {
Return Chrome_Eval("document.getElementsByName('" . ObjName . "')[" . Index . "].value = '" . Value . "';", Timeout)
}
; #FUNCTION# =====================================)===============================================================================
; Name...........: Chrome_InputSetCheckedByName()
; Description ...: Sets the "checked" attribute of an <input> element based on it's "name" attribute.
; Syntax.........: Chrome_InputSetCheckedByName(ObjName, Value, Index := 0, Timeout := 5)
; Parameters ....: ObjName - the value of the "name" attribute
; Value - The boolean to set the "checked" attribute to
; Index - Optional: the index of the element if many are found.
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns Value.
; On Failure - Returns "", and sets @ERROR = 2.
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_InputSetCheckedByName(ObjName, Value, Index := 0, Timeout := 5) {
Value := Value ? "True" : "False"
Return Chrome_Eval("document.getElementsByName('" . ObjName . "')[" . Index . "].checked = " . Value . ";", Timeout)
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_OptionSelectWithTextByObjName()
; Description ...: Selects an <option> element with a specific "text" based on it's <select> element "name" attribute.
; Syntax.........: Chrome_OptionSelectWithTextByObjName(OptionText, ObjName, Timeout := 5)
; Parameters ....: OptionText - the text of the <option> element
; ObjName - the value of the <select> element's "name" attribute
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns "".
; On Failure - Returns "", and sets @ERROR = 2.
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_OptionSelectWithTextByObjName(OptionText, ObjName, Timeout := 5) {
Return Chrome_Eval("_ChromeOptionSelectWithTextByObjName=" . OptionText . "|" . ObjName, Timeout)
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_OptionSelectWithValueByObjName()
; Description ...: Selects an <option> element with a specific "value" based on it's <select> element "name" attribute.
; Syntax.........: Chrome_OptionSelectWithValueByObjName(OptionValue, ObjName, Timeout := 5)
; Parameters ....: OptionValue - the value of the <option> element
; ObjName - the value of the <select> element's "name" attribute
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns $option_value.
; On Failure - Returns "", and sets @ERROR = 2.
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_OptionSelectWithValueByObjName(OptionValue, ObjName, Timeout := 5) {
Return Chrome_Eval("document.getElementsByName('" . ObjName . "')[0].value = '" . OptionValue . "';", Timeout)
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_InputSetCheckedWithValueByName()
; Description ...: Sets the "checked" attribute of an element with a specific "value" based on it's "name" attribute.
; Syntax.........: Chrome_InputSetCheckedWithValueByName(InputValue, ObjName, Checked, Timeout := 5)
; Parameters ....: InputValue - the value of the element
; ObjName - the value of the element's "name" attribute
; Checked - the boolean value of the "checked" attribute
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns "".
; On Failure - Returns "", and sets @ERROR = 2.
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_InputSetCheckedWithValueByName(InputValue, ObjName, Checked, Timeout := 5) {
Checked := Checked ? "True" : "False"
Return Chrome_Eval("_ChromeInputSetCheckedWithValueByName=" . InputValue . "|" . ObjName . "|" . Checked, Timeout)
}
; #FUNCTION# =====================================================================================================================
; Name...........: Chrome_ObjGetPropertyByName()
; Description ...: Get the value of a specific property of a element based on it's "name" property.
; Syntax.........: Chrome_ObjGetPropertyByName(objname, propertyname, index := 0, timeout := 5)
; Parameters ....: ObjName - the value of the "name" property
; PropertyName - the name of the property
; Index - Optional: the index of the element if many are found.
; Timeout - Optional: a number of minutes before exiting the function.
; Return values .: On Success - Returns the "innerHTML" attribute value.
; On Failure - Returns "".
; Author ........: seangriffin
; Modified.......:
; Remarks .......: A prerequisite is that the Chrome browser is open
; (Window title = "[REGEXPTITLE:.*- Google Chrome]").
; Related .......:
; Link ..........:
; Example .......: Yes
; ================================================================================================================================
Chrome_ObjGetPropertyByName(ObjName, PropertyName, Index := 0, Timeout := 5) {
Return Chrome_Eval("document.getElementsByName('" . ObjName . "')[" . Index . "]." . PropertyName . ";", Timeout)
}
; ================================================================================================================================
; Auxiliary functions for internal use.
; ================================================================================================================================
Chrome_GetLocalAppDataDir() {
; CSIDL_LOCAL_APPDATA = 0x001C
VarSetCapacity(SFP, 2 * 260, 0) ; special folder path
Return (DllCall("Shell32.dll\SHGetSpecialFolderPath", "Ptr", 0, "Str", SFP, "Int", 0x1C, "UInt", 0, "UInt") ? SFP : "")
}
; ================================================================================================================================
Chrome_NmhExists() {
Process, Exist, autoit-chrome-native-messaging-host.exe
Return %ErrorLevel%
}
; ================================================================================================================================
Chrome_WinExists() {
TitleMatchMode := A_TitleMatchMode
SetTitleMatchmode, Regex
ChromeWin := WinExist(".*- Google Chrome")
SetTitleMatchmode, %TitleMatchMode%
Return ChromeWin
}