/* functions */
static gboolean vp_process_input(const char* input);
+static void vp_run_user_script(void);
+static char* vp_jsref_to_string(JSContextRef context, JSValueRef ref);
static void vp_print_version(void);
static void vp_init(void);
static void vp_read_config(void);
vp_set_status(VP_STATUS_NORMAL);
}
+ /* run user script file */
+ vp_run_user_script();
+
/* status bar is updated by vp_set_mode */
vp_set_mode(VP_MODE_NORMAL , FALSE);
vp_update_urlbar(uri);
}
}
+static void vp_run_user_script(void)
+{
+ char* js = NULL;
+ GError* error = NULL;
+
+ if (g_file_test(core.files[FILES_SCRIPT], G_FILE_TEST_IS_REGULAR)
+ && g_file_get_contents(core.files[FILES_SCRIPT], &js, NULL, &error)
+ ) {
+ char* value = NULL;
+ char* error = NULL;
+
+ vp_eval_script(webkit_web_view_get_main_frame(vp.gui.webview), js, &value, &error);
+ if (error) {
+ fprintf(stderr, "%s", error);
+ g_free(error);
+ } else {
+ g_free(value);
+ }
+ g_free(js);
+ }
+}
+
+void vp_eval_script(WebKitWebFrame* frame, char* script, char** value, char** error)
+{
+ JSContextRef js = webkit_web_frame_get_global_context(frame);
+ JSValueRef exception = NULL;
+ JSValueRef result = NULL;
+ JSStringRef str = JSStringCreateWithUTF8CString(script);
+
+ result = JSEvaluateScript(js, str, JSContextGetGlobalObject(js), NULL, 0, &exception);
+ JSStringRelease(str);
+
+ if (result) {
+ *value = vp_jsref_to_string(js, result);
+ } else {
+ *error = vp_jsref_to_string(js, exception);
+ }
+}
+
+static char* vp_jsref_to_string(JSContextRef context, JSValueRef ref)
+{
+ char *string;
+ JSStringRef str_ref = JSValueToStringCopy(context, ref, NULL);
+ size_t len = JSStringGetMaximumUTF8CStringSize(str_ref);
+
+ string = g_new0(char, len);
+ JSStringGetUTF8CString(str_ref, string, len);
+ JSStringRelease(str_ref);
+
+ return string;
+}
+
static void vp_print_version(void)
{
fprintf(stderr, "%s/%s (build %s %s)\n", "vimp", VERSION, __DATE__, __TIME__);
core.files[FILES_CLOSED] = g_build_filename(path, "closed", NULL);
util_create_file_if_not_exists(core.files[FILES_CLOSED]);
+ core.files[FILES_SCRIPT] = g_build_filename(path, "scripts.js", NULL);
+
g_free(path);
}
#include <stdlib.h>
#include <string.h>
#include <webkit/webkit.h>
+#include <JavaScriptCore/JavaScript.h>
#ifdef HAS_GTK3
#include <gdk/gdkx.h>
#include <gtk/gtkx.h>
FILES_CONFIG = 0,
FILES_COOKIE,
FILES_CLOSED,
+ FILES_SCRIPT,
FILES_LAST
};
void vp_update_urlbar(const char* uri);
void vp_update_status_style(void);
void vp_echo(const MessageType type, gboolean hide, const char *error, ...);
+void vp_eval_script(WebKitWebFrame* frame, char* script, char** value, char** error);
gboolean vp_set_mode(Mode mode, gboolean clean);
void vp_set_widget_font(GtkWidget* widget, const VpColor* fg, const VpColor* bg, PangoFontDescription* font);
gboolean vp_load_uri(const Arg* arg);