From: Daniel Carl Date: Thu, 20 Apr 2017 22:03:55 +0000 (+0200) Subject: Run js for scrolling from webextension #367. X-Git-Url: https://git.owens.tech/about.html/about.html/git?a=commitdiff_plain;h=5f65084c7082b71f4509e1b17841f8fbff80d918;p=vimb.git Run js for scrolling from webextension #367. --- diff --git a/src/ext-proxy.c b/src/ext-proxy.c index daf04ce..ea958fe 100644 --- a/src/ext-proxy.c +++ b/src/ext-proxy.c @@ -153,6 +153,11 @@ static void on_proxy_created(GDBusProxy *new_proxy, GAsyncResult *result, NULL); } +void ext_proxy_eval_script(Client *c, char *js) +{ + dbus_call(c, "EvalJsNoResult", g_variant_new("(s)", js), NULL); +} + /** * Request the web extension to focus first editable element. * Returns whether an focusable element was found or not. diff --git a/src/ext-proxy.h b/src/ext-proxy.h index 8258df9..5d4a073 100644 --- a/src/ext-proxy.h +++ b/src/ext-proxy.h @@ -23,6 +23,7 @@ #include "main.h" const char *ext_proxy_init(void); +void ext_proxy_eval_script(Client *c, char *js); void ext_proxy_focus_input(Client *c); void ext_proxy_set_header(Client *c, const char *headers); diff --git a/src/normal.c b/src/normal.c index be5e987..b7d9234 100644 --- a/src/normal.c +++ b/src/normal.c @@ -688,19 +688,19 @@ static VbResult normal_scroll(Client *c, const NormalCmdInfo *info) js = g_strdup_printf( "window.scroll(window.scrollX, %d * (1 + (document.height - window.innerHeight) / 100));", info->count); - webkit_web_view_run_javascript(c->webview, js, NULL, NULL, NULL); + ext_proxy_eval_script(c, js); g_free(js); return RESULT_COMPLETE; } /* Without count scroll to the end of the page. */ - webkit_web_view_run_javascript(c->webview, "window.scroll(window.scrollX, document.body.scrollHeight);", NULL, NULL, NULL); + ext_proxy_eval_script(c, "window.scroll(window.scrollX, document.body.scrollHeight);"); return RESULT_COMPLETE; case '0': - webkit_web_view_run_javascript(c->webview, "window.scroll(0, window.scrollY);", NULL, NULL, NULL); + ext_proxy_eval_script(c, "window.scroll(0, window.scrollY);"); return RESULT_COMPLETE; case '$': - webkit_web_view_run_javascript(c->webview, "window.scroll(document.body.scrollWidth, window.scrollY);", NULL, NULL, NULL); + ext_proxy_eval_script(c, "window.scroll(document.body.scrollWidth, window.scrollY);"); return RESULT_COMPLETE; default: if (info->key2 == 'g') { @@ -708,18 +708,18 @@ static VbResult normal_scroll(Client *c, const NormalCmdInfo *info) js = g_strdup_printf( "window.scroll(window.scrollX, %d * (1 + (document.height - window.innerHeight) / 100));", info->count); - webkit_web_view_run_javascript(c->webview, js, NULL, NULL, NULL); + ext_proxy_eval_script(c, js); g_free(js); return RESULT_COMPLETE; } /* Without count gg scrolls to the top of the page. */ - webkit_web_view_run_javascript(c->webview, "window.scroll(window.scrollX, 0);", NULL, NULL, NULL); + ext_proxy_eval_script(c, "window.scroll(window.scrollX, 0);"); return RESULT_COMPLETE; } return RESULT_ERROR; } js = g_strdup_printf("window.scrollBy(%d,%d);", x, y); - webkit_web_view_run_javascript(c->webview, js, NULL, NULL, NULL); + ext_proxy_eval_script(c, js); g_free(js); return RESULT_COMPLETE; diff --git a/src/webextension/ext-main.c b/src/webextension/ext-main.c index 3abf4bf..661ba28 100644 --- a/src/webextension/ext-main.c +++ b/src/webextension/ext-main.c @@ -59,6 +59,9 @@ static const GDBusInterfaceVTable interface_vtable = { static const char introspection_xml[] = "" " " + " " + " " + " " " " " " " " @@ -290,7 +293,17 @@ static void dbus_handle_method_call(GDBusConnection *conn, const char *sender, { char *value; - if (!g_strcmp0(method, "FocusInput")) { + if (!g_strcmp0(method, "EvalJsNoResult")) { + g_variant_get(parameters, "(s)", &value); + JSGlobalContextRef jsContext; + + jsContext = webkit_frame_get_javascript_context_for_script_world( + webkit_web_page_get_main_frame(ext.webpage), + webkit_script_world_get_default() + ); + JSValueRef ref = NULL; + ext_util_js_eval(jsContext, value, &ref); + } else if (!g_strcmp0(method, "FocusInput")) { ext_dom_focus_input(webkit_web_page_get_dom_document(ext.webpage)); g_dbus_method_invocation_return_value(invocation, NULL); } else if (!g_strcmp0(method, "SetHeaderSetting")) { diff --git a/src/webextension/ext-util.c b/src/webextension/ext-util.c index f63cb11..facb4a7 100644 --- a/src/webextension/ext-util.c +++ b/src/webextension/ext-util.c @@ -23,6 +23,27 @@ #include "../config.h" #include "ext-util.h" +/** + * Evaluates given string as script and return if this call succeed or not. + */ +gboolean ext_util_js_eval(JSContextRef ctx, const char *script, JSValueRef *result) +{ + JSStringRef js_str; + JSValueRef exc = NULL, res = NULL; + + js_str = JSStringCreateWithUTF8CString(script); + res = JSEvaluateScript(ctx, js_str, JSContextGetGlobalObject(ctx), NULL, 0, &exc); + JSStringRelease(js_str); + + if (exc) { + *result = exc; + return FALSE; + } + + *result = res; + return TRUE; +} + /** * Creates a temporary file with given content. * diff --git a/src/webextension/ext-util.h b/src/webextension/ext-util.h index a4286a5..877933a 100644 --- a/src/webextension/ext-util.h +++ b/src/webextension/ext-util.h @@ -21,7 +21,9 @@ #define _EXT_UTIL_H #include +#include gboolean ext_util_create_tmp_file(const char *content, char **file); +gboolean ext_util_js_eval(JSContextRef ctx, const char *script, JSValueRef *result); #endif /* end of include guard: _EXT_UTIL_H */