integrate adblocker into main process.

This commit is contained in:
NunoSempere 2025-11-28 12:48:00 -03:00
parent 877d4b7dc9
commit 27840e3521
3 changed files with 49 additions and 3 deletions

View File

@ -21,7 +21,10 @@ CONFIG=src/config.h
# Plugins
include src/plugins/plugins.mk
# PLUGINS=./src/plugins/stand_in/stand_in.c
ADBLOCK='-L/usr/lib/wyebrowser/adblock.so' # optional adblocking; depends on https://github.com/jun7/wyebadblock
# Adblock extension (built separately as a shared library)
ADBLOCK_EXT_DIR=src/plugins/adblock
ADBLOCK_EXT=$(ADBLOCK_EXT_DIR)/librosenrot-adblock.so
## Formatter
STYLE_BLUEPRINT="{BasedOnStyle: webkit, AllowShortIfStatementsOnASingleLine: true, IndentCaseLabels: true, AllowShortEnumsOnASingleLine: true}"
@ -34,7 +37,13 @@ USER_CACHE_DIR=/home/`whoami`/.cache/rosenrot
RUNTIME_FILES_DIR=/opt/rosenrot/
build: $(SRC) $(PLUGINS) $(CONFIG) constants user_cache
$(CC) $(STD) $(WARNINGS) $(SECURITY) $(DEPRECATION_FLAGS) $(OPTIMIZED_MORE) $(DEBUG) $(INCS) $(PLUGINS) $(SRC) -o out/rosenrot $(LIBS) $(ADBLOCK)
$(CC) $(STD) $(WARNINGS) $(SECURITY) $(DEPRECATION_FLAGS) $(OPTIMIZED_MORE) $(DEBUG) $(INCS) $(PLUGINS) $(SRC) -o out/rosenrot $(LIBS)
build_adblock:
@echo "# Building adblock extension"
cd $(ADBLOCK_EXT_DIR) && $(MAKE)
build_all: build build_adblock
@echo
format: $(SRC) $(PLUGINS)
@ -75,6 +84,7 @@ user_cache:
runtime_files:
@echo
sudo mkdir -p /opt/rosenrot/
sudo mkdir -p /opt/rosenrot/extensions/
sudo cp src/styles-gtk/style-gtk4.css /opt/rosenrot/
sudo touch /opt/rosenrot/uris.txt
sudo chmod a+rw /opt/rosenrot/uris.txt
@ -82,6 +92,13 @@ runtime_files:
sudo cp src/plugins/style/style.js /opt/rosenrot/
sudo cp src/plugins/readability/readability.js /opt/rosenrot/
install_adblock: build_adblock
@echo "# Installing adblock extension"
cd $(ADBLOCK_EXT_DIR) && sudo $(MAKE) install
@echo "# Don't forget to download easylist.txt:"
@echo "# curl -o /tmp/easylist.txt https://easylist.to/easylist/easylist.txt"
@echo "# sudo mv /tmp/easylist.txt /opt/rosenrot/easylist.txt"
# More misc recipes
lint:

View File

@ -21,6 +21,9 @@
#define LIBRE_REDIRECT_ENABLED true
#define READABILITY_ENABLED true
#define CUSTOM_USER_AGENT false
#define ADBLOCK_ENABLED false
#define ADBLOCK_EXTENSIONS_DIR "/opt/rosenrot/extensions"
#define ADBLOCK_FILTERLIST_PATH "/opt/rosenrot/easylist.txt"
/*
To disable plugins:
1. set their corresponding variable to false

View File

@ -133,6 +133,29 @@ void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
}
/* New tabs */
/* Shared web context for all views (needed for web extensions) */
static WebKitWebContext* shared_web_context = NULL;
static WebKitWebContext* get_shared_web_context()
{
if (shared_web_context == NULL) {
shared_web_context = webkit_web_context_new();
/* Configure web extensions for adblock if enabled */
if (ADBLOCK_ENABLED) {
webkit_web_context_set_web_process_extensions_directory(shared_web_context, ADBLOCK_EXTENSIONS_DIR);
/* Pass configuration to the extension */
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&builder, "{sv}", "enabled", g_variant_new_boolean(TRUE));
webkit_web_context_set_web_process_extensions_initialization_user_data(
shared_web_context, g_variant_builder_end(&builder));
}
}
return shared_web_context;
}
WebKitWebView* create_new_webview()
{
WebKitSettings* settings = webkit_settings_new_with_settings(WEBKIT_DEFAULT_SETTINGS, NULL);
@ -149,7 +172,10 @@ WebKitWebView* create_new_webview()
webkit_cookie_manager_set_persistent_storage(cookiemanager, DATA_DIR "/cookies.sqlite", WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
webkit_cookie_manager_set_accept_policy(cookiemanager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS);
WebKitWebView* view = g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "network-session", network_session, "user-content-manager", contentmanager, NULL);
/* Get shared web context (configured with web extensions if adblock enabled) */
WebKitWebContext* context = get_shared_web_context();
WebKitWebView* view = g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "network-session", network_session, "user-content-manager", contentmanager, "web-context", context, NULL);
NULLCHECK(view);
GtkEventController* event_controller = gtk_event_controller_key_new();