run_async_javascript_function_in_world


Description:

[ Version ( deprecated = true , deprecated_since = "2.40" , since = "2.38" ) ]
public async void run_async_javascript_function_in_world (string body, Variant arguments, string world_name, Cancellable? cancellable = null)

Warning: run_async_javascript_function_in_world is deprecated since 2.40.

Asynchronously run body in the script world with name world_name of the current page context in this.

Note:

If WebKitSettings:enable-javascript is FALSE, this method will do nothing. This API differs from 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 run_javascript_in_world.end 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:

```c 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); } ```

Parameters:

this

a WebView

body

the JavaScript function body

arguments

a Variant 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.

cancellable

a Cancellable or null to ignore

callback

a TaskReadyCallback to call when the script finished

user_data

the data to pass to callback function