]> git.armaanb.net Git - chorizo.git/blobdiff - src/browser.c
Use content manager to run scripts and user styles
[chorizo.git] / src / browser.c
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;
 
-    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->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,
@@ -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);
-
-        run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
     }
     gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
 }
@@ -1134,38 +1182,6 @@ 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_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;
@@ -1189,7 +1205,7 @@ trust_user_certs(WebKitWebContext *wc) {
     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);