From 5e3cab2a6300275d02f98db7b1c7ac210b8f0626 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Thu, 19 Sep 2024 15:16:15 +0200 Subject: [PATCH] add custom filter command (Ctrl+F) --- config.h | 2 ++ plugins/style/style.c | 2 +- plugins/style/style.h | 2 +- plugins/style/style.js | 22 +++++++++++++++++++ rosenrot4.c | 49 +++++++++++++++++++++++++++++++++--------- 5 files changed, 65 insertions(+), 12 deletions(-) diff --git a/config.h b/config.h index af13901..5f2c879 100644 --- a/config.h +++ b/config.h @@ -92,6 +92,7 @@ typedef enum { show_finder, finder_next, finder_prev, + filter, halve_window, rebig_window, @@ -134,6 +135,7 @@ static struct { { CTRL, KEY(f), show_finder }, { CTRL, KEY(n), finder_next }, { CTRL, KEY(N), finder_prev }, + { CTRL | SFT, KEY(F), filter }, { CTRL, KEY(Up), halve_window }, { CTRL, KEY(Down), rebig_window }, { CTRL, KEY(p), prettify }, diff --git a/plugins/style/style.c b/plugins/style/style.c index adc2c7c..3d92cbc 100644 --- a/plugins/style/style.c +++ b/plugins/style/style.c @@ -1,7 +1,7 @@ #include #include #include -#define STYLE_N 8049 + 1000 +#define STYLE_N 8675 + 1000 void read_style_js(char* string) { diff --git a/plugins/style/style.h b/plugins/style/style.h index c0f3d4e..11fa58f 100644 --- a/plugins/style/style.h +++ b/plugins/style/style.h @@ -1,5 +1,5 @@ #pragma once -#define STYLE_N 8049 + 1000 +#define STYLE_N 8675 + 1000 void read_style_js(char* string); diff --git a/plugins/style/style.js b/plugins/style/style.js index e80b953..52f1939 100644 --- a/plugins/style/style.js +++ b/plugins/style/style.js @@ -299,3 +299,25 @@ if (document.domain == "twitter.com" || document.domain == "x.com") { } // document.body.style.visibility = "visible"; + +// Add some code to filter out articles for Sentinel +function filterDetailsByKeywordHide(keyword) { + // Get all the
elements on the page + const detailsElements = document.querySelectorAll("details"); + + // Loop through each
element + detailsElements.forEach((element) => { + // Find the

element inside the

that follows the first

(assumed to be the summary here) + const summaryElement = element.querySelector("h3 + p"); + + // Check if the summary text includes the keyword (case-insensitive match) + if ( + summaryElement && + summaryElement.textContent.toLowerCase().includes(keyword.toLowerCase()) + ) { + // If the keyword is found, hide this
element + element.style.display = "none"; + } + }); +} +console.log("Hello world"); diff --git a/rosenrot4.c b/rosenrot4.c index ef46a16..9196f85 100644 --- a/rosenrot4.c +++ b/rosenrot4.c @@ -9,7 +9,7 @@ /* Global variables */ static GtkNotebook* notebook; static GtkWindow* window; -typedef enum { _SEARCH, _FIND, _HIDDEN } Bar_entry_mode; +typedef enum { _SEARCH, _FIND, _FILTER, _HIDDEN } Bar_entry_mode; static struct { GtkHeaderBar* widget; GtkEntry* line; @@ -227,6 +227,13 @@ void toggle_bar(GtkNotebook* notebook, Bar_entry_mode mode) gtk_window_set_focus(window, GTK_WIDGET(bar.line)); break; } + case _FILTER: { + gtk_entry_set_placeholder_text(bar.line, "Filter"); + gtk_entry_buffer_set_text(bar.line_text, "", strlen("")); + gtk_widget_set_visible(GTK_WIDGET(bar.widget), 1); + gtk_window_set_focus(window, GTK_WIDGET(bar.line)); + break; + } case _HIDDEN: gtk_widget_set_visible(GTK_WIDGET(bar.widget), 0); } @@ -236,16 +243,35 @@ void toggle_bar(GtkNotebook* notebook, Bar_entry_mode mode) void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook) /* consider passing notebook as the data here? */ { WebKitWebView* view = notebook_get_webview(notebook); - if (bar.entry_mode == _SEARCH) - load_uri(view, gtk_entry_buffer_get_text(bar.line_text)); - else if (bar.entry_mode == _FIND) - webkit_find_controller_search( - webkit_web_view_get_find_controller(view), - gtk_entry_buffer_get_text(bar.line_text), - WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | WEBKIT_FIND_OPTIONS_WRAP_AROUND, - G_MAXUINT); + const char* bar_line_text = gtk_entry_buffer_get_text(bar.line_text); + switch (bar.entry_mode) { + case _SEARCH: { + load_uri(view, bar_line_text); + gtk_widget_set_visible(GTK_WIDGET(bar.widget), 0); + break; + } + case _FIND: { + webkit_find_controller_search( + webkit_web_view_get_find_controller(view), + bar_line_text, + WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | WEBKIT_FIND_OPTIONS_WRAP_AROUND, + G_MAXUINT); + gtk_widget_set_visible(GTK_WIDGET(bar.widget), 0); + break; + } + case _FILTER: { + const char* js_template = "filterDetailsByKeywordHide(\"%s\")"; + char js_command[strlen(js_template) + strlen(bar_line_text) + 2]; + snprintf(js_command, sizeof(js_command) + 1, js_template, bar_line_text); + webkit_web_view_evaluate_javascript(view, js_command, -1, NULL, "rosenrot-filter-plugin", NULL, NULL, NULL); + gtk_widget_set_visible(GTK_WIDGET(bar.widget), 0); + + break; + } + case _HIDDEN: + // no op + } - gtk_widget_set_visible(GTK_WIDGET(bar.widget), 0); } /* Shortcuts */ @@ -332,6 +358,9 @@ int handle_shortcut(func id) case show_finder: toggle_bar(notebook, _FIND); break; + case filter: + toggle_bar(notebook, _FILTER); + break; case finder_next: webkit_find_controller_search_next(webkit_web_view_get_find_controller(view));