Execute JavaScript in a WebGadget using COMate

Share your advanced PureBasic knowledge/code with the community.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Execute JavaScript in a WebGadget using COMate

Post 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 
I may look like a mule, but I'm not a complete ass.
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

Really REALLY nice, Congratullations!!!
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post 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 
ImageThe happiness is a road...
Not a destination
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

The script part in the button cant be runned if the page isnt fully loaded or will prompt an error.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Thanks RICARDO 8)
I try to include a loop to wait
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post 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 
ImageThe happiness is a road...
Not a destination
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post 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.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Execute JavaScript in a WebGadget using COMate

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 154
Joined: Thu Sep 17, 2015 3:39 pm

Re: Execute JavaScript in a WebGadget using COMate

Post by CELTIC88 »

you saved me, thank you a thousand times ! :)
interested in Cybersecurity..
Post Reply