Summary Files Commits Branches @commit @diff @stats
path: root / badwolf.c
diff options
context:
space:
mode:
Commit5547d3a708e75a62553b19c81e1afc66ef668a5c (patch)
Author
 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
2019-03-15 10:26:31 +0100
Committer
 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
2021-04-09 06:39:55 +0200
Files5824544ca6dde96a04a186e74679ea44717c1e5d /badwolf.c
goodwolf-5547d3a708e75a62553b19c81e1afc66ef668a5c.tar
goodwolf-5547d3a708e75a62553b19c81e1afc66ef668a5c.tar.gz
goodwolf-5547d3a708e75a62553b19c81e1afc66ef668a5c.zip
Parent01237f5229147431a26c86675657e2fe588928fb (diff)
Handle WebKit content-filter
1 files changed, 85 insertions, 2 deletions
-rw-r--r--badwolf.c87
diff --git a/badwolf.c b/badwolf.c
index 23c444c..ec2a691 100644
--- 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);