From: Peter Hofmann Date: Sat, 14 Jun 2014 15:45:34 +0000 (+0200) Subject: Complete adblock X-Git-Tag: v1.0.0~317 X-Git-Url: https://git.armaanb.net/?p=chorizo.git;a=commitdiff_plain;h=15e4cb8ad0bcc9bc11a4470d1ae3bf93d16c05ae Complete adblock --- diff --git a/Makefile b/Makefile index 09e396e..2c9a12f 100644 --- 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 056fe1f..d6bff5d 100644 --- 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 9fc7d54..d10d892 100644 --- 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]);