generalize filterByKeyword function

This commit is contained in:
NunoSempere 2024-09-19 16:45:54 +02:00
parent 82584d6a59
commit fdf688cba2
4 changed files with 40 additions and 18 deletions

View File

@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STYLE_N 8692 + 1000
#define STYLE_N 9140 + 1000
void read_style_js(char* string)
{

View File

@ -1,5 +1,5 @@
#pragma once
#define STYLE_N 8692 + 1000
#define STYLE_N 9140 + 1000
void read_style_js(char* string);

View File

@ -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 <details> elements on the page
const detailsElements = document.querySelectorAll("details");
// Loop through each <details> element
detailsElements.forEach((element) => {
// Find the <p> element inside the <details> that follows the first <h3> (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 <details> 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");

View File

@ -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);