Hi!

I asked some times ago for the possibility to run userdefinded javascripts on
every page load.

Here is a patch to do this. If someone else is interested in it I will updated
the manpages too.

I use also attached script file to block flash on all pages and to enable it
via mouseclick (script is from another brower project - but I can't find it
anymore).

Daniel
From 24020e87b6ab6a76cdd733f50644ddc81d12fc18 Mon Sep 17 00:00:00 2001
From: Daniel Carl <[email protected]>
Date: Sun, 9 Sep 2012 16:21:16 +0200
Subject: [PATCH 1/1] Added feature to run userland javascripts on every page
 load.

---
 Makefile |    3 ++-
 config.h |    4 ++++
 main.c   |   52 +++++++++++++++++++++++++++++++++++++++++++++-------
 3 files changed, 51 insertions(+), 8 deletions(-)

diff --git a/Makefile b/Makefile
index 772a30c..be4f119 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,8 @@
 TARGET = vimprobable2
 
 # Objectfiles, needed for $(TARGET)
-OBJ = main.o utilities.o callbacks.o
+#OBJ = main.o utilities.o callbacks.o
+OBJ = $(patsubst %.c, %.o, $(wildcard *.c))
 # Manpages
 MAN1 = vimprobable2.1
 MAN5 = vimprobablerc.5
diff --git a/config.h b/config.h
index 45957c4..55ac59a 100644
--- a/config.h
+++ b/config.h
@@ -82,6 +82,10 @@ static URIHandler uri_handlers[] = {
 /* user styles */
 #define             USER_STYLESHEET             "%s/vimprobable/style.css", config_base
 
+/* user javascript */
+#define             ENABLE_USER_SCRIPTFILE
+#define             USER_SCRIPTFILE             "%s/vimprobable/scripts.js", config_base
+
 /* ssl */
 static gboolean strict_ssl              = TRUE; /* FALSE will accept any SSL certificate at face value */
 static char ca_bundle[MAX_SETTING_SIZE] = "/etc/ssl/certs/ca-certificates.crt";
diff --git a/main.c b/main.c
index b3407ba..69d522e 100644
--- a/main.c
+++ b/main.c
@@ -81,6 +81,7 @@ static gboolean view_source(const Arg * arg);
 static gboolean zoom(const Arg *arg);
 static gboolean fake_key_event(const Arg *arg);
 
+static void clear_focus(void);
 static void update_url(const char *uri);
 static void setup_modkeys(void);
 static void setup_gui(void);
@@ -92,6 +93,7 @@ static void jsapi_evaluate_script(const gchar *script, gchar **value, gchar **me
 static void download_progress(WebKitDownload *d, GParamSpec *pspec);
 static void set_widget_font_and_color(GtkWidget *widget, const char *font_str,
                 const char *bg_color_str, const char *fg_color_str);
+static void scripts_run_user_file(void);
 
 static gboolean history(void);
 static gboolean process_set_line(char *line);
@@ -210,6 +212,7 @@ webview_load_committed_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointe
     update_url(uri);
     script(&a);
     g_free(a.s);
+    scripts_run_user_file();
 
     if (mode == ModeInsert || mode == ModeHints) {
         Arg a = { .i = ModeNormal };
@@ -222,15 +225,10 @@ void
 webview_load_finished_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data) {
     WebKitWebSettings *settings = webkit_web_view_get_settings(webview);
     gboolean scripts;
-    
+
     g_object_get(settings, "enable-scripts", &scripts, NULL);
     if (escape_input_on_load && scripts && !manual_focus && !gtk_widget_is_focus(inputbox)) {
-        Arg a = { .i = Silent, .s = g_strdup("hints.clearFocus();") };
-        script(&a);
-        g_free(a.s);
-        a.i = ModeNormal;
-        a.s = NULL;
-        set(&a);
+        clear_focus();
     }
     if (HISTORY_MAX_ENTRIES > 0)
         history();
@@ -1834,6 +1832,19 @@ focus_input(const Arg *arg) {
     return TRUE;
 }
 
+static void
+clear_focus(void) {
+    static Arg a;
+
+    a.s = g_strdup("hints.clearFocus();");
+    a.i = Silent;
+    script(&a);
+    g_free(a.s);
+    a.i = ModeNormal;
+    a.s = NULL;
+    set(&a);
+}
+
 static gboolean
 browser_settings(const Arg *arg) {
     char line[255];
@@ -2455,6 +2466,33 @@ setup_signals() {
         "inspect-web-view",                             G_CALLBACK(inspector_inspect_web_view_cb),   NULL);
 }
 
+#ifdef ENABLE_USER_SCRIPTFILE
+static void
+scripts_run_user_file() {
+    gchar *js = NULL, *user_scriptfile = NULL;
+    GError *error = NULL;
+
+    user_scriptfile = g_strdup_printf(USER_SCRIPTFILE);
+
+    /* run the users script file */
+    if (g_file_test(user_scriptfile, G_FILE_TEST_IS_REGULAR)
+            && g_file_get_contents(user_scriptfile, &js, NULL, &error)) {
+
+        gchar *value = NULL, *message = NULL;
+
+        jsapi_evaluate_script(js, &value, &message);
+        if (message) {
+            fprintf(stderr, "%s", message);
+            g_free(message);
+        }
+    } else {
+        fprintf(stderr, "Cannot open %s: %s\n", user_scriptfile, error ? error->message : "file not found");
+    }
+
+    g_free(user_scriptfile);
+}
+#endif
+
 #ifdef ENABLE_COOKIE_SUPPORT
 void
 setup_cookies()
-- 
1.7.9.5

Attachment: scripts.js
Description: application/javascript

Attachment: signature.asc
Description: Digital signature

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Vimprobable-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/vimprobable-users

Reply via email to