To call JavaScript Functions in C ++ code, you must first obtain reference to the JavaScript Functions to be called.
Step 1: Obtain the JavaScript context that defines the function. I have not tried winlaunche. In gtklauncher, I use the following method:
Frame = webkit_web_view_get_main_frame (web_view );
Context = webkit_web_frame_get_global_context (FRAME );
Winlauncher should be similar.
Step 2: Go to the Global Object of the context. Assume that the JavaScript function you defined is global.
Jsobjectref globalobject = jscontextgetglobalobject (context );
After obtaining the global object, a JS character reference object jsstringref containing the global function name to be called is required. Assume that the JavaScript function my_function is declared as follows:
Function my_function (A, B)
{
// Do something
Alert (A + B );
}
Step 3: Get reference of JavaScript function "my_function"
First create a string my_function:
Jsstringref myfunctionname = jsstringcreatewithuf8cstring ("my_function ");
Then, use the function name "my_function" to obtain the reference functionobject of the function from the Global Object attribute.
Jsobjectref functionobject = (jsobjectref) jsobjectgetproperty (context, globalobject, myfunctionname, null );
Step 4: Call JavaScript Functions
In the following example, assume that the global JavaScript function my_function contains two numeric parameters:
Jsvalueref arguments [2];
Jsvalueref result;
Int num_arguments = 2;
Arguments [0] = jsvaluemakenumber (context, 1.0 );
Arguments [1] = jsvaluemakenumber (context, 3.14 );
Result = jsobjectcallasfunction (context, functionobject, globalobject, num_arguments, arguments, null );
Compile gtklauncher with the following command: GCC 'pkg-config -- cflags -- libs webkit-1.0 GTK +-1000' main. C-S-O gtklauncher
Start gtklancher and call the JavaScript function my_function. In the alert prompt box, 4.14 is displayed.