Method

WebKit2WebViewrun_async_javascript_function_in_world

Declaration [src]

void
webkit_web_view_run_async_javascript_function_in_world (
  WebKitWebView* web_view,
  const gchar* body,
  GVariant* arguments,
  const char* world_name,
  GCancellable* cancellable,
  GAsyncReadyCallback callback,
  gpointer user_data
)

Description [src]

Asynchronously run body in the script world with name world_name of the current page context in web_view. If WebKitSettings:enable-javascript is FALSE, this method will do nothing. This API differs from webkit_web_view_run_javascript_in_world() in that the JavaScript function can return a Promise and its result will be properly passed to the callback.

When the operation is finished, callback will be called. You can then call webkit_web_view_run_javascript_in_world_finish() to get the result of the operation.

For instance here is a dummy example that shows how to pass arguments to a JS function that returns a Promise that resolves with the passed argument:

static void
web_view_javascript_finished (GObject      *object,
                              GAsyncResult *result,
                              gpointer      user_data)
{
    WebKitJavascriptResult *js_result;
    JSCValue               *value;
    GError                 *error = NULL;

    js_result = webkit_web_view_run_javascript_finish (WEBKIT_WEB_VIEW (object), result, &error);
    if (!js_result) {
        g_warning ("Error running javascript: %s", error->message);
        g_error_free (error);
        return;
    }

    value = webkit_javascript_result_get_js_value (js_result);
    if (jsc_value_is_number (value)) {
        gint32        int_value = jsc_value_to_string (value);
        JSCException *exception = jsc_context_get_exception (jsc_value_get_context (value));
        if (exception)
            g_warning ("Error running javascript: %s", jsc_exception_get_message (exception));
        else
            g_print ("Script result: %d\n", int_value);
        g_free (str_value);
    } else {
        g_warning ("Error running javascript: unexpected return value");
    }
    webkit_javascript_result_unref (js_result);
}

static void
web_view_evaluate_promise (WebKitWebView *web_view)
{
    GVariantDict dict;
    g_variant_dict_init (&dict, NULL);
    g_variant_dict_insert (&dict, "count", "u", 42);
    GVariant *args = g_variant_dict_end (&dict);
    const gchar *body = "return new Promise((resolve) => { resolve(count); });";
    webkit_web_view_run_async_javascript_function_in_world (web_view, body, arguments, NULL, NULL, web_view_javascript_finished, NULL);
}
Available since:2.38
Deprecated since:2.40

Use webkit_web_view_call_async_javascript_function() instead.

Parameters

body const gchar*
 

The JavaScript function body.

 The data is owned by the caller of the function.
 The value is a NUL terminated UTF-8 string.
arguments GVariant
 

A GVariant with format {&sv} storing the function arguments. Function argument values must be one of the following types, or contain only the following GVariant types: number, string, array, and dictionary. world_name (nullable): the name of a WebKitScriptWorld, if no name (i.e. NULL) is provided, the default world is used. Any value that is not NULL is a distinct world.

 The data is owned by the caller of the function.
world_name const char*
  No description available.
 The data is owned by the caller of the function.
 The value is a NUL terminated UTF-8 string.
cancellable GCancellable
 

A GCancellable or NULL to ignore.

 The argument can be NULL.
 The data is owned by the caller of the function.
callback GAsyncReadyCallback
 

A GAsyncReadyCallback to call when the script finished.

 The argument can be NULL.
user_data gpointer
 

The data to pass to callback function.

 The argument can be NULL.
 The data is owned by the caller of the function.