generalize filterByKeyword function
This commit is contained in:
parent
82584d6a59
commit
fdf688cba2
|
@ -1,7 +1,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#define STYLE_N 8692 + 1000
|
#define STYLE_N 9140 + 1000
|
||||||
|
|
||||||
void read_style_js(char* string)
|
void read_style_js(char* string)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define STYLE_N 8692 + 1000
|
#define STYLE_N 9140 + 1000
|
||||||
|
|
||||||
void read_style_js(char* string);
|
void read_style_js(char* string);
|
||||||
|
|
|
@ -302,23 +302,45 @@ if (document.domain == "twitter.com" || document.domain == "x.com") {
|
||||||
// document.body.style.visibility = "visible";
|
// document.body.style.visibility = "visible";
|
||||||
|
|
||||||
// Add some code to filter out articles for Sentinel
|
// 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
|
function filterByKeyword(str) {
|
||||||
detailsElements.forEach((element) => {
|
// e.g., "keyword" (equivalent to "keyword, p, 1")
|
||||||
// Find the <p> element inside the <details> that follows the first <h3> (assumed to be the summary here)
|
// e.g., "keyword, div, 3"
|
||||||
const summaryElement = element.querySelector("h3 + p");
|
// 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)
|
// Convert NodeList to Array to use array methods
|
||||||
if (
|
const elementsArray = Array.from(elements);
|
||||||
summaryElement &&
|
|
||||||
summaryElement.textContent.toLowerCase().includes(keyword.toLowerCase())
|
// Filter elements containing the keyword
|
||||||
) {
|
const matchingElements = elementsArray.filter((element) =>
|
||||||
// If the keyword is found, hide this <details> element
|
element.textContent.toLowerCase().includes(keyword.toLowerCase()),
|
||||||
element.style.display = "none";
|
);
|
||||||
|
|
||||||
|
// 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");
|
|
||||||
|
|
|
@ -260,7 +260,7 @@ void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook) /* con
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case _FILTER: {
|
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];
|
char js_command[strlen(js_template) + strlen(bar_line_text) + 2];
|
||||||
snprintf(js_command, sizeof(js_command) + 1, js_template, bar_line_text);
|
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);
|
webkit_web_view_evaluate_javascript(view, js_command, -1, NULL, "rosenrot-filter-plugin", NULL, NULL, NULL);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user