Convert focus_element_by_id.js to C
authorVirgil Dupras <hsoft@hardcoded.net>
Fri, 27 Jul 2018 15:41:45 +0000 (11:41 -0400)
committerVirgil Dupras <hsoft@hardcoded.net>
Tue, 31 Jul 2018 17:31:45 +0000 (13:31 -0400)
src/ext-proxy.c
src/ext-proxy.h
src/input.c
src/scripts/focus_element_by_id.js [deleted file]
src/webextension/ext-dom.c
src/webextension/ext-dom.h
src/webextension/ext-main.c

index e26ecc3..f3ba0ab 100644 (file)
@@ -224,6 +224,16 @@ void ext_proxy_set_header(Client *c, const char *headers)
     dbus_call(c, "SetHeaderSetting", g_variant_new("(s)", headers), NULL);
 }
 
+void ext_proxy_lock_input(Client *c, const char *element_id)
+{
+    dbus_call(c, "LockInput", g_variant_new("(ts)", c->page_id, element_id), NULL);
+}
+
+void ext_proxy_unlock_input(Client *c, const char *element_id)
+{
+    dbus_call(c, "UnlockInput", g_variant_new("(ts)", c->page_id, element_id), NULL);
+}
+
 /**
  * Call a dbus method.
  */
index 95c5e5d..535dcf5 100644 (file)
@@ -27,5 +27,7 @@ void ext_proxy_eval_script(Client *c, char *js, GAsyncReadyCallback callback);
 GVariant *ext_proxy_eval_script_sync(Client *c, char *js);
 void ext_proxy_focus_input(Client *c);
 void ext_proxy_set_header(Client *c, const char *headers);
+void ext_proxy_lock_input(Client *c, const char *element_id);
+void ext_proxy_unlock_input(Client *c, const char *element_id);
 
 #endif /* end of include guard: _EXT_PROXY_H */
index 607b07b..ac6db07 100644 (file)
@@ -175,7 +175,7 @@ VbResult input_open_editor(Client *c)
     g_strfreev(argv);
 
     /* disable the active element */
-    ext_proxy_eval_script(c, "vimb_input_mode_element.disabled=true", NULL);
+    ext_proxy_lock_input(c, element_id);
 
     /* watch the editor process */
     EditorData *data = g_slice_new0(EditorData);
@@ -230,15 +230,13 @@ static void resume_editor(GPid pid, int status, EditorData *data)
     }
 
     if (data->element_id && strlen(data->element_id) > 0) {
-        jscode_enable = g_strdup_printf(JS_FOCUS_ELEMENT_BY_ID,
-                data->element_id, data->element_id);
+        ext_proxy_unlock_input(data->c, data->element_id);
     } else {
         jscode_enable = g_strdup_printf(JS_FOCUS_EDITOR_MAP_ELEMENT,
                 data->element_map_key, data->element_map_key);
+        ext_proxy_eval_script(data->c, jscode_enable, NULL);
+        g_free(jscode_enable);
     }
-    ext_proxy_eval_script(data->c, jscode_enable, NULL);
-    g_free(jscode_enable);
-
     g_unlink(data->file);
     g_free(data->file);
     g_free(data->element_id);
diff --git a/src/scripts/focus_element_by_id.js b/src/scripts/focus_element_by_id.js
deleted file mode 100644 (file)
index fb03e74..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-document.getElementById("%s").disabled=false;
-document.getElementById("%s").focus();
index 188d0a8..faff17f 100644 (file)
@@ -33,11 +33,11 @@ gboolean ext_dom_is_editable(WebKitDOMElement *element)
 {
     char *type;
     gboolean result = FALSE;
-    
+
     if (!element) {
         return FALSE;
     }
-    
+
     /* element is editable if it's a text area or input with no type, text or
      * password */
     if (webkit_dom_html_element_get_is_content_editable(WEBKIT_DOM_HTML_ELEMENT(element))
@@ -170,6 +170,27 @@ char *ext_dom_editable_get_value(WebKitDOMElement *element)
     return value;
 }
 
+void ext_dom_lock_input(WebKitDOMDocument *parent, char *element_id)
+{
+    WebKitDOMElement *elem;
+
+    elem = webkit_dom_document_get_element_by_id(parent, element_id);
+    if (elem != NULL) {
+        webkit_dom_element_set_attribute(elem, "disabled", "true", NULL);
+    }
+}
+
+void ext_dom_unlock_input(WebKitDOMDocument *parent, char *element_id)
+{
+    WebKitDOMElement *elem;
+
+    elem = webkit_dom_document_get_element_by_id(parent, element_id);
+    if (elem != NULL) {
+        webkit_dom_element_remove_attribute(elem, "disabled");
+        webkit_dom_element_focus(elem);
+    }
+}
+
 /**
  * Indicates if the give nelement is visible.
  */
@@ -177,3 +198,4 @@ static gboolean is_element_visible(WebKitDOMHTMLElement *element)
 {
     return TRUE;
 }
+
index fa25f4b..7a0cdda 100644 (file)
@@ -26,5 +26,7 @@
 gboolean ext_dom_is_editable(WebKitDOMElement *element);
 gboolean ext_dom_focus_input(WebKitDOMDocument *doc);
 char *ext_dom_editable_get_value(WebKitDOMElement *element);
+void ext_dom_lock_input(WebKitDOMDocument *parent, char *element_id);
+void ext_dom_unlock_input(WebKitDOMDocument *parent, char *element_id);
 
 #endif /* end of include guard: _EXT-DOM_H */
index 835921c..ccfe6aa 100644 (file)
@@ -84,6 +84,14 @@ static const char introspection_xml[] =
     "  <method name='SetHeaderSetting'>"
     "   <arg type='s' name='headers' direction='in'/>"
     "  </method>"
+    "  <method name='LockInput'>"
+    "   <arg type='t' name='page_id' direction='in'/>"
+    "   <arg type='s' name='elemend_id' direction='in'/>"
+    "  </method>"
+    "  <method name='UnlockInput'>"
+    "   <arg type='t' name='page_id' direction='in'/>"
+    "   <arg type='s' name='elemend_id' direction='in'/>"
+    "  </method>"
     " </interface>"
     "</node>";
 
@@ -419,6 +427,22 @@ static void dbus_handle_method_call(GDBusConnection *conn, const char *sender,
         }
         ext.headers = soup_header_parse_param_list(value);
         g_dbus_method_invocation_return_value(invocation, NULL);
+    } else if (!g_strcmp0(method, "LockInput")) {
+        g_variant_get(parameters, "(ts)", &pageid, &value);
+        page = get_web_page_or_return_dbus_error(invocation, WEBKIT_WEB_EXTENSION(extension), pageid);
+        if (!page) {
+            return;
+        }
+        ext_dom_lock_input(webkit_web_page_get_dom_document(page), value);
+        g_dbus_method_invocation_return_value(invocation, NULL);
+    } else if (!g_strcmp0(method, "UnlockInput")) {
+        g_variant_get(parameters, "(ts)", &pageid, &value);
+        page = get_web_page_or_return_dbus_error(invocation, WEBKIT_WEB_EXTENSION(extension), pageid);
+        if (!page) {
+            return;
+        }
+        ext_dom_unlock_input(webkit_web_page_get_dom_document(page), value);
+        g_dbus_method_invocation_return_value(invocation, NULL);
     }
 }