Run js for scrolling from webextension #367.
authorDaniel Carl <danielcarl@gmx.de>
Thu, 20 Apr 2017 22:03:55 +0000 (00:03 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Thu, 20 Apr 2017 22:03:55 +0000 (00:03 +0200)
src/ext-proxy.c
src/ext-proxy.h
src/normal.c
src/webextension/ext-main.c
src/webextension/ext-util.c
src/webextension/ext-util.h

index daf04ce..ea958fe 100644 (file)
@@ -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.
index 8258df9..5d4a073 100644 (file)
@@ -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);
 
index be5e987..b7d9234 100644 (file)
@@ -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;
index 3abf4bf..661ba28 100644 (file)
@@ -59,6 +59,9 @@ static const GDBusInterfaceVTable interface_vtable = {
 static const char introspection_xml[] =
     "<node>"
     " <interface name='" VB_WEBEXTENSION_INTERFACE "'>"
+    "  <method name='EvalJsNoResult'>"
+    "   <arg type='s' name='js' direction='in'/>"
+    "  </method>"
     "  <method name='FocusInput'>"
     "  </method>"
     "  <signal name='PageCreated'>"
@@ -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")) {
index f63cb11..facb4a7 100644 (file)
 #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.
  *
index a4286a5..877933a 100644 (file)
@@ -21,7 +21,9 @@
 #define _EXT_UTIL_H
 
 #include <glib.h>
+#include <JavaScriptCore/JavaScript.h>
 
 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 */