diff options
author | Daniel Carl <danielcarl@gmx.de> | 2016-09-28 00:03:32 +0200 |
---|---|---|
committer | Daniel Carl <danielcarl@gmx.de> | 2016-09-28 00:03:32 +0200 |
commit | 12d53b57e29807d619c114cdd0bef76bc96772a7 (patch) | |
tree | 3316a482920d83ca710836868bc1a9902eb34918 /src/ex.c | |
parent | c4b159a1ba42d4456ba918a5925c71917f6ce44a (diff) |
Show output of :eval in inputbox.
Diffstat (limited to 'src/ex.c')
-rw-r--r-- | src/ex.c | 47 |
1 files changed, 45 insertions, 2 deletions
@@ -22,6 +22,7 @@ * commands from inputbox and the ex commands. */ +#include <JavaScriptCore/JavaScript.h> #include <string.h> #include <sys/wait.h> @@ -121,6 +122,8 @@ static VbCmdResult execute(Client *c, const ExArg *arg); static VbCmdResult ex_bookmark(Client *c, const ExArg *arg); static VbCmdResult ex_eval(Client *c, const ExArg *arg); +static void ex_eval_javascript_finished(GObject *object, + GAsyncResult *result, Client *c); static VbCmdResult ex_hardcopy(Client *c, const ExArg *arg); static VbCmdResult ex_map(Client *c, const ExArg *arg); static VbCmdResult ex_unmap(Client *c, const ExArg *arg); @@ -747,11 +750,51 @@ static VbCmdResult ex_bookmark(Client *c, const ExArg *arg) static VbCmdResult ex_eval(Client *c, const ExArg *arg) { - /* TODO allow to get the return value and possible errors. */ - webkit_web_view_run_javascript(c->webview, arg->rhs->str, NULL, NULL, NULL); + /* Called as :eval! - don't print to inputbox. */ + if (arg->bang) { + webkit_web_view_run_javascript(c->webview, arg->rhs->str, NULL, NULL, NULL); + } else { + webkit_web_view_run_javascript(c->webview, arg->rhs->str, NULL, + (GAsyncReadyCallback)ex_eval_javascript_finished, c); + } + return CMD_SUCCESS; } +static void ex_eval_javascript_finished(GObject *object, + GAsyncResult *result, Client *c) +{ + WebKitJavascriptResult *js_result; + JSValueRef value; + JSGlobalContextRef context; + GError *error = NULL; + + js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object), result, &error); + if (!js_result) { + vb_echo(c, MSG_ERROR, TRUE, "%s", error->message); + g_error_free(error); + + return; + } + + context = webkit_javascript_result_get_global_context(js_result); + value = webkit_javascript_result_get_value(js_result); + if (JSValueIsString(context, value)) { + JSStringRef str_ref; + char *string; + size_t len; + + str_ref = JSValueToStringCopy(context, value, NULL); + len = JSStringGetMaximumUTF8CStringSize(str_ref); + string = g_new(char, len); + JSStringGetUTF8CString(str_ref, string, len); + JSStringRelease(str_ref); + vb_echo(c, MSG_NORMAL, FALSE, "%s", string); + g_free(string); + } + webkit_javascript_result_unref(js_result); +} + static VbCmdResult ex_hardcopy(Client *c, const ExArg *arg) { /* TODO no implemented yet */ |