Meine erste Class -> wb (=WebBrowser)
Posted: 08 Jan 2018, 07:06
Unten ist mein erster ernsthafter Versuch eine eigene Class zu erstellen.
Im momentanen Status der wb-Class, bitte ich um Rueckmeldungen, wenn grobe Fehler oder Ungeschicktheiten im Aufbau der wb-Class vorhanden sind. Ansonsten warte ich, bis es bei https://autohotkey.com/boards/viewtopic.php?f=7&t=41332 weiter geht, um dem Ziel naeher zu kommen, Browser-Steuerung einfacher zu ermoeglichen.
Im momentanen Status der wb-Class, bitte ich um Rueckmeldungen, wenn grobe Fehler oder Ungeschicktheiten im Aufbau der wb-Class vorhanden sind. Ansonsten warte ich, bis es bei https://autohotkey.com/boards/viewtopic.php?f=7&t=41332 weiter geht, um dem Ziel naeher zu kommen, Browser-Steuerung einfacher zu ermoeglichen.
Code: Select all
; Danke nnnik https://autohotkey.com/boards/viewtopic.php?f=7&t=41332
; Danke Beteiligte https://autohotkey.com/board/topic/64563-basic-ahk-v11-com-tutorial-for-webpages/
; Danke Glines http://the-automator.com/tag/web-scraping-intro-with-autohotkey/
; Danke justme ohne den ich nicht so weit gekommen waere
; Mein erster ernsthafter Versuch, der Erstellung einer eigenen Class
class wb {
__New( WbName ) {
this.name := WbName
if(WbName="IE_"){ ; Wenn der New wb(Uebergabe-Parameter) exakt "IE_" enthaelt, dann wird das zuletzt benutzte IE_Reiter-Fenster uebernommen.
this.Com := this.WBGet()
; this.Com := WBGet()
}
else if (SubStr(WbName,1,3)="IE_") ; Wenn der New wb(Uebergabe-Parameter) mit "IE_" beginnt, dann wird ein neues IE_Reiter-Fenster erstellt.
{
this.Com := ComObjCreate("InternetExplorer.Application")
Sleep 300
}
else ; Wenn der New wb(Uebergabe-Parameter) nicht mit "IE_" beginnt, dann ... --> Reserviert fuer andere Browser
{
StringSplit,1Browser2RestName,WbName,_
if (1Browser2RestName0 < 2 OR 1Browser2RestName1 <> "IE")
MsgBox,0,% A_LineFile "[" A_LineNumber "]", Uebergabe-Parameter >%WbName%< nicht unterstuetzt. `n`nIE_`nIE_...`nerwartet. `n`nBrowser >%1Browser2RestName1%< noch nicht unterstuetzt!,10
}
}
getUrl() {
return this.com.LocationURL ;grab current url
}
getUrlAfterQuestionMark() {
return this.com.document.location.Search ; gets substring of URL following question mark
}
getUrlPathname() {
return this.com.document.location.pathname ;returns pathname
}
getLocationName() {
return this.com.LocationName ;grab page Titlerite
}
getUrlHostName() {
return this.com.document.location.hostname ;returns host
}
getUrlAfterHash() {
return this.com.document.location.hash ;retreives everyting from the # on
}
getUserAgent() {
return this.com.document.parentWindow.navigator.userAgent ;Get User Agent
}
getSourceCode(){
SourceCode := this.com.document.documentElement.innerHtml
this.NotBusy()
return SourceCode
}
getAllText(){
AllText:=this.com.document.documentElement.innerText ;Get All text on page
this.NotBusy()
return AllText
}
getUrlProtocol(){
return this.com.document.location.protocol ;retreives the protocol (http, https, etc)
}
refresh() {
this.com.refresh ;Reload page
this.NotBusy()
return
}
getAllLinks(){
Links := this.Com.Document.Links ; collection of hyperlinks on the page
Loop % Links.Length ; Loop through links
If ((Link := Links[A_Index-1].InnerText) != "") { ; if the link is not blank
(OuterHTML := Links[A_Index-1].OuterHTML) ; Grab outerHTML for each link
Link:=StrReplace(Link,"`n")
Link:=StrReplace(Link,"`r")
Link:=StrReplace(Link,"`t")
OuterHTML:=StrReplace(OuterHTML,"`n")
OuterHTML:=StrReplace(OuterHTML,"`r")
OuterHTML:=StrReplace(OuterHTML,"`t")
Msg .= A_Index-1 A_Space A_Space A_Space Link A_Tab OuterHTML "`r`n" ; add items to the msg list
}
this.NotBusy()
return Msg
}
FocusClick(ID:="",Name:="",ClassName:="",TagName:=""){
Dashes:=false
if ID contains -,_,¯
Dashes:=true
if (ID<>"")
{
if Dashes
this.Com.document.getElementByID(ID).focus() ; Acts like clicking the link
else
this.Com.document.all[ID].focus()
this.NotBusy()
if Dashes
this.Com.document.getElementByID(ID).click() ; Acts like clicking the link
else
this.Com.document.all[ID].click()
this.NotBusy()
}
if (Name<>"")
{
this.Com.document.GetElementsByName(Name)[0].focus()
this.NotBusy()
this.Com.document.GetElementsByName(Name)[0].click()
this.NotBusy()
}
if (ClassName<>"")
{
this.Com.document.GetElementsByName(ClassName)[0].focus()
this.NotBusy()
this.Com.document.GetElementsByName(ClassName)[0].click()
this.NotBusy()
}
if (TagName<>"")
{
this.Com.document.GetElementsByName(TagName)[0].focus()
this.NotBusy()
this.Com.document.GetElementsByName(TagName)[0].click()
this.NotBusy()
}
; this.NotBusy()
}
FeldEintrag(SuchString,ID:="",Name:="",ClassName:="",TagName:="") {
try
{
SuchStringProbe:=this.Com.document.getElementByID(ID).Value ;Unique ID-with dashes ; Existiert die ID
this.Com.document.getElementByID(ID).Value :=SuchString ;Unique ID-with dashes ; Wert eintragen
this.NotBusy()
SuchStringProbe:=this.Com.document.getElementByID(ID).Value ;Unique ID-with dashes ; eingetragenen Wet fuer Probe auslesen
}
if (SuchStringProbe=SuchString)
Erfolreich.="ID = " ID
else
{
Loop 100 {
NullIndex:=A_Index-1
try{
; MsgBox %ID% nicht gefunden
SuchStringProbe:=this.Com.document.GetElementsByName(Name)[NullIndex].Value ;Object Name- Get array value
this.Com.document.GetElementsByName(Name)[NullIndex].Value := SuchString
this.NotBusy()
SuchStringProbe:=this.Com.document.GetElementsByName(Name)[NullIndex].Value ;Object Name- Get array value
; MsgBox %SuchStringProbe%
if (SuchStringProbe=SuchString)
{
; MsgBox % NullIndex " " SuchStringProbe
break
}
}
}
if (SuchStringProbe=SuchString)
Erfolreich.="Name[" NullIndex "] = " Name
else
{
Loop 100 {
NullIndex:=A_Index-1
try{
SuchStringProbe:=this.Com.document.getElementsByClassName(ClassName)[NullIndex].Value ;Get classname and Array value
this.Com.document.getElementsByClassName(ClassName)[NullIndex].Value := SuchString
this.NotBusy()
SuchStringProbe:=this.Com.document.getElementsByClassName(ClassName)[NullIndex].Value ;Get classname and Array value
if (SuchStringProbe=SuchString)
{
; MsgBox % NullIndex " " SuchStringProbe
break
}
}
}
if (SuchStringProbe=SuchString)
Erfolreich.="ClassName[" NullIndex "] = " ClassName
else
{
Loop 100 {
NullIndex:=A_Index-1
try{
SuchStringProbe:=this.Com.document.GetElementsByTagName(TagName)[NullIndex].Value ;Get Tagname and Array value
this.Com.document.GetElementsByTagName(TagName)[NullIndex].Value := SuchString
this.NotBusy()
SuchStringProbe:=this.Com.document.GetElementsByTagName(TagName)[NullIndex].Value ;Get Tagname and Array value
if (SuchStringProbe=SuchString)
{
; MsgBox % NullIndex " " SuchStringProbe
break
}
}
}
if (SuchStringProbe=SuchString)
Erfolreich.="TagName[" NullIndex "] = " TagName
else
{
; MsgBox Der Text konnte nicht ins Feld eingetragen werden.
Erfolreich:=false
}
}
}
}
this.NotBusy()
return Erfolreich
}
Visible(flag:=true){
this.Com.Visible := flag
; this.NotBusy() ; Achtung, darf nicht rein. Ohne visible kein busy-Ende !!!!!!!!!!!!!!!!!!
}
Navigate(Url){
this.Com.Navigate(Url)
this.NotBusy()
}
Help(Methode){
if(Methode="")
return "geplant: wb-Class-Hilfe + Liste aller Methoden + Beschreibungen"
else
return "geplant: Liste aller Methoden + Beschreibungen, gefiltert nach >" Methode "<"
}
NotBusy(){
while this.Com.busy or this.Com.ReadyState != 4
Sleep 10
}
delete() {
; %ObjName%:=""
this.Com:=""
}
; Folgende Funktion (danke Glines) ist hier fuer den Class-internen Aufruf vorgesehen.
;************Pointer to Open IE Window******************
WBGet(WinTitle="ahk_class IEFrame", Svr#=1) { ;// based on ComObjQuery docs
static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
, IID := "{0002DF05-0000-0000-C000-000000000046}" ;// IID_IWebBrowserApp
;// , IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ;// IID_IHTMLWindow2
SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
if (ErrorLevel != "FAIL") {
lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
}
}
} ; written by Glines
}
;
; S1 := new wb("Edge_S1") ; Test nicht unterstuetzter Browser
WinActivate,ahk_exe iexplore.exe
WinWaitActive,ahk_exe iexplore.exe,,10
Sleep 100
IfWinActive,ahk_exe iexplore.exe
{
SoundBeep
S1 := new wb("IE_")
MsgBox,0,% A_LineFile "[" A_LineNumber "]", % S1.getUrl(),4
}
else
{
S1 := new wb("IE_S1")
S1.Visible(1)
}
; S2 := new wb("IE_S2") ; 2. Browser Sitzung
; S2.Visible(1)
S1.Navigate("https://autohotkey.com/boards/viewtopic.php?f=7&t=41332")
; S2.Navigate("www.conrad.de")
MsgBox,0,% A_LineFile "[" A_LineNumber "]", % S1.getUrl() " " S1.getLocationName() " alle Links:`r`n" S1.getAllLinks(),5
MsgBox,0,% A_LineFile "[" A_LineNumber "]", % S1.getUrl() " " S1.getLocationName() " GesammtText:`r`n" S1.getAllText(),5
MsgBox,0,% A_LineFile "[" A_LineNumber "]", % S1.getUrl() " " S1.getLocationName() " QuellText:`r`n" S1.getSourceCode(),5
sleep 300
S1.Navigate("https://autohotkey.com/boards/viewtopic.php?f=7&t=41332&p=192787#p192787")
MsgBox,0,% A_LineFile "[" A_LineNumber "]", % S1.getUrlAfterHash() ,2
S1.refresh()
; sleep 2000
S1.Navigate("Google.de")
MsgBox,0,% A_LineFile "[" A_LineNumber "]", % S1.FeldEintrag("Hugo","lst-ib","q","gsfi","input"),2
; sleep 1000
S1.FocusClick("gsr")
S1.FocusClick("","btnK")
MsgBox,0,% A_LineFile "[" A_LineNumber "]", % S1.getUrlAfterQuestionMark(),3
MsgBox,0,% A_LineFile "[" A_LineNumber "]", % S1.getUrlPathname(),2
MsgBox,0,% A_LineFile "[" A_LineNumber "]", % S1.getUrlHostname(),2
MsgBox,0,% A_LineFile "[" A_LineNumber "]", % S1.getLocationName(),2
MsgBox,0,% A_LineFile "[" A_LineNumber "]", % S1.getUserAgent(),4
S1.delete("S1")