]> git.armaanb.net Git - chorizo.git/commitdiff
Use content manager to run scripts and user styles
authorArmaan Bhojwani <me@armaanb.net>
Thu, 10 Jun 2021 03:57:27 +0000 (23:57 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Thu, 10 Jun 2021 17:49:47 +0000 (13:49 -0400)
Not sure why this wasn't implemented upstream

man/chorizo-usage.1.scd
man/chorizo.1.scd
src/browser.c

index 1f7362952afcdfe1d26f8bd6c685c82eb9ca86b9..c21ae1daebe5c0b813de69ac44ee7e7ae74cd83f 100644 (file)
@@ -20,9 +20,9 @@ resume downloads. If a file already exists, it won't be touched. Instead, the
 new file name will have a suffix such as *.1*, *.2*, *.3*, and so on.
 
 # USER-SUPPLIED JAVASCRIPT FILES
 new file name will have a suffix such as *.1*, *.2*, *.3*, and so on.
 
 # USER-SUPPLIED JAVASCRIPT FILES
-After a page has been successfully loaded, the directory
-_~/.local/share/chorizo/user-scripts_ will be scanned and each file in it ending
-with *.js* will be run as a JavaScript file in the context of said page.
+When a page is being loaded, the directory _~/.local/share/chorizo/user-scripts_
+will be scanned and each file in it ending with *.js* will be run as a
+JavaScript file in the context of said page.
 
 *chorizo* comes with the following scripts:
 
 
 *chorizo* comes with the following scripts:
 
@@ -40,6 +40,11 @@ with *.js* will be run as a JavaScript file in the context of said page.
 Those bundled scripts are automatically installed on *make install*. To use
 them, though, make sure to link them to the directory mentioned above.
 
 Those bundled scripts are automatically installed on *make install*. To use
 them, though, make sure to link them to the directory mentioned above.
 
+# USER-SUPPLIED CSS FILES
+User supplied CSS files will be scanned for from
+_~/.local/share/chorizo/user-styles_, and be applied every time a page
+loads. The rules in these files override any rules provided by the website.
+
 # WEB EXTENSIONS
 On startup, WebKit checks _~/.local/share/chorizo/web_extensions_ for any *.so*
 files. See
 # WEB EXTENSIONS
 On startup, WebKit checks _~/.local/share/chorizo/web_extensions_ for any *.so*
 files. See
index 45048ba74b8c60077f4ea3ed9e3e5c951c7c205c..5ff49f0a5fffe3cb62af0a5ecff279d4862071d1 100644 (file)
@@ -52,18 +52,21 @@ XDG variables will be used to construct these paths.
        Database where cookies are stored. It is kept in the same format as Firefox,
        so the file can easily be copied over.
 
        Database where cookies are stored. It is kept in the same format as Firefox,
        so the file can easily be copied over.
 
-~/.local/share/chorizo/user-scripts
+*~/.local/share/chorizo/user-scripts*
        Directory to store user-supplied JavaScript snippets. See *chorizo-usage*(1).
 
        Directory to store user-supplied JavaScript snippets. See *chorizo-usage*(1).
 
-~/.local/share/chorizo/web_extensions
+*~/.local/share/chorizo/user-styles*
+       Directory to store user-supplied CSS snippets. See *chorizo-usage*(1).
+
+*~/.local/share/chorizo/web_extensions*
        Sets the directory where WebKit will look for web extensions. See
        *chorizo-usage*(1).
 
        Sets the directory where WebKit will look for web extensions. See
        *chorizo-usage*(1).
 
-~/.cache/chorizo
-
-~/.cache/webkitgtk
+*~/.cache/chorizo*
+*~/.cache/webkitgtk*
+       General caches.
 
 
-~/.local/share/webkitgtk
+*~/.local/share/webkitgtk*
        WebKitGTK will dump its caches and local storage here. It is probably wise to
        clean those directories regularly or to mount them as *tmpfs*(5).
 
        WebKitGTK will dump its caches and local storage here. It is probably wise to
        clean those directories regularly or to mount them as *tmpfs*(5).
 
index 032bc231e9287a3f086e6e0af94d59ef4a0e20a7..e6d890b3fdf2e51f8e1d146ea6c624c5a856286c 100644 (file)
@@ -152,13 +152,63 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
 
     c->focus_new_tab = focus_tab;
 
 
     c->focus_new_tab = focus_tab;
 
-    if (related_wv == NULL)
-        c->web_view = webkit_web_view_new();
-    else
+    if (related_wv == NULL) {
+        WebKitUserContentManager *ucm = webkit_user_content_manager_new();
+        WebKitUserScript *wkscript;
+        WebKitUserStyleSheet *wkstyle;
+        gchar *path = NULL, *source, *base;
+        const gchar *entry = NULL;
+        GDir *dir = NULL;
+
+        base = g_build_filename(g_get_user_data_dir(), __NAME__, "user-scripts",
+                                NULL);
+        dir = g_dir_open(base, 0, NULL);
+        if (dir != NULL) {
+            while ((entry = g_dir_read_name(dir)) != NULL) {
+                path = g_build_filename(base, entry, NULL);
+                if (g_str_has_suffix(path, ".js")) {
+                    g_file_get_contents(path, &source, NULL, NULL);
+                    wkscript = webkit_user_script_new(
+                        source, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
+                        WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START, NULL,
+                        NULL);
+                    webkit_user_content_manager_add_script(ucm, wkscript);
+                    webkit_user_script_unref(wkscript);
+                }
+                g_free(path);
+                g_free(source);
+            }
+            g_dir_close(dir);
+        }
+
+        base = g_build_filename(g_get_user_data_dir(), __NAME__, "user-styles",
+                                NULL);
+        dir = g_dir_open(base, 0, NULL);
+        if (dir != NULL) {
+            while ((entry = g_dir_read_name(dir)) != NULL) {
+                path = g_build_filename(base, entry, NULL);
+                if (g_str_has_suffix(path, ".css")) {
+                    g_file_get_contents(path, &source, NULL, NULL);
+                    wkstyle = webkit_user_style_sheet_new(
+                        source, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
+                        WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL);
+                    webkit_user_content_manager_add_style_sheet(ucm, wkstyle);
+                    webkit_user_style_sheet_unref(wkstyle);
+                }
+                g_free(path);
+                g_free(source);
+            }
+            g_dir_close(dir);
+        }
+
+        g_free(base);
+
+        c->web_view = webkit_web_view_new_with_user_content_manager(ucm);
+    } else {
         c->web_view = webkit_web_view_new_with_related_view(related_wv);
         c->web_view = webkit_web_view_new_with_related_view(related_wv);
+    }
 
     c->settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
 
     c->settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
-
     webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view),
                                    cfg.global_zoom);
     webkit_settings_set_enable_javascript(c->settings,
     webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view),
                                    cfg.global_zoom);
     webkit_settings_set_enable_javascript(c->settings,
@@ -378,8 +428,6 @@ changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data) {
          * references. */
         webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view), grab_feeds,
                                        NULL, grab_feeds_finished, c);
          * references. */
         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);
 }
     }
     gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
 }
