commit: 5547d3a708e75a62553b19c81e1afc66ef668a5c
parent 01237f5229147431a26c86675657e2fe588928fb
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 15 Mar 2019 10:26:31 +0100
Handle WebKit content-filter
Diffstat:
M | badwolf.1 | 19 | +++++++++++++------ |
M | badwolf.c | 87 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- |
M | badwolf.h | 2 | ++ |
M | po/messages.pot | 87 | +++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------- |
4 files changed, 156 insertions(+), 39 deletions(-)
diff --git a/badwolf.1 b/badwolf.1
@@ -1,6 +1,6 @@
-./" BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
-./" Copyright © 2019-2020 Badwolf Authors <https://hacktivis.me/projects/badwolf>
-./" SPDX-License-Identifier: BSD-3-Clause
+.\" BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
+.\" Copyright © 2019-2021 Badwolf Authors <https://hacktivis.me/projects/badwolf>
+.\" SPDX-License-Identifier: BSD-3-Clause
.Dd 2019-10-31
.Dt BADWOLF 1
.Sh NAME
@@ -75,6 +75,16 @@ A more generic variable name is also intended to be used in the future.
.El
.Sh FILES
.Bl -tag -width Ds -compact
+.It Pa ${XDG_CONFIG_HOME:-$HOME/.config}/badwolf/content-filters.json
+WebKit-specific content-filter file, this allows to block unwanted content (ads, nagware, ...).
+For some introductory information about the format see:
+.Lk https://webkit.org/blog/3476/content-blockers-first-look/
+.Pp
+For a converter using AblockPlus-style filters, try:
+.Lk https://gitlab.com/eyeo/adblockplus/abp2blocklist
+.Pp
+For a ready-to-use file (that you should update periodically), try:
+.Lk https://easylist-downloads.adblockplus.org/easylist_min_content_blocker.json
.It Pa ${XDG_DATA_HOME:-$HOME/.local/share}/badwolf/bookmarks.xbel
XBEL (XML Bookmark Exchange Language) file, known to be currently supported by:
.Xr elinks 1 ,
@@ -90,10 +100,7 @@ to be loaded into
.Nm . Note: They aren't the JavaScript-based Web-Extensions supported by Firefox or Chrome, but native code in shared objects using the WebKitGTK API.
.Pp
Examples of useful extensions may be found at:
-.Bl -item -compact
.Lk https://hacktivis.me/git/badwolf-extensions
-.Lk https://github.com/jun7/wyebadblock
-.El
.It Pa ${DATADIR:-/usr/local/share}/badwolf/interface.css
.It Pa ${XDG_DATA_HOME:-$HOME/.local/share}/badwolf/interface.css
CSS files (respectively system and user-level) for styling badwolf interface.
diff --git a/badwolf.c b/badwolf.c
@@ -70,6 +70,7 @@ static void new_tabCb_clicked(GtkButton *new_tab, gpointer user_data);
static void closeCb_clicked(GtkButton *close, gpointer user_data);
static void
notebookCb_switch__page(GtkNotebook *notebook, GtkWidget *page, guint page_num, gpointer user_data);
+void content_managerCb_ready(GObject *store, GAsyncResult *result, gpointer user_data);
static gboolean
WebViewCb_close(WebKitWebView *webView, gpointer user_data)
@@ -710,6 +711,8 @@ new_browser(struct Window *window, const gchar *target_url, struct Client *old_b
old_browser == NULL ? NULL : old_browser->webView,
"settings",
settings,
+ "user-content-manager",
+ window->content_manager,
NULL));
gtk_widget_set_name(GTK_WIDGET(browser->webView), "browser__webView");
if(old_browser == NULL)
@@ -950,10 +953,73 @@ notebookCb_switch__page(GtkNotebook *notebook, GtkWidget *page, guint page_num,
gtk_window_set_title(GTK_WINDOW(window->main_window), gtk_widget_get_tooltip_text(label));
}
+void
+content_managerCb_ready(GObject *store, GAsyncResult *result, gpointer user_data)
+{
+ struct Window *window = (struct Window *)user_data;
+ GError *err = NULL;
+
+ WebKitUserContentFilter *filter =
+ webkit_user_content_filter_store_load_finish(window->content_store, result, &err);
+
+ if(filter == NULL)
+ {
+ if(err == NULL)
+ {
+ fprintf(stderr, _("badwolf: failed to load content-filter, err: [%d] %s\n"), -1, "unknown");
+ }
+ else
+ {
+ fprintf(stderr,
+ _("badwolf: failed to load content-filter, err: [%d] %s\n"),
+ err->code,
+ err->message);
+ }
+ }
+ else
+ {
+ fprintf(stderr, _("badwolf: content-filter loaded, adding to content-manager…\n"));
+ webkit_user_content_manager_add_filter(window->content_manager, filter);
+ }
+}
+
+static void
+storeCb_finish(WebKitUserContentFilterStore *store, GAsyncResult *result, gpointer user_data)
+{
+ struct Window *window = (struct Window *)user_data;
+ GError *err = NULL;
+
+ WebKitUserContentFilter *filter =
+ webkit_user_content_filter_store_save_finish(window->content_store, result, &err);
+
+ if(filter == NULL)
+ {
+ if(err == NULL)
+ {
+ fprintf(stderr,
+ _("badwolf: failed to compile content-filters.json, err: [%d] %s\n"),
+ -1,
+ "unknown");
+ }
+ else
+ {
+ fprintf(stderr,
+ _("badwolf: failed to compile content-filters.json, err: [%d] %s\n"),
+ err->code,
+ err->message);
+ }
+ }
+ else
+ {
+ webkit_user_content_filter_store_load(
+ window->content_store, "a", NULL, content_managerCb_ready, window);
+ }
+}
+
int
main(int argc, char *argv[])
{
- struct Window *window = &(struct Window){NULL, NULL, NULL, NULL};
+ struct Window *window = &(struct Window){NULL, NULL, NULL, NULL, NULL, NULL};
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, DATADIR "/locale");
bind_textdomain_codeset(PACKAGE, "UTF-8");
@@ -983,7 +1049,24 @@ main(int argc, char *argv[])
window->main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
window->notebook = gtk_notebook_new();
window->new_tab = gtk_button_new_from_icon_name("tab-new-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
- window->downloads_tab = badwolf_downloads_tab_new();
+ window->downloads_tab = badwolf_downloads_tab_new();
+ window->content_manager = webkit_user_content_manager_new();
+
+ gchar *contentFilterPath =
+ g_build_filename(g_get_user_config_dir(), g_get_prgname(), "content-filters.json", NULL);
+ GFile *contentFilterFile = g_file_new_for_path(contentFilterPath);
+ fprintf(stderr, _("content-filters file set to: %s\n"), contentFilterPath);
+
+ gchar *filtersPath = g_build_filename(g_get_user_cache_dir(), g_get_prgname(), "filters", NULL);
+ fprintf(stderr, _("content-filters directory set to: %s\n"), filtersPath);
+ window->content_store = webkit_user_content_filter_store_new(filtersPath);
+
+ webkit_user_content_filter_store_save_from_file(window->content_store,
+ "a",
+ contentFilterFile,
+ NULL,
+ (GAsyncReadyCallback)storeCb_finish,
+ window);
gtk_window_set_default_size(
GTK_WINDOW(window->main_window), BADWOLF_DEFAULT_WIDTH, BADWOLF_DEFAULT_HEIGHT);
diff --git a/badwolf.h b/badwolf.h
@@ -14,6 +14,8 @@ struct Window
GtkWidget *notebook;
GtkWidget *new_tab;
GtkWidget *downloads_tab;
+ WebKitUserContentManager *content_manager;
+ WebKitUserContentFilterStore *content_store;
};
struct Client
diff --git a/po/messages.pot b/po/messages.pot
@@ -6,9 +6,9 @@
#, fuzzy
msgid ""
msgstr ""
-"Project-Id-Version: Badwolf 1.0.3+g1b10b339.develop\n"
+"Project-Id-Version: Badwolf 1.0.3+gdf1e25f.content-blockers\n"
"Report-Msgid-Bugs-To: contact+badwolf-msgid@hacktivis.me\n"
-"POT-Creation-Date: 2021-03-30 14:25+0200\n"
+"POT-Creation-Date: 2021-04-09 06:17+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -74,23 +74,23 @@ msgstr ""
msgid "Bookmarks: unable to parse file \"%s\"\n"
msgstr ""
-#: badwolf.c:954
+#: badwolf.c:1030
#, c-format
msgid "Buildtime WebKit version: %d.%d.%d\n"
msgstr ""
-#: badwolf.c:439
+#: badwolf.c:440
msgid "Continue"
msgstr ""
-#: badwolf.c:389
+#: badwolf.c:390
msgid ""
"Couldn't verify the TLS certificate to ensure a better security of the "
"connection. You might want to verify your machine and network.\n"
"\n"
msgstr ""
-#: badwolf.c:99
+#: badwolf.c:100
msgid "Crashed"
msgstr ""
@@ -98,31 +98,31 @@ msgstr ""
msgid "Download starting…"
msgstr ""
-#: badwolf.c:412
+#: badwolf.c:413
msgid "Error: Some unknown error occurred validating the certificate.\n"
msgstr ""
-#: badwolf.c:393
+#: badwolf.c:394
msgid "Error: The X509 Certificate Authority is unknown.\n"
msgstr ""
-#: badwolf.c:406
+#: badwolf.c:407
msgid "Error: The certificate has been revoked.\n"
msgstr ""
-#: badwolf.c:403
+#: badwolf.c:404
msgid "Error: The certificate has expired. Check your system's clock.\n"
msgstr ""
-#: badwolf.c:409
+#: badwolf.c:410
msgid "Error: The certificate is considered to be insecure.\n"
msgstr ""
-#: badwolf.c:400
+#: badwolf.c:401
msgid "Error: The certificate isn't valid yet. Check your system's clock.\n"
msgstr ""
-#: badwolf.c:396
+#: badwolf.c:397
msgid "Error: The given identity doesn't match the expected one.\n"
msgstr ""
@@ -133,79 +133,104 @@ msgid ""
"Runtime WebKit version: %d.%d.%d"
msgstr ""
-#: badwolf.c:889
+#: badwolf.c:904
msgid "New tab"
msgstr ""
-#: badwolf.c:1006
+#: badwolf.c:1099
msgid "Open new tab"
msgstr ""
-#: badwolf.c:103
+#: badwolf.c:104
msgid "Out of Memory"
msgstr ""
-#: badwolf.c:952
+#: badwolf.c:1028
#, c-format
msgid "Running Badwolf version: %s\n"
msgstr ""
-#: badwolf.c:959
+#: badwolf.c:1035
#, c-format
msgid "Runtime WebKit version: %d.%d.%d\n"
msgstr ""
-#: badwolf.c:436
+#: badwolf.c:437
#, c-format
msgid "TLS Error for %s."
msgstr ""
-#: badwolf.c:439
+#: badwolf.c:440
msgid "Temporarily Add Exception"
msgstr ""
-#: badwolf.c:647
+#: badwolf.c:650
msgid "Toggle javascript"
msgstr ""
-#: badwolf.c:652
+#: badwolf.c:655
msgid "Toggle loading images automatically"
msgstr ""
-#: badwolf.c:107
+#: badwolf.c:108
msgid "Unknown Crash"
msgstr ""
-#: badwolf.c:650
+#: badwolf.c:653
msgid "_IMG"
msgstr ""
-#: badwolf.c:645
+#: badwolf.c:648
msgid "_JS"
msgstr ""
-#: badwolf.c:777
+#: badwolf.c:981
+#, c-format
+msgid "badwolf: content-filter loaded, adding to content-manager…\n"
+msgstr ""
+
+#: badwolf.c:999 badwolf.c:1004
+#, c-format
+msgid "badwolf: failed to compile content-filters.json, err: [%d] %s\n"
+msgstr ""
+
+#: badwolf.c:969 badwolf.c:974
+#, c-format
+msgid "badwolf: failed to load content-filter, err: [%d] %s\n"
+msgstr ""
+
+#: badwolf.c:1059
+#, c-format
+msgid "content-filters directory set to: %s\n"
+msgstr ""
+
+#: badwolf.c:1056
+#, c-format
+msgid "content-filters file set to: %s\n"
+msgstr ""
+
+#: badwolf.c:792
msgid "search in current page"
msgstr ""
-#: badwolf.c:98
+#: badwolf.c:99
msgid "the web process crashed.\n"
msgstr ""
-#: badwolf.c:102
+#: badwolf.c:103
msgid "the web process exceeded the memory limit.\n"
msgstr ""
-#: badwolf.c:106
+#: badwolf.c:107
msgid "the web process terminated for an unknown reason.\n"
msgstr ""
-#: badwolf.c:966
+#: badwolf.c:1042
#, c-format
msgid "webkit-web-extension directory set to: %s\n"
msgstr ""
#. TRANSLATOR Ignore this entry. Done for forcing Unicode in xgettext.
-#: badwolf.c:1042
+#: badwolf.c:1135
msgid "ø"
msgstr ""