Page 1 of 1

(COMMate) simulate keypress

Posted: Tue Aug 25, 2015 11:21 pm
by flood
Is it possible to simulate a keypress in an input DOM element in the webgadget? If so, can someone do me a favor and share some example code?

Thank you.

Re: (COMMate) simulate keypress

Posted: Wed Aug 26, 2015 9:37 am
by ostapas
There is more or less complete set of procedures for automating IE:

Code: Select all

Procedure IE_LoadWait(oIE, timeout=-1)
  
  If oIE = 0
    ProcedureReturn #False
  EndIf
  
  If timeout = -1
    timeout = 600
  EndIf
  
  stop_time = Date() + timeout
  
  Repeat
    
    dhGetValue("%s", @Result, oIE, ".document.readyState")
    If Result<>0
      res.s = PeekS(Result)
    EndIf
    
    If res <> "complete" Or res <> "4"
      If Date() >= stop_time
        ProcedureReturn #False
      EndIf
    EndIf
    
    Delay(100)
    
  Until res = "complete" Or res = "4"
  
  ProcedureReturn #True
  
EndProcedure
Procedure IE_Navigate(oIE, url.s, timeout=-1)
  
  If oIE = 0
    ProcedureReturn #False 
  EndIf
  
  If dhCallMethod(oIE, ".Navigate(%T)", @url.s) = 0
    ProcedureReturn #True
  Else
    ProcedureReturn #False 
  EndIf
  If timeout <> -1
    IE_LoadWait(oIE, timeout)
  EndIf
  
