]> git.armaanb.net Git - chorizo.git/commitdiff
Introduce user-supplied JavaScript files
authorPeter Hofmann <scm@uninformativ.de>
Sat, 4 Jan 2020 08:39:24 +0000 (09:39 +0100)
committerPeter Hofmann <scm@uninformativ.de>
Sat, 22 Feb 2020 08:55:32 +0000 (09:55 +0100)
CHANGES
browser.c
man1/lariza.1
man1/lariza.usage.1

diff --git a/CHANGES b/CHANGES
index 362bc82de667b87095cdb47f042c9984542b5bcc..6efe05007ac9278af254e275883530e8c14bcd15 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,5 +1,10 @@
 Release history for lariza
 
+next
+  [Added]
+  - User-supplied JavaScript files can be put into
+    ~/.config/lariza/scripts.
+
 v19.05  2019-05-05
   [Fixed]
   - We no longer use WebKit's deprecated JavaScript API.
index 6d022e543faf9952504218d92175b2f306b46174..aae2faf2ea4e846731f4f2054add1792f8843666 100644 (file)
--- a/browser.c
+++ b/browser.c
@@ -48,6 +48,7 @@ static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
                               WebKitHitTestResult *, gpointer);
 static gboolean quit_if_nothing_active(void);
 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
+static void run_user_scripts(WebKitWebView *);
 static void search(gpointer, gint);
 static void show_web_view(WebKitWebView *, gpointer);
 static Window tabbed_launch(void);
@@ -372,6 +373,8 @@ changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
         webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
                                        grab_feeds, NULL,
                                        grab_feeds_finished, c);
+
+        run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
     }
     gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
 }
@@ -1127,6 +1130,36 @@ remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
     return TRUE;
 }
 
+void
+run_user_scripts(WebKitWebView *web_view)
+{
+    gchar *base = NULL, *path = NULL, *contents = NULL;
+    const gchar *entry = NULL;
+    GDir *scriptdir = NULL;
+
+    base = g_build_filename(g_get_user_config_dir(), __NAME__, "scripts", NULL);
+    scriptdir = g_dir_open(base, 0, NULL);
+    if (scriptdir != NULL)
+    {
+        while ((entry = g_dir_read_name(scriptdir)) != NULL)
+        {
+            path = g_build_filename(base, entry, NULL);
+            if (g_str_has_suffix(path, ".js"))
+            {
+                if (g_file_get_contents(path, &contents, NULL, NULL))
+                {
+                    webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
+                    g_free(contents);
+                }
+            }
+            g_free(path);
+        }
+        g_dir_close(scriptdir);
+    }
+
+    g_free(base);
+}
+
 void
 search(gpointer data, gint direction)
 {
index 9c5a92f6c7b72b9eebd2be812926cc1bf7101239..cbd3783d7e6205c6eab6fd1562b2f3538927de5f 100644 (file)
@@ -101,6 +101,10 @@ Directory where trusted certificates are stored. See
 Configuration file for keyword base searching. See
 \fBlariza.usage\fP(1).
 .TP
+\fI~/.config\:/lariza\:/scripts\fP
+Directory to store user-supplied JavaScript snippets. See
+\fBlariza.usage\fP(1).
+.TP
 \fI~/.local\:/share\:/lariza\:/web_extensions\fP
 Sets the directory where WebKit will look for web extensions. See
 \fBlariza.usage\fP(1).
index 17279ccd2afa0dfb00692425d4bd101e836dedcc..fec4bdb027f30d51ecc74dd28a9a51c6cb9d4dd7 100644 (file)
@@ -149,6 +149,15 @@ is where your search term will be placed.
 .P
 Lines starting with \fB#\fP are ignored.
 .\" --------------------------------------------------------------------
+.SH "USER-SUPPLIED JAVASCRIPT FILES"
+After a page has been successfully loaded, the directory
+\fI~/.config\:/lariza\:/scripts\fP will be scanned and each file in it
+ending with \fB.js\fP will be run as a JavaScript file in the context of
+said page.
+.P
+During development, you will most likely want to run \fBlariza\fP with
+$\fBLARIZA_ENABLE_CONSOLE_TO_STDOUT\fP enabled.
+.\" --------------------------------------------------------------------
 .SH "TRUSTED CERTIFICATES"
 By default, \fBlariza\fP trusts whatever CAs are trusted by WebKit, i.e. by
 your GnuTLS installation. If you wish to trust additional certificates,