addBindOnStartLoading & Javascript question

123 views
Skip to first unread message

Sean Fitzgerald

unread,
Jan 20, 2011, 2:05:43 PM1/20/11
to berk...@googlegroups.com
I want to use the addBindOnStartLoading functions to execute some javascript on load, but it's not very clear to me how to use the function call. The glut_input example uses the call multiple times, but it doesn't seem to do anything. I'd expect some console output from glut_util's onJavascriptCallback, but it never happens. My questions:

1. Are javascript callbacks implemented at all?

2. If so, is there a simple explanation of how to use them? All I really need to do is to get the value of document.body.scrollwidth and document.body.scrollheight when a page loads.

Thanks!

Patrick Horn

unread,
Jan 21, 2011, 1:58:13 AM1/21/11
to berk...@googlegroups.com, platfo...@googlegroups.com
On 20.01.2011 11:05, Sean Fitzgerald wrote:
> I want to use the addBindOnStartLoading functions to execute some
> javascript on load, but it's not very clear to me how to use the
> function call. The glut_input example uses the call multiple times,
> but it doesn't seem to do anything. I'd expect some console output
> from glut_util's onJavascriptCallback, but it never happens. My questions:
>
> 1. Are javascript callbacks implemented at all?
>
> 2. If so, is there a simple explanation of how to use them?
A long answer to #1, since this is not documented anywhere:

What the function does is bind some Script::Variant value to the
javascript window object (or something else, depending on the name)

Many values are constants, like strings, integers, booleans, null, etc.
However, there are two special types of Script::Variant, which you get
by constructing:
Script::Variant::bindFunction(WideString name, bool synchronous);

As an example, glut_input.cpp does this:
bk_texture_window->window()->addBindOnStartLoading(
Berkelium::WideString::point_to(L"glutCallbackSync"),

Berkelium::Script::Variant::bindFunction(Berkelium::WideString::point_to(L"glutCB"),
true));

The first string "glutCallbackSync" is the object that appears in
"window" (it literally appears as the left side of an '=' expression)
The second string "glutCB" is the string that is passed to
WindowDelegate::onJavascriptCallback in glut_util.hpp
The "true" indicates that the function is synchronous, and javascript
execution will halt until it returns a value (if it does not return a
value, the page will hang).

So putting this together, what happens is
window.glutCallbackSync(arg1,arg2...) will run
WindowDelegate::onJavascriptCallback and return a value.

Also, one thing to watch out for: Berkelium does not implement arrays or
objects, so each arg1,... passed must be string, boolean, number, or null.
Anything else will result in an error message passed to
WindowDelegate::onConsoleMessage and the function will return null.

I'm attaching a sample html page that will work with glut_input. If
things are working correctly, you should see onJavascriptCallback()
displayed in your terminal window, intermingled with alert boxes.

> 2. If so, is there a simple explanation of how to use them? All I
> really need to do is to get the value of document.body.scrollwidth and
> document.body.scrollheight when a page loads.
>
> Thanks!

For your question #2:
You should make sure to check scrollWidth and scrollHeight only when the
page finishes loading (WindowDelegate::onLoad). One way to do this is to
execute javascript, calling your callback with those values, as you
suggest. I would suggest passing "false" to make the callback
asynchronous so that the page will be more responsive.

The API is somewhat clunky because it is limited by the IPC calls
defined between parent and child -- and there's no call for "run
javsacript something and return its result", so the callback madness is
a reasonable solution.

-Patrick

glut_input.html

Jarrett Chisholm

unread,
Nov 21, 2012, 12:53:24 PM11/21/12
to berk...@googlegroups.com
Hi guys,

I know this thread is a bit old, but I do have a question about the glutCB function....where is this defined?  I see where it gets bound in the glut_input.cpp demo, but I don't see where it is actually defined. 

When I run the glut_input demo with that html you (Patrick) provided, in the console it looks like the return value is 'Hello World!' when the call to glutCallbackSync is made from the javascript...

cheers

jarrett

Ewen Cheslack-Postava

unread,
Nov 21, 2012, 2:05:23 PM11/21/12
to berk...@googlegroups.com
It isn't defined anywhere, it's just getting passed as a parameter to a callback. The Javascript call to the bound function causes Berkelium to invoke WindowDelegate::onJavascriptCallback with "glutGB" as the funcName parameter. You have to use that to do dispatch from your C++ code to the specific handler. See GLTextureWindow::onJavascriptCallback in glut_util.hpp, which is what's printing the messages you're seeing.

-Ewen
Reply all
Reply to author
Forward
0 new messages