EndProcedure
Procedure IE_Create(url.s, visible=#True)
  
  oIE = dhCreateObject("InternetExplorer.Application")
  If oIE=0
    ProcedureReturn #False 
  EndIf
  
  If url.s <> ""
    IE_Navigate(oIE, url.s)
  EndIf
  
  If visible = #True
    dhPutValue(oIE, ".Visible = %b", #True) 
  ElseIf visible = #False
    dhPutValue(oIE, ".Visible = %b", #False) 
  EndIf
  
  dhPutValue  (oIE, ".Silent = %b", #True)
  dhPutValue(oIE, ".AddressBar = %b", #False)
  dhPutValue(oIE, ".MenuBar = %b", #False)
  dhPutValue(oIE, ".ToolBar = %b", #False)
  dhPutValue(oIE, ".StatusBar = %b", #False)
  
  ProcedureReturn oIE
  
EndProcedure
Procedure IE_quit(oIE)
  
  If oIE=0
    ProcedureReturn #False
  EndIf
  
  dhGetValue("%o", @result, oIE, ".quit")
  oIE = 0
  
  ProcedureReturn #True
  
EndProcedure
Procedure.s IE_ReadAllHTML(oIE)
  
  If oIE=0
    ProcedureReturn ""
  EndIf
  
  dhGetValue("%s", @result, oIE, ".Document.documentElement.innerHTML")
  
  If result<>0
    html.s = PeekS(result)
  Else
    ProcedureReturn ""
  EndIf
  
  ProcedureReturn html.s
EndProcedure
Procedure.s IE_ReadBodyHTML(oIE)
  
  If oIE=0
    ProcedureReturn ""
  EndIf
  
  dhGetValue("%s", @result, oIE, ".document.body.innerHTML")
  
  If result <> 0
    html.s = PeekS(result)
  Else
    ProcedureReturn ""
  EndIf
  
  ProcedureReturn html
  
EndProcedure
Procedure.s IE_ReadAllTEXT(oIE)
  
  If oIE = 0
    ProcedureReturn ""
  EndIf
  
  dhGetValue("%s", @result, oIE, ".document.documentElement.innerText")
  
  If result <> 0
    text.s = PeekS(result)
  Else
    ProcedureReturn ""
  EndIf
  
  ProcedureReturn text
  
EndProcedure
Procedure.s IE_ReadBodyTEXT(oIE)
  
  If oIE=0
    ProcedureReturn ""
  EndIf
  
  dhGetValue("%s", @result, oIE, ".document.body.innerText")
  
  If result <> 0
    text.s = PeekS(result)
  Else
    ProcedureReturn ""
  EndIf
  
  ProcedureReturn text
  
EndProcedure
Procedure IE_NumObj(oObj)
  
  If oObj = 0
    ProcedureReturn #False
  EndIf
  
  dhGetValue("%d", @NumElements, oObj, ".length")
  
  ProcedureReturn NumElements
  
EndProcedure
Procedure IE_GetAllObj(oIE)
  
  If oIE = 0
    ProcedureReturn #False
  EndIf
  
  dhGetValue("%o", @oObj, oIE, ".document.all")
  If oObj = 0
    ProcedureReturn #False
  EndIf
  
  ProcedureReturn oObj
  
EndProcedure
Procedure IE_GetObjByTag(oIE, tag.s)
  
  If oIE = 0
    ProcedureReturn #False
  EndIf
  
  dhGetValue("%o", @oObj, oIE, ".Document.getElementsByTagName('%s')", @tag.s)
  If oObj = 0
    ProcedureReturn #False
  EndIf
  
  ProcedureReturn oObj
  
EndProcedure
Procedure IE_GetObjLinks(oIE)
  
  If oIE=0
    ProcedureReturn #False
  EndIf
  
  dhGetValue("%o", @oObj, oIE, ".Document.links")
  If oObj = 0
    ProcedureReturn #False
  EndIf
  
  ProcedureReturn oObj
  
EndProcedure
Procedure IE_GetObjImg(oIE)
  
  If oIE = 0
    ProcedureReturn #False
  EndIf
  
  dhGetValue("%o", @oObj, oIE, ".Document.images")
  If oObj = 0
    ProcedureReturn #False
  EndIf
  
  ProcedureReturn oObj
  
EndProcedure
Procedure IE_GetObjForm(oIE)
  
  If oIE=0
    ProcedureReturn #False
  EndIf
  
  dhGetValue("%o", @oObj, oIE, ".Document.forms")
  If oObj = 0
    ProcedureReturn #False
  EndIf
  
  ProcedureReturn oObj
  
EndProcedure
Procedure IE_GetObjFrames(oIE)
  
  If oIE=0
    ProcedureReturn #False
  EndIf
  
  dhGetValue("%o", @oObj, oIE, ".document.parentwindow.frames")
  If oObj = 0
    ProcedureReturn #False
  EndIf
  
  ProcedureReturn oObj
  
EndProcedure
Procedure IE_GetObjById(oObj, id.s)
  
  If oObj = 0 Or id.s = ""
    ProcedureReturn #False
  EndIf
  
  num = IE_NumObj(oObj)
  If num = 0
    ProcedureReturn #False
  EndIf
  
  For i = 0 To num - 1
    dhGetValue("%o", @oRes, oObj, ".item(%d)", i)
    If oRes <> 0
      dhGetValue("%s", @text, oRes, ".id")
      If text <> 0
        If PeekS(text) = id.s
          ProcedureReturn oRes
        EndIf
      EndIf
    EndIf
  Next i
  
  ProcedureReturn #False
  
EndProcedure
Procedure IE_GetObjByName(oObj, name.s)
  
  If oObj = 0 Or name.s = ""
    ProcedureReturn #False
  EndIf
  
  num = IE_NumObj(oObj)
  If num = 0
    ProcedureReturn #False
  EndIf
  
  For i = 0 To num - 1
    dhGetValue("%o", @oRes, oObj, ".item(%d)", i)
    If oRes <> 0
      dhGetValue("%s", @text, oRes, ".name")
      If text <> 0
        If PeekS(text) = name.s
          ProcedureReturn oRes
        EndIf
      EndIf
    EndIf
  Next i
  
  ProcedureReturn #False
  
EndProcedure
Procedure IE_GetObjByIndex(oObj, index)
  
  If oObj = 0 Or index < 0
    ProcedureReturn #False
  EndIf
  
  dhGetValue("%o", @oRes, oObj, ".item(%d)", index)
  If oRes = 0
    ProcedureReturn #False
  EndIf
  
  ProcedureReturn oRes
  
EndProcedure
Procedure.s IE_ReadObjParam(oObj, param.s)
  
  If oObj = 0 Or param = ""
    ProcedureReturn ""
  EndIf
  
  dhGetValue("%s", @text, oObj, "." + param.s)
  If text = 0
    ProcedureReturn ""
  EndIf
  
  ProcedureReturn PeekS(text)
  
EndProcedure
Procedure IE_WriteObjParam(oObj, param.s, value.s)
  
  If oObj = 0 Or param.s = ""
    ProcedureReturn #False
  EndIf
  
  res_operation = dhPutValue(oObj, "."+param.s+" = %T", @value.s)
  If res_operation <> 0
    ProcedureReturn #False
  EndIf
  
  ProcedureReturn #True
  
EndProcedure
Procedure IE_Action(oObj, action.s)
  
  If oObj = 0 Or action.s = ""
    ProcedureReturn #False
  EndIf
  action.s = LCase(action.s)
  
  Select action.s
    Case "focus"
      dhCallMethod(oObj, ".Focus()")
    Case "click"
      dhCallMethod(oObj, ".Click()")
    Case "back"
      dhCallMethod(oObj, ".GoBack()")
    Case "forvard"
      dhCallMethod(oObj, ".GoForvard()")
    Case "blur"
      dhCallMethod(oObj, ".Blur()")
    Case "home"
      dhCallMethod(oObj, ".GoHome()")
    Case "invisible"
      dhPutValue(oObj, ".Visible = %b", #False)
    Case "visible"
      dhPutValue(oObj, ".Visible = %b", #True)
    Case "search"
      dhCallMethod(oObj, ".GoSearch()")
    Case "fullscreen"
      dhPutValue(oObj, ".FullScreen = %b", #True)
    Case "stop"
      dhCallMethod(oObj, ".Stop")
    Case "quit"
      dhGetValue("%o", @result, oObj, ".quit")
    Case "selectall"
      dhGetValue("%T", @res, oObj, ".document.execCommand('SelectAll')")
    Case "unselect"
      dhGetValue("%T", @res, oObj, ".document.execCommand('UnSelect')")
    Case "cut"
      dhGetValue("%T", @res, oObj, ".document.execCommand('Cut')")
    Case "copy"
      dhGetValue("%T", @res, oObj, ".document.execCommand('Copy')")
    Case "paste"
      dhGetValue("%T", @res, oObj, ".document.execCommand('Paste')")
    Case "delete"
      dhGetValue("%T", @res, oObj, ".document.execCommand('Delete')")
    Case "saveas"
      dhGetValue("%T", @res, oObj, ".document.execCommand('SaveAs')")
    Case "refresh"
      dhGetValue("%T", @res, oObj, ".document.execCommand('Refresh')")
    Case "print"
      dhGetValue("%T", @res, oObj, ".document.parentwindow.Print()")
  EndSelect
  
  ProcedureReturn #True
  
EndProcedure
Procedure IE_FormSubmit(oForm)
  
  If oForm = 0
    ProcedureReturn #False
  EndIf
  
  result_operation = dhCallMethod(oForm, ".submit()")
  If result_operation<>0
    ProcedureReturn #False
  EndIf
  
  ProcedureReturn #True
  
EndProcedure

Just not too sure where did I find it or who is the author. It also needs PureDispHelper lib by ts-soft http://forums.purebasic.com/english/vie ... hp?t=26744
Usage examples:

Code: Select all

Procedure Create_and_navigate()
  
  oIE = IE_create("http://www.purebasic.fr/", #True)
  If oIE=0
    ProcedureReturn 0
  EndIf
  IE_LoadWait(oIE, 60)
  IE_Navigate(oIE, "www.google.com", 60)
  
  IE_quit(oIE)
  
EndProcedure

Procedure Get_html_and_text()
  
  oIE = IE_create("http://www.purebasic.fr/", #True)
  If oIE=0
    ProcedureReturn 0
  EndIf
  IE_LoadWait(oIE, 60)
  
  Debug IE_ReadAllHTML(oIE)
  Debug IE_ReadBodyHTML(oIE)
  Debug IE_ReadAllTEXT(oIE)
  Debug IE_ReadBodyTEXT(oIE)
  
  IE_quit(oIE)
  
EndProcedure

Procedure Get_Tag_objects()
  
  oIE = IE_create("http://www.purebasic.fr/", #True)
  If oIE=0
    ProcedureReturn 0
  EndIf
  IE_LoadWait(oIE, 60)
  
  oObj = IE_GetAllObj(oIE)
  oObj = IE_GetObjByTag(oIE, "html")
  Debug IE_NumObj(oObj)
  oObj = IE_GetObjByTag(oIE, "meta")
  Debug IE_NumObj(oObj)
  oObj = IE_GetObjByTag(oIE, "title")
  Debug IE_NumObj(oObj)
  oObj = IE_GetObjByTag(oIE, "head")
  Debug IE_NumObj(oObj)
  oObj = IE_GetObjByTag(oIE, "body")
  Debug IE_NumObj(oObj)
  oObj = IE_GetObjByTag(oIE, "a")
  Debug IE_NumObj(oObj)
  oObj = IE_GetObjByTag(oIE, "img")
  Debug IE_NumObj(oObj)
  oObj = IE_GetObjByTag(oIE, "table")
  Debug IE_NumObj(oObj)
  oObj = IE_GetObjByTag(oIE, "div")
  Debug IE_NumObj(oObj)
  oObj = IE_GetObjByTag(oIE, "form")
  Debug IE_NumObj(oObj)
  
  IE_quit(oIE)
  
EndProcedure

Procedure Get_Objects_Collection()
  
  oIE = IE_create("http://www.purebasic.fr/", #True)
  If oIE=0
    ProcedureReturn 0
  EndIf
  IE_LoadWait(oIE, 60)
  
  oObj = IE_GetAllObj(oIE)
  Debug IE_NumObj(oObj)
  
  oObj = IE_GetObjLinks(oIE)
  Debug IE_NumObj(oObj)
  
  oObj = IE_GetObjImg(oIE)
  Debug IE_NumObj(oObj)
  
  oObj = IE_GetObjForm(oIE)
  Debug IE_NumObj(oObj)
  
  IE_quit(oIE)
EndProcedure

Procedure Get_obj_by_id()
  
  url.s = "http://www.google.com/"
  oIE = IE_create(url.s, #True)
  If oIE=0
    ProcedureReturn 0
  EndIf
  IE_LoadWait(oIE, 60)
  
  oDoc = IE_GetAllObj(oIE)
  Debug IE_GetObjById(oDoc, "body")
  
  oForms = IE_GetObjForm(oIE)
  Debug IE_GetObjByName(oForms, "gbqf")
  
  oLinks = IE_GetObjLinks(oIE)
  num = IE_NumObj(oLinks)
  For i=0 To num-1
    Debug IE_GetObjByIndex(oLinks, i)
  Next i
  
  IE_quit(oIE)
  
EndProcedure

Procedure Read_object_params()
  
  url.s = "http://www.purebasic.fr"
  oIE = IE_create(url.s, #True)
  If oIE=0
    ProcedureReturn 0
  EndIf
  IE_LoadWait(oIE, 60)
  
  oLinks = IE_GetObjLinks(oIE)
  num = IE_NumObj(oLinks)
  For i=0 To num-1
    oObj = IE_GetObjByIndex(oLinks, i)
    Debug IE_ReadObjParam(oObj, "href")
    Debug IE_ReadObjParam(oObj, "target")
    Debug IE_ReadObjParam(oObj, "title")
    Debug IE_ReadObjParam(oObj, "rel")
    Debug IE_ReadObjParam(oObj, "text")
    Debug IE_ReadObjParam(oObj, "id")
    Debug "--------------------"
  Next i
  
  oImages = IE_GetObjByTag(oIE, "img")
  num = IE_NumObj(oImages)
  For i=0 To num-1
    oObj = IE_GetObjByIndex(oImages, i)
    Debug IE_ReadObjParam(oObj, "src")
    Debug IE_ReadObjParam(oObj, "alt")
    Debug IE_ReadObjParam(oObj, "width")
    Debug IE_ReadObjParam(oObj, "height")
    Debug IE_ReadObjParam(oObj, "title")
    Debug IE_ReadObjParam(oObj, "id")
    Debug "--------------------"
  Next i
  
  IE_quit(oIE)
  
EndProcedure

Procedure Set_Object_params()
  
  url.s = "http://www.purebasic.fr/"
  rpls_url.s = "http://www.google.com/"
  oIE = IE_create(url.s, #True)
  If oIE=0
    ProcedureReturn 0
  EndIf
  IE_LoadWait(oIE, 60)
  Delay(5000)
  
  oLinks = IE_GetObjLinks(oIE)
  num = IE_NumObj(oLinks)
  For i=0 To num-1
    oObj = IE_GetObjByIndex(oLinks, i)
    IE_WriteObjParam(oObj, "href", rpls_url.s)
    IE_WriteObjParam(oObj, "title", Str(i))
    IE_WriteObjParam(oObj, "text", Str(i))
    Delay(200)
  Next i
  
  oImages = IE_GetObjByTag(oIE, "img")
  num = IE_NumObj(oImages)
  For i=0 To num-1
    oObj = IE_GetObjByIndex(oImages, i)
    IE_WriteObjParam(oObj, "src", "http://www.google.kz/images/icons/product/chrome-48.png")
    IE_WriteObjParam(oObj, "width", "48")
    IE_WriteObjParam(oObj, "height", "48")
    IE_WriteObjParam(oObj, "title", Str(i))
  Next i
  
  ;IE_quit(oIE)
  
EndProcedure

Procedure Action()
  
  url.s = "http://www.purebasic.fr/"
  rpls_url.s = "http://www.google.com/"
  oIE = IE_create(url.s, #True)
  If oIE=0
    ProcedureReturn 0
  EndIf
  IE_LoadWait(oIE, 60)
  
  For i=1 To 5
    oLinks = IE_GetObjLinks(oIE)
    num = IE_NumObj(oLinks)
    index = Random(num)
    oObj = IE_GetObjByIndex(oLinks, index)
    IE_Action(oObj, "focus")
    IE_Action(oObj, "click")
    IE_LoadWait(oIE, 60)
    Delay(100)
  Next i
  
  For i=1 To 5
    IE_Action(oIE, "back")
    IE_LoadWait(oIE, 60)
    Delay(100)
  Next i
  
  For i=1 To 5
    IE_Action(oIE, "forvard")
    IE_LoadWait(oIE, 60)
    Delay(100)
  Next i
  
  IE_Action(oIE, "quit")
  
EndProcedure



Re: (COMMate) simulate keypress

Posted: Wed Aug 26, 2015 3:57 pm
by flood
Thanks for the response. My project uses commate plus, so I would like to use that instead of another library, if possible.