From: Virgil Dupras Date: Fri, 27 Jul 2018 15:41:45 +0000 (-0400) Subject: Convert focus_element_by_id.js to C X-Git-Url: https://git.owens.tech/projects.html/projects.html/git?a=commitdiff_plain;h=7b887666b79577b234115695ca30cb90a2011076;p=vimb.git Convert focus_element_by_id.js to C --- diff --git a/src/ext-proxy.c b/src/ext-proxy.c index e26ecc3..f3ba0ab 100644 --- a/src/ext-proxy.c +++ b/src/ext-proxy.c @@ -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. */ diff --git a/src/ext-proxy.h b/src/ext-proxy.h index 95c5e5d..535dcf5 100644 --- a/src/ext-proxy.h +++ b/src/ext-proxy.h @@ -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 */ diff --git a/src/input.c b/src/input.c index 607b07b..ac6db07 100644 --- a/src/input.c +++ b/src/input.c @@ -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 index fb03e74..0000000 --- a/src/scripts/focus_element_by_id.js +++ /dev/null @@ -1,2 +0,0 @@ -document.getElementById("%s").disabled=false; -document.getElementById("%s").focus(); diff --git a/src/webextension/ext-dom.c b/src/webextension/ext-dom.c index 188d0a8..faff17f 100644 --- a/src/webextension/ext-dom.c +++ b/src/webextension/ext-dom.c @@ -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; } + diff --git a/src/webextension/ext-dom.h b/src/webextension/ext-dom.h index fa25f4b..7a0cdda 100644 --- a/src/webextension/ext-dom.h +++ b/src/webextension/ext-dom.h @@ -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 */ diff --git a/src/webextension/ext-main.c b/src/webextension/ext-main.c index 835921c..ccfe6aa 100644 --- a/src/webextension/ext-main.c +++ b/src/webextension/ext-main.c @@ -84,6 +84,14 @@ static const char introspection_xml[] = " " " " " " + " " + " " + " " + " " + " " + " " + " " + " " " " ""; @@ -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); } }