Page 1 of 1

Execute JavaScript in a WebGadget using COMate

Posted: Sat Feb 14, 2009 11:35 am
by srod
Hi,

A while ago Freak posted some code which allows one to execute JavaScript commands against a WebGadget etc. Ricardo juggled it around a bit and I have just translated a small part of it to COMate.

I reckon it could be useful for those who work with this kind of stuff.

Code: Select all

IncludePath "..\"
XIncludeFile "COMate.pbi"

Define.COMateObject WebObject

Procedure.i ExecuteJavaScript(Gadget, command$) 
  Protected browser.COMateObject, documentDispatch.COMateObject, script.COMateObject
  Protected result
  browser = COMate_WrapCOMObject(GetWindowLong_(GadgetID(gadget), #GWL_USERDATA)) 
  If browser 
    documentDispatch = browser\GetObjectProperty("Document")
    If documentDispatch
      script = documentDispatch\GetObjectProperty("script")
      If script
        result = script\Invoke("eval('" + command$ + "')")
        script\release()
      EndIf  
      documentDispatch\Release()
    EndIf
    browser\Release()
  EndIf 
  ProcedureReturn result
EndProcedure 

If OpenWindow(0, 10000, 0, 0, 0, "WebGadget",  #PB_Window_SystemMenu|#PB_Window_Maximize)
  WebGadget(0, 0, 0, WindowWidth(0)+50,WindowHeight(0)-100, "http://www.purebasic.fr/english/posting.php?mode=reply&t=33983") 
  ButtonGadget(1,50,WindowHeight(0)-50,150,25,"Fill & Send") 
  Repeat 
    Event = WaitWindowEvent(); 
    Select Event 
      Case #PB_Event_Gadget 
        Select EventGadget() 
          Case 1 
            xMessage$ = "Hello from PB :)" 
            ExecuteJavaScript(0,"document.all.message.value=" + Chr(34) + xMessage$ + Chr(34)) 
            ExecuteJavaScript(0,"document.all.preview.click()") 
            ;DO NOT run the following line as it will automatically post in the COMate thread in the PB forums!
;            ExecuteJavaScript(0,"document.post.post.click()") 
        EndSelect 
      Case #PB_Event_CloseWindow 
        Break 
    EndSelect 
  Until GetAsyncKeyState_(#VK_ESCAPE) 
EndIf 

Posted: Sat Feb 14, 2009 1:36 pm
by ricardo
Really REALLY nice, Congratullations!!!

Posted: Sun Feb 15, 2009 6:35 pm
by Kwai chang caine
Congratulation again a time For the splendid COMATE.

Do you know why i have an error " 'o' have the null value " ???
It's just before the page appears

Code: Select all

IncludePath "..\" 
XIncludeFile "COMate.pbi" 

Define.COMateObject WebObject 

Procedure.i ExecuteJavaScript(Gadget, command$) 
  Protected browser.COMateObject, documentDispatch.COMateObject, script.COMateObject 
  Protected result 
  browser = COMate_WrapCOMObject(GetWindowLong_(GadgetID(gadget), #GWL_USERDATA)) 
  If browser 
    documentDispatch = browser\GetObjectProperty("Document") 
    If documentDispatch 
      script = documentDispatch\GetObjectProperty("script") 
      If script 
        result = script\Invoke("eval('" + command$ + "')") 
        script\release() 
      EndIf  
      documentDispatch\Release() 
    EndIf 
    browser\Release() 
  EndIf 
  ProcedureReturn result 
EndProcedure 


If OpenWindow(0, 10000, 0, 0, 0, "WebGadget",  #PB_Window_SystemMenu|#PB_Window_Maximize) 
  WebGadget(0, 0, 0, WindowWidth(0)+50,WindowHeight(0)-100, "http://www.google.fr/") 
  
  ButtonGadget(1,50,WindowHeight(0)-50,150,25,"Fill & Send") 
  Repeat 
    Event = WaitWindowEvent(); 
    Select Event 
      Case #PB_Event_Gadget 
        Select EventGadget() 
          Case 1 
            xMessage$ = "Hello from PB :)" 
            ExecuteJavaScript(0,"document.all.q.value=" + Chr(34) + xMessage$ + Chr(34)) 
            Delay(10)
            ExecuteJavaScript(0,"document.all.btnG.click()")
            ;ExecuteJavaScript(0,"document.post.btnG.click()") 
        EndSelect 
      Case #PB_Event_CloseWindow 
        Break 
    EndSelect 
  Until GetAsyncKeyState_(#VK_ESCAPE) 
EndIf 

Posted: Sun Feb 15, 2009 7:43 pm
by ricardo
The script part in the button cant be runned if the page isnt fully loaded or will prompt an error.

Posted: Sun Feb 15, 2009 7:46 pm
by Kwai chang caine
Thanks RICARDO 8)
I try to include a loop to wait

Posted: Sun Feb 15, 2009 7:52 pm
by Kwai chang caine
The script part in the button cant be runned if the page isnt fully loaded or will prompt an error.
Are you sure ??
Because i have put a second button for separate the two function and it's the same problem :cry:

Code: Select all

IncludePath "..\" 
XIncludeFile "COMate.pbi" 

Define.COMateObject WebObject 

Procedure.i ExecuteJavaScript(Gadget, command$) 
  Protected browser.COMateObject, documentDispatch.COMateObject, script.COMateObject 
  Protected result 
  browser = COMate_WrapCOMObject(GetWindowLong_(GadgetID(gadget), #GWL_USERDATA)) 
  If browser 
    documentDispatch = browser\GetObjectProperty("Document") 
    If documentDispatch 
      script = documentDispatch\GetObjectProperty("script") 
      If script 
        result = script\Invoke("eval('" + command$ + "')") 
        script\release() 
      EndIf  
      documentDispatch\Release() 
    EndIf 
    browser\Release() 
  EndIf 
  ProcedureReturn result 
EndProcedure 


If OpenWindow(0, 10000, 0, 0, 0, "WebGadget",  #PB_Window_SystemMenu|#PB_Window_Maximize) 
  WebGadget(0, 0, 0, WindowWidth(0)+50,WindowHeight(0)-100, "http://www.google.fr/") 
  
  ButtonGadget(1,50,WindowHeight(0)-50,150,25,"Fill") 
  ButtonGadget(2,250,WindowHeight(0)-50,150,25,"Send") 
   
  Repeat 
    Event = WaitWindowEvent(); 
    Select Event 
      Case #PB_Event_Gadget 
        Select EventGadget() 
          Case 1 
            xMessage$ = "Hello from PB :)" 
            ExecuteJavaScript(0,"document.all.q.value=" + Chr(34) + xMessage$ + Chr(34)) 
           Case 2
            ExecuteJavaScript(0,"document.all.btnG.click()")
            
        EndSelect 
      Case #PB_Event_CloseWindow 
        Break 
    EndSelect 
  Until GetAsyncKeyState_(#VK_ESCAPE) 
EndIf 

Posted: Sun Feb 15, 2009 9:33 pm
by ricardo
I dont explain myself well. :wink:

I mean you cant executejavascript in a webpage if it was not fully loaded. Not just the click button part but any javascript.
You need to make sure the webpage is 100% loaded after running the execute javascript part.

Re: Execute JavaScript in a WebGadget using COMate

Posted: Tue Apr 14, 2015 9:20 am
by IdeasVacuum
Hello chaps

How can I get this to work with a javascript function that returns a string (utf8)?

e.g. function Myfunction(sArg1, sArg2) {
//do incredible things
return sIncredibleString
}

Well in fact there is a very simple way to accomplish this that really suits my requirement - cross-platform too!

http://www.purebasic.fr/english/viewtopic.php?p=273634

Re: Execute JavaScript in a WebGadget using COMate

Posted: Fri Apr 20, 2018 4:32 pm
by CELTIC88
you saved me, thank you a thousand times ! :)