]> git.armaanb.net Git - chorizo.git/commitdiff
Complete adblock
authorPeter Hofmann <scm@uninformativ.de>
Sat, 14 Jun 2014 15:45:34 +0000 (17:45 +0200)
committerPeter Hofmann <scm@uninformativ.de>
Sat, 14 Jun 2014 15:45:34 +0000 (17:45 +0200)
Makefile
README
zea.c

index 09e396ef93aae9dd9e5e1cef115da4033dcd94d0..2c9a12f4e6118e0ded810a153ec085271adaa44c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ CFLAGS += -Wall -Wextra -O3
 zea: zea.c
        $(CC) $(CFLAGS) $(LDFLAGS) \
                -o $@ $< \
-               `pkg-config --cflags --libs gtk+-2.0 webkit-1.0`
+               `pkg-config --cflags --libs gtk+-2.0 glib-2.0 webkit-1.0`
 
 clean:
        rm -f zea
diff --git a/README b/README
index 056fe1f19e560931665ce394b426f8a161e95f11..d6bff5d5a655dff588322f4362a42363d3deca0d 100644 (file)
--- a/README
+++ b/README
@@ -1,9 +1,9 @@
 zea - zeig's einfach an
 =======================
 
-zea is a minimalistic web browser using Gtk2 and WebKit. "Zeig's einfach
-an" is german and translates roughly to "just show it" (the damn web
-page).
+zea is a minimalistic web browser using Gtk2, GLib and WebKit. "Zeig's
+einfach an" is german and translates roughly to "just show it" (the damn
+web page).
 
 Features:
 
@@ -13,11 +13,11 @@ Features:
        - Pluggability into suckless' tabbed
        - vi-like scrolling (modified by CTRL)
        - Searching the current page for a word
+       - Adblock
        - Support for Flash and Java
 
 Planned features:
 
-       - Adblock
        - Keyword based searching (opening "wi foo" will search wikipedia)
 
 
@@ -33,6 +33,18 @@ Each new tab will then show your bookmarks and is scaled by a factor of
 0.8.
 
 
+Adblock
+=======
+
+zea has built-in adblock functionality. In each line of
+
+       ~/.config/zea/adblock.black
+
+you can store a regular expression. These expressions match
+case-insensitive and partially, i.e. ".*foo.*" is the same as ".*FOO.*"
+and you can use anchors like "^https?://...".
+
+
 Literature
 ==========
 
@@ -40,3 +52,9 @@ API references:
 
        - http://webkitgtk.org/reference/webkitgtk/stable/index.html
        - https://developer.gnome.org/gtk2/stable/index.html
+       - https://developer.gnome.org/glib/stable/index.html
+
+Regular expressions supported by GRegex, you can use these in your
+adblock patterns:
+
+       - https://developer.gnome.org/glib/stable/glib-regex-syntax.html
diff --git a/zea.c b/zea.c
index 9fc7d542bb33fb028378cf75e0b6091e3720d2e1..d10d8928d83079b42408e9c0c8b354046c51eeb1 100644 (file)
--- a/zea.c
+++ b/zea.c
@@ -17,6 +17,7 @@ static gboolean zea_do_download(WebKitWebView *, WebKitDownload *, gpointer);
 static gboolean zea_download_request(WebKitWebView *, WebKitWebFrame *,
                                      WebKitNetworkRequest *, gchar *,
                                      WebKitWebPolicyDecision *, gpointer);
+static void zea_load_adblock(void);
 static void zea_load_status_changed(GObject *obj, GParamSpec *pspec,
                                     gpointer data);
 static gboolean zea_location_key(GtkWidget *, GdkEvent *, gpointer);
@@ -39,6 +40,7 @@ static gdouble global_zoom = 1.0;
 static gchar *search_text = NULL;
 static gchar *first_uri = NULL;
 static gboolean show_all_requests = FALSE;
+static GSList *adblock_patterns = NULL;
 
 
 struct Client
@@ -57,17 +59,30 @@ zea_adblock(WebKitWebView *web_view, WebKitWebFrame *frame,
             WebKitWebResource *resource, WebKitNetworkRequest *request,
             WebKitNetworkResponse *response, gpointer data)
 {
+       GSList *it = adblock_patterns;
+       const gchar *uri;
+
        (void)web_view;
        (void)frame;
        (void)resource;
        (void)response;
        (void)data;
 
+       uri = webkit_network_request_get_uri(request);
        if (show_all_requests)
-               fprintf(stderr, "-> %s\n", webkit_network_request_get_uri(request));
+               fprintf(stderr, "-> %s\n", uri);
 
-       /* XXX Changing the URI here using webkit_network_request_set_uri()
-        * effectively blocks the request. */
+       while (it)
+       {
+               if (g_regex_match((GRegex *)(it->data), uri, 0, NULL))
+               {
+                       webkit_network_request_set_uri(request, "about:blank");
+                       if (show_all_requests)
+                               fprintf(stderr, "\tBLOCKED!\n");
+                       return;
+               }
+               it = g_slist_next(it);
+       }
 }
 
 void
@@ -326,6 +341,44 @@ zea_uri_changed(GObject *obj, GParamSpec *pspec, gpointer data)
        gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? "zea" : t));
 }
 
+void
+zea_load_adblock(void)
+{
+       GRegex *re = NULL;
+       GError *err = NULL;
+       GIOChannel *channel = NULL;
+       gchar *path = NULL;
+       gchar *buf = NULL;
+       gsize length, term;
+
+       path = g_strdup_printf("%s/zea/adblock.black", g_get_user_config_dir());
+       channel = g_io_channel_new_file(path, "r", &err);
+       if (channel != NULL)
+       {
+               while (g_io_channel_read_line(channel, &buf, &length, &term, &err)
+                      == G_IO_STATUS_NORMAL)
+               {
+                       g_strstrip(buf);
+                       re = g_regex_new(buf,
+                                        G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
+                                        G_REGEX_MATCH_PARTIAL, &err);
+                       if (err != NULL)
+                       {
+                               fprintf(stderr, "zea: Could not compile regex: %s\n", buf);
+                               g_error_free(err);
+                               err = NULL;
+                       }
+                       adblock_patterns = g_slist_append(adblock_patterns, re);
+
+                       g_free(buf);
+               }
+
+               if (err != NULL)
+                       g_free(err);
+       }
+       g_free(path);
+}
+
 void
 zea_load_status_changed(GObject *obj, GParamSpec *pspec, gpointer data)
 {
@@ -470,6 +523,8 @@ main(int argc, char **argv)
                exit(EXIT_FAILURE);
        }
 
+       zea_load_adblock();
+
        first_uri = g_strdup(argv[optind]);
        for (i = optind; i < argc; i++)
                zea_new_client(argv[i]);