diff --git a/plugins/style/style.c b/plugins/style/style.c index 3a4769b..da27c40 100644 --- a/plugins/style/style.c +++ b/plugins/style/style.c @@ -1,7 +1,7 @@ #include #include #include -#define STYLE_N 8692 + 1000 +#define STYLE_N 9140 + 1000 void read_style_js(char* string) { diff --git a/plugins/style/style.h b/plugins/style/style.h index a486aee..da4b2d0 100644 --- a/plugins/style/style.h +++ b/plugins/style/style.h @@ -1,5 +1,5 @@ #pragma once -#define STYLE_N 8692 + 1000 +#define STYLE_N 9140 + 1000 void read_style_js(char* string); diff --git a/plugins/style/style.js b/plugins/style/style.js index 3cd16d4..0c9b14a 100644 --- a/plugins/style/style.js +++ b/plugins/style/style.js @@ -302,23 +302,45 @@ 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"); +function filterByKeyword(str) { + // e.g., "keyword" (equivalent to "keyword, p, 1") + // e.g., "keyword, div, 3" + // might not work with level=0, but not sure why + const args = str.split(", "); + let keword = null; + let selector = "p"; /* or "*" for all */ + let level = 1; + if (args.length > 0) { + keyword = args[0].trim(); + } + if (args.length > 1) { + selector = args[1].trim(); + } + if (args.length > 2) { + level = Number(args[2].trim()); + } + console.log(keyword, selector, level); + // Get all elements matching the selector + const elements = document.querySelectorAll(selector); - // 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"; + // Convert NodeList to Array to use array methods + const elementsArray = Array.from(elements); + + // Filter elements containing the keyword + const matchingElements = elementsArray.filter((element) => + element.textContent.toLowerCase().includes(keyword.toLowerCase()), + ); + + // Remove parent of each matching element + matchingElements.forEach((element) => { + let ancestor = element; // Start with the current element + // Loop to climb up the DOM tree according to the level required + for (let i = 0; i < level && ancestor !== null; i++) { + ancestor = ancestor.parentNode; // Move up in the DOM tree + } + if (ancestor) { + ancestor.style.display = "none"; } }); } -console.log("Hello world"); diff --git a/rosenrot4.c b/rosenrot4.c index 9196f85..2f193f6 100644 --- a/rosenrot4.c +++ b/rosenrot4.c @@ -260,7 +260,7 @@ void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook) /* con break; } case _FILTER: { - const char* js_template = "filterDetailsByKeywordHide(\"%s\")"; + const char* js_template = "filterByKeyword(\"%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);