surf

surf
git clone git@git.zachrice.app:repos/surf.git
Log | Files | Refs | README | LICENSE

surf-adblock-webkit.diff (3184B)


      1 Description: WebKit Content Filter ad blocker for surf
      2 Uses WebKit2GTK's built-in Content Filter API (same engine as Safari
      3 content blockers) to block ads, trackers, and malware domains.
      4 
      5 On startup, surf compiles ~/.surf/adblock.json (WebKit Content Blocker
      6 JSON format) into bytecode and applies it to all views. The filter
      7 supports URL blocking, cookie blocking, and CSS element hiding.
      8 
      9 Run surf-adblock-update to download and convert Steven Black's unified
     10 hosts blocklist (~130k domains). Cron it weekly for auto-updates.
     11 
     12 Requires: curl (for the update script)
     13 Setup:    surf-adblock-update && restart surf
     14 
     15 Apply:   patch -p1 < patches/surf-adblock-webkit.diff
     16 Unpatch: patch -R -p1 < patches/surf-adblock-webkit.diff
     17 
     18 diff --git a/config.def.h b/config.def.h
     19 --- a/config.def.h
     20 +++ b/config.def.h
     21 @@ -8,6 +8,8 @@
     22  static char *cookiefile     = "~/.surf/cookies.txt";
     23 +static char *adblockdir    = "~/.surf/adblock/";
     24 +static char *adblockfile   = "~/.surf/adblock.json";
     25 
     26  /* Webkit default features */
     27 diff --git a/surf.c b/surf.c
     28 --- a/surf.c
     29 +++ b/surf.c
     30 @@ -181,6 +181,10 @@
     31  static void cleanup(void);
     32 
     33 +/* Adblock */
     34 +static void loadadblock(void);
     35 +static void adblockloadcb(GObject *src, GAsyncResult *res, gpointer unused);
     36 +
     37  /* GTK/WebKit */
     38 @@ -260,6 +260,8 @@
     39  static int spair[2];
     40 +static WebKitUserContentFilterStore *filterstore;
     41 +static WebKitUserContentFilter *adblockfilter;
     42  char *argv0;
     43 @@ -360,6 +360,8 @@
     44  	cookiefile  = buildfile(cookiefile);
     45  	scriptfile  = buildfile(scriptfile);
     46  	certdir     = buildpath(certdir);
     47 +	adblockdir  = buildpath(adblockdir);
     48 +	adblockfile = buildfile(adblockfile);
     49  	if (curconfig[Ephemeral].val.i)
     50 @@ -429,6 +429,8 @@
     51  	}
     52 +
     53 +	loadadblock();
     54  }
     55 @@ -1091,6 +1091,39 @@
     56  }
     57 
     58 +void
     59 +loadadblock(void)
     60 +{
     61 +	GFile *gf;
     62 +
     63 +	if (!g_file_test(adblockfile, G_FILE_TEST_EXISTS))
     64 +		return;
     65 +
     66 +	filterstore = webkit_user_content_filter_store_new(adblockdir);
     67 +	gf = g_file_new_for_path(adblockfile);
     68 +	webkit_user_content_filter_store_save_from_file(filterstore, "adblock",
     69 +	    gf, NULL, adblockloadcb, NULL);
     70 +	g_object_unref(gf);
     71 +}
     72 +
     73 +void
     74 +adblockloadcb(GObject *src, GAsyncResult *res, gpointer unused)
     75 +{
     76 +	GError *err = NULL;
     77 +	Client *c;
     78 +
     79 +	adblockfilter = webkit_user_content_filter_store_save_finish(
     80 +	    WEBKIT_USER_CONTENT_FILTER_STORE(src), res, &err);
     81 +	if (err) {
     82 +		fprintf(stderr, "surf: adblock: %s\n", err->message);
     83 +		g_error_free(err);
     84 +		return;
     85 +	}
     86 +	for (c = clients; c; c = c->next)
     87 +		webkit_user_content_manager_add_filter(
     88 +		    webkit_web_view_get_user_content_manager(c->view),
     89 +		    adblockfilter);
     90 +}
     91 +
     92  void
     93  cleanup(void)
     94  {
     95 @@ -1098,6 +1098,10 @@
     96  	while (clients)
     97  		destroyclient(clients);
     98 
     99 +	if (adblockfilter)
    100 +		webkit_user_content_filter_unref(adblockfilter);
    101 +	if (filterstore)
    102 +		g_object_unref(filterstore);
    103  	close(spair[0]);
    104 @@ -1103,6 +1103,8 @@
    105  	g_free(stylefile);
    106  	g_free(cachedir);
    107 +	g_free(adblockdir);
    108 +	g_free(adblockfile);
    109  	XCloseDisplay(dpy);
    110 @@ -1143,6 +1143,9 @@
    111 
    112  		contentmanager = webkit_user_content_manager_new();
    113 +		if (adblockfilter)
    114 +			webkit_user_content_manager_add_filter(
    115 +			    contentmanager, adblockfilter);
    116 
    117  		if (curconfig[Ephemeral].val.i) {