summaryrefslogtreecommitdiffstats
path: root/surf.c
diff options
context:
space:
mode:
authorGravatar BanceDev 2026-02-19 11:17:56 -0500
committerGravatar BanceDev 2026-02-19 11:17:56 -0500
commitfb95e27168ba9f8a4b654a5c15bc087f1343f53c (patch)
tree1ff2be11b54fc3c77782d7486232ecd736ce357e /surf.c
parentswitch to ecosia search (diff)
json list adblocker
Diffstat (limited to '')
-rw-r--r--surf.c118
1 files changed, 118 insertions, 0 deletions
diff --git a/surf.c b/surf.c
index 65bdbb3..cf51e7c 100644
--- a/surf.c
+++ b/surf.c
@@ -253,6 +253,7 @@ static int cookiepolicy;
static Display *dpy;
static Client *clients;
static char *stylefile;
+static char *adblockjson;
static const char *useragent;
static Parameter *curconfig;
static int modparams[ParameterLast];
@@ -1092,6 +1093,109 @@ cleanup(void)
XCloseDisplay(dpy);
}
+typedef struct {
+ WebKitUserContentManager *ucm;
+} AdblockCtx;
+
+static void
+adblock_filter_ready(GObject *store, GAsyncResult *res, gpointer data)
+{
+ AdblockCtx *ctx = data;
+ WebKitUserContentFilter *filter;
+ GError *err = NULL;
+
+ filter = webkit_user_content_filter_store_load_finish(
+ WEBKIT_USER_CONTENT_FILTER_STORE(store), res, &err);
+
+ if (err) {
+ fprintf(stderr, "surf: adblock filter not compiled yet, run: surf -A /path/to/easylist.json\n");
+ g_error_free(err);
+ g_free(ctx);
+ return;
+ }
+
+ webkit_user_content_manager_add_filter(ctx->ucm, filter);
+ webkit_user_content_filter_unref(filter);
+ g_free(ctx);
+}
+
+static void
+adblock_compiled(GObject *store, GAsyncResult *res, gpointer data)
+{
+ AdblockCtx *ctx = data;
+ WebKitUserContentFilter *filter;
+ GError *err = NULL;
+
+ filter = webkit_user_content_filter_store_save_finish(
+ WEBKIT_USER_CONTENT_FILTER_STORE(store), res, &err);
+
+ if (err) {
+ fprintf(stderr, "surf: adblock compile error: %s\n", err->message);
+ g_error_free(err);
+ } else {
+ fprintf(stderr, "surf: adblock filter compiled and cached\n");
+ webkit_user_content_manager_add_filter(ctx->ucm, filter);
+ webkit_user_content_filter_unref(filter);
+ }
+ g_free(ctx);
+ if (mainloop && g_main_loop_is_running(mainloop))
+ g_main_loop_quit(mainloop);
+}
+
+static void
+adblock_load(WebKitUserContentManager *ucm)
+{
+ gchar *storedir;
+ WebKitUserContentFilterStore *store;
+ AdblockCtx *ctx;
+
+ storedir = g_build_filename(g_get_home_dir(), ".surf", "contentfilter", NULL);
+ g_mkdir_with_parents(storedir, 0700);
+ store = webkit_user_content_filter_store_new(storedir);
+ g_free(storedir);
+
+ ctx = g_new0(AdblockCtx, 1);
+ ctx->ucm = ucm;
+
+ webkit_user_content_filter_store_load(store, "easylist", NULL,
+ adblock_filter_ready, ctx);
+ g_object_unref(store);
+}
+
+static void
+adblock_compile(const char *jsonfile, WebKitUserContentManager *ucm)
+{
+ gchar *storedir;
+ WebKitUserContentFilterStore *store;
+ AdblockCtx *ctx;
+ GBytes *jsonbytes;
+ gchar *jsondata = NULL;
+ gsize jsonlen = 0;
+ GError *err = NULL;
+
+ if (!g_file_get_contents(jsonfile, &jsondata, &jsonlen, &err)) {
+ fprintf(stderr, "surf: could not read adblock JSON '%s': %s\n",
+ jsonfile, err->message);
+ g_error_free(err);
+ return;
+ }
+
+ storedir = g_build_filename(g_get_home_dir(), ".surf", "contentfilter", NULL);
+ g_mkdir_with_parents(storedir, 0700);
+ store = webkit_user_content_filter_store_new(storedir);
+ g_free(storedir);
+
+ jsonbytes = g_bytes_new_take(jsondata, jsonlen);
+ ctx = g_new0(AdblockCtx, 1);
+ ctx->ucm = ucm;
+
+ fprintf(stderr, "surf: compiling adblock filter from %s...\n", jsonfile);
+ webkit_user_content_filter_store_save(store, "easylist", jsonbytes, NULL,
+ adblock_compiled, ctx);
+ g_bytes_unref(jsonbytes);
+ g_object_unref(store);
+}
+
WebKitWebView *
newview(Client *c, WebKitWebView *rv)
{
@@ -1139,6 +1243,7 @@ newview(Client *c, WebKitWebView *rv)
useragent = webkit_settings_get_user_agent(settings);
contentmanager = webkit_user_content_manager_new();
+ adblock_load(contentmanager);
/* WebKit 6.0: network session owns ephemeral mode, cookies, TLS */
if (curconfig[Ephemeral].val.i) {
@@ -2048,6 +2153,9 @@ main(int argc, char *argv[])
defconfig[CookiePolicies].val.v = EARGF(usage());
defconfig[CookiePolicies].prio = 2;
break;
+ case 'A':
+ adblockjson = EARGF(usage());
+ break;
case 'b':
defconfig[ScrollBars].val.i = 0;
defconfig[ScrollBars].prio = 2;
@@ -2169,6 +2277,16 @@ main(int argc, char *argv[])
c = newclient(NULL);
showview(NULL, c);
+ if (adblockjson) {
+ adblock_compile(adblockjson,
+ webkit_web_view_get_user_content_manager(c->view));
+ mainloop = g_main_loop_new(NULL, FALSE);
+ g_main_loop_run(mainloop);
+ g_main_loop_unref(mainloop);
+ cleanup();
+ return 0;
+ }
+
loaduri(c, &arg);
updatetitle(c);