December 11, 2024, 10:25:07 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Communicating Variables Between EBASIC and BROWSER Javascript

Started by Logman, March 20, 2009, 06:27:58 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Logman

Quick question; don't know if this is the right forum, but I'm using NC/S programming.

I have included a browser window in my code, which brings up HTML/CSS coded web pages. Some of the web pages perform DOM routines using Javascript.

Is there anyway to set up Global variables that can be accessed/altered by both the Javascript in the browser window and the EBasic code? What I'm trying to avoid is having to communicate variable parameters back to the main code using forms-type messaging (?action=post; etc.) or cookies.

Logman
Education is what you get when you read the fine print.<br />Experience is what you get when you don't!

sapero

This example shows how to access variables using the disphelper:WINDOW win
OPENWINDOW win,0,0,200,100,@NOAUTODRAW|@SIZE|@MINBOX|@MAXBOX,0,"BROWSER TEST",&handler
CENTERWINDOW win
ATTACHBROWSER win

' force the browser to b created
BROWSECMD win,@NAVIGATE, "about:blank"

' create example page with scripting variable
FILE f
openfile f, GetStartPath()+"1.htm", "w"
write f, "<html><body>"
write f, "<script>var MyVariable=5;</script>"
write f, "<button id=clickme onclick='return alert(MyVariable);'>show MyVariable</button>"
write f, "</body></html>"
closefile f

' open the page
BROWSECMD win,@NAVIGATE, GetStartPath()+"1.htm"
IDispatch dsp = GetBrowserDispatch(win)

' wait until not loaded
int busy=1
while busy
wait
GetComProperty(dsp,"%b",&busy,"Busy")
wend

' query the current MyVariable value, then change it.
int value
IDispatch document
GetComProperty(dsp,"%o",&document,"Document.script")
GetComProperty(document,"%d",&value,"MyVariable")

MessageBox win, "MyVariable is "+str$(value),""
' change MyVariable value
SetComProperty(document,"MyVariable = %d", 12345)
' click the button
CallObjectMethod(document, "clickme.click()")

document->Release()
dsp->Release()
waituntil win.hwnd=0


sub handler
if (@message = @idclosewindow) then closewindow win
endsub



extern _IID_IWebBrowser2 as GUID
sub GetBrowserDispatch(WINDOW w),comref
declare import, GetPropA(int hwnd,string name),pointer
pointer p = GetPropA(w.hwnd, "BROWSER")
IDispatch dsp = 0
if (p)
dsp = *<comref>p
dsp->QueryInterface(_IID_IWebBrowser2, &dsp)
endif
return dsp
endsub

Logman

Education is what you get when you read the fine print.<br />Experience is what you get when you don't!

Logman

Sapero:

I got your code snippets up and running just fine. I really appreciated your help.

Now I'd like to understand the underlying COM/OLE Automation implementation by studying the COM usage and Javascript objects in my web pages.

Could you tell me which 'include' files contain _IID_IWebBrowser2, GetPropA, QueryInterface, etc.?

I thought they were in the 'shell.inc' file, but I can't locate them so I can study them.

Thanks ???

Logman
Education is what you get when you read the fine print.<br />Experience is what you get when you don't!

sapero

The exdisp.inc header defines the root webbrowser interfaces and guids. It includes also windows stuff and base com and ole interfaces.
Optional mshtml.inc allows you to iterate though document, window, forms, buttons, options... elements, directly execute methods and set properties, but it requires much more code.
For example the call to GetComProperty you may replace with
' query the current MyVariable value, then change it.
int value

$include "exdisp.inc"
$include "mshtml.inc"
IDispatch document

$ifdef never

GetComProperty(dsp,"%o",&document,"Document.script") ' using disphelper

$else

IDispatch temp
IWebBrowser2 browser = dsp
if ((browser->get_Document(&temp) = 0) and temp)
IHTMLDocument doc1
if (temp->QueryInterface(_IID_IHTMLDocument, &doc1) = 0) ' using raw methods
doc1->get_script(&document)
doc1->Release()
endif
temp->Release()
endif

$endif

Logman

Very cool! ;D

Being fairly new to EBasic, I take it that you did all the work in porting/translating 'exdisp.inc' and 'mshtml.inc'. If so, thanks.

I should have know there were additional include files I didn't have on my system at home when I first ran the program. At work, I installed the additional set of includes I got from you and the program ran flawlessly.

Question: what's the difference, for instance, between using _IID_IWebBrowser2 and just plain _IID_IWebBrowser in the 'exdisp.inc' file? I would not have caught that without your help, but don't understand the difference.

Thanks again.

Logman
Education is what you get when you read the fine print.<br />Experience is what you get when you don't!

sapero

Open the exdisp header and find "interface IWebBrowser", remember the last method name (get_Busy).
Now find "interface IWebBrowser2", then inside it find the get_Busy method:
stdmethod get_Busy(pointer pBool),HRESULT
'IWebBrowserApp methods
stdmethod Quit(),HRESULT
... 19 more methods
'IWebBrowser2 methods
stdmethod Navigate2
... 18 more methods
endinterface

If you don't need the additional methods, you may query for IWebBrowser or IWebBrowserApp interface only, but IWebBrowser2 is the standard for today.

Logman

Education is what you get when you read the fine print.<br />Experience is what you get when you don't!