]> 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 b8e219d12947c5c38c58e4d3740a7fd50204613d..e6d890b3fdf2e51f8e1d146ea6c624c5a856286c 100644 (file)
@@ -81,6 +81,8 @@ struct MainWindow {
 } mw;
 
 gint clients = 0;
+struct Client **client_arr;
+
 int cooperative_pipe_fp = 0;
 gchar *search_text;
 
@@ -150,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,
@@ -200,20 +252,20 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
     GtkWidget *locbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
 
     c->jsbutton = gtk_toggle_button_new_with_label("JS");
+    gtk_widget_set_tooltip_text(c->jsbutton, "Toggle JavaScript execution");
     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->jsbutton),
                                  !cfg.javascript_disabled);
     g_signal_connect(G_OBJECT(c->jsbutton), "toggled", G_CALLBACK(togglejs), c);
 
     c->imgbutton = gtk_toggle_button_new_with_label("IMG");
+    gtk_widget_set_tooltip_text(c->imgbutton, "Toggle image loading");
     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->imgbutton),
                                  !cfg.images_disabled);
     g_signal_connect(G_OBJECT(c->imgbutton), "toggled", G_CALLBACK(toggleimg),
                      c);
 
     c->location = gtk_entry_new();
-    gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 5);
-    gtk_box_pack_end(GTK_BOX(locbox), c->jsbutton, FALSE, FALSE, 0);
-    gtk_box_pack_end(GTK_BOX(locbox), c->imgbutton, FALSE, FALSE, 0);
+    gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 0);
 
     if (cfg.private) {
         GtkWidget *privindicator = gtk_label_new("Private mode");
@@ -223,6 +275,9 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
         gtk_box_pack_end(GTK_BOX(locbox), privindicator, FALSE, FALSE, 5);
     }
 
+    gtk_box_pack_start(GTK_BOX(locbox), c->jsbutton, FALSE, FALSE, 5);
+    gtk_box_pack_start(GTK_BOX(locbox), c->imgbutton, FALSE, FALSE, 0);
+
     g_signal_connect(G_OBJECT(c->location), "key-press-event",
                      G_CALLBACK(key_location), c);
     g_signal_connect(G_OBJECT(c->location), "icon-release",
@@ -291,6 +346,9 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
     set_uri(uri, c);
 
     clients++;
+    client_arr = realloc(client_arr, (clients + 1) * sizeof(client_arr[0]));
+    client_arr[clients] = c;
+
     return WEBKIT_WEB_VIEW(c->web_view);
 }
 
@@ -370,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);
 }
@@ -422,9 +478,9 @@ changed_title(GObject *obj, GParamSpec *pspec, gpointer data) {
     gboolean mute = webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view));
     gchar *muted = (mute) ? "[m] " : "";
     sprintf(name, "%s%s", muted, t);
-
     gtk_label_set_text(GTK_LABEL(c->tablabel), name);
     g_free(name);
+
     gtk_widget_set_tooltip_text(c->tablabel, t);
     mainwindow_title(gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
 }
@@ -711,7 +767,7 @@ init_default_web_context(void) {
         webkit_web_context_set_preferred_languages(wc, cfg.accepted_language);
 
     g_signal_connect(G_OBJECT(wc), "download-started",
-                     G_CALLBACK(download_start), &cfg);
+                     G_CALLBACK(download_start), cfg.download_dir);
 
     trust_user_certs(wc);
 
@@ -726,6 +782,7 @@ init_default_web_context(void) {
         WebKitCookiePersistentStorage type =
             WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE;
         webkit_cookie_manager_set_persistent_storage(cm, fname, type);
+        g_free(fname);
     }
 
     const gchar *const languages[2] = {(const gchar *)cfg.spellcheck_language,
@@ -1068,6 +1125,7 @@ mainwindow_setup(void) {
     gchar *title = malloc(strlen(priv) + strlen(__NAME__) + 1);
     sprintf(title, "%s%s", __NAME__, priv);
     gtk_window_set_title(GTK_WINDOW(mw.win), title);
+    g_free(title);
 
     mw.notebook = gtk_notebook_new();
     gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
@@ -1124,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;
@@ -1177,17 +1203,20 @@ show_web_view(WebKitWebView *web_view, gpointer data) {
 void
 trust_user_certs(WebKitWebContext *wc) {
     GTlsCertificate *cert;
-    const gchar *basedir, *file, *absfile;
-    GDir *dir;
+    gchar *basedir, *absfile;
+    const gchar *file;
+    GDir *dir = NULL;
 
     basedir = g_build_filename(g_get_user_data_dir(), __NAME__, "certs", NULL);
     dir = g_dir_open(basedir, 0, NULL);
+    g_free(basedir);
     if (dir != NULL) {
         file = g_dir_read_name(dir);
         while (file != NULL) {
             absfile = g_build_filename(g_get_user_data_dir(), __NAME__, "certs",
                                        file, NULL);
             cert = g_tls_certificate_new_from_file(absfile, NULL);
+            g_free(absfile);
             if (cert == NULL)
                 fprintf(stderr, __NAME__ ": Could not load trusted cert '%s'\n",
                         file);
@@ -1206,17 +1235,16 @@ get_ini(void) {
     config = g_key_file_new();
 
     // Load user config
-    if (!g_key_file_load_from_file(config,
-                                   g_build_filename(g_get_user_config_dir(),
-                                                    __NAME__, "chorizo.ini",
-                                                    NULL),
-                                   flags, NULL)) {
+    gchar *fname = g_build_filename(g_get_user_config_dir(), __NAME__,
+                                    "chorizo.ini", NULL);
+    if (!g_key_file_load_from_file(config, fname, flags, NULL)) {
         // Load global config
         if (!g_key_file_load_from_file(config, "/etc/chorizo.ini", flags,
                                        NULL)) {
             fprintf(stderr, "Could not load chorizo.ini");
         }
     }
+    g_free(fname);
     return config;
 }
 
@@ -1253,6 +1281,7 @@ main(int argc, char **argv) {
     downloadmanager_setup();
     mainwindow_setup();
 
+    client_arr = malloc(sizeof(struct Client *));
     if (optind >= argc) {
         client_new(cfg.home_uri, NULL, TRUE, TRUE);
     } else {
@@ -1263,5 +1292,9 @@ main(int argc, char **argv) {
     if (!cfg.cooperative_instances || cfg.cooperative_alone)
         gtk_main();
 
+    for (int i = 0; i < clients; i++) {
+        free(&(client_arr[i]));
+    }
+
     exit(EXIT_SUCCESS);
 }