@@ -1134,38 +1182,6 @@ remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data) {
     return TRUE;
 }
 
     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_data_dir(), __NAME__, "user-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);
-            char *jscmd = malloc(strlen(path) + 36);
-            sprintf(jscmd, "console.log(\"Running userscript %s\");", path);
-            if (g_str_has_suffix(path, ".js")) {
-                if (g_file_get_contents(path, &contents, NULL, NULL)) {
-                    webkit_web_view_run_javascript(web_view, jscmd, NULL, NULL,
-                                                   NULL);
-                    webkit_web_view_run_javascript(web_view, contents, NULL,
-                                                   NULL, NULL);
-                    g_free(contents);
-                    g_free(jscmd);
-                }
-            }
-            g_free(path);
-        }
-        g_dir_close(scriptdir);
-    }
-
-    g_free(base);
-}
-
 void
 show_web_view(WebKitWebView *web_view, gpointer data) {
     struct Client *c = (struct Client *)data;
 void
 show_web_view(WebKitWebView *web_view, gpointer data) {
     struct Client *c = (struct Client *)data;
@@ -1189,7 +1205,7 @@ trust_user_certs(WebKitWebContext *wc) {
     GTlsCertificate *cert;
     gchar *basedir, *absfile;
     const gchar *file;
     GTlsCertificate *cert;
     gchar *basedir, *absfile;
     const gchar *file;
-    GDir *dir;
+    GDir *dir = NULL;
 
     basedir = g_build_filename(g_get_user_data_dir(), __NAME__, "certs", NULL);
     dir = g_dir_open(basedir, 0, NULL);
 
     basedir = g_build_filename(g_get_user_data_dir(), __NAME__, "certs", NULL);
     dir = g_dir_open(basedir, 0, NULL);