added url filtering

This commit is contained in:
WanderingPenwing 2024-08-07 20:26:12 +02:00
parent bd970142e1
commit 276fe8e8ed
7 changed files with 3835 additions and 3 deletions

View file

@ -22,11 +22,18 @@ options:
surf: $(OBJ)
$(CC) $(SURFLDFLAGS) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
$(OBJ) $(WOBJ): config.h config.mk
$(OBJ) $(WOBJ): config.h config.mk filters_compiled
config.h:
cp config.def.h $@
filters_compiled: filters
sed -e '/^$$/d' -e 's|\\|\\\\|g' -e 's|$$|",|' -e 's|^|"|' < filters > $@
filters:
@echo creating $@ from filters.def
@cp filters.def $@
$(OBJ): $(SRC)
$(CC) $(SURFCFLAGS) $(CFLAGS) -c $(SRC)
@ -41,7 +48,7 @@ clean:
rm -f $(WLIB) $(WOBJ)
distclean: clean
rm -f config.h surf-$(VERSION).tar.gz
rm -f config.h surf-$(VERSION).tar.gz filters_compiled
dist: distclean
mkdir -p surf-$(VERSION)

View file

@ -19,6 +19,14 @@ static SearchEngine searchengines[] = {
{ "-", "https://searx.ox2.fr/search?q=%s" },
};
/* Regular expressions to match URLs that should not be loaded */
char *filter_patterns[] = {
#include "filters_compiled"
};
/* Define this for verbose filtering */
// #define FILTER_VERBOSE
/* Webkit default features */
/* Highest priority value will be used.
* Default parameters are priority 0

2
filters Normal file
View file

@ -0,0 +1,2 @@
^eviladvertisments\.com$
/favicon\.ico$

3622
filters.def Normal file

File diff suppressed because it is too large Load diff

2
filters_compiled Normal file
View file

@ -0,0 +1,2 @@
"^eviladvertisments\\.com$",
"/favicon\\.ico$",

138
surf-0.6-navhist.diff Normal file
View file

@ -0,0 +1,138 @@
diff --git a/config.def.h b/config.def.h
index a221c86..9840736 100644
--- a/config.def.h
+++ b/config.def.h
@@ -32,6 +32,16 @@ static Bool hidebackground = FALSE;
} \
}
+#define SELNAV { \
+ .v = (char *[]){ "/bin/sh", "-c", \
+ "prop=\"`xprop -id $0 _SURF_HIST" \
+ " | sed -e 's/^.[^\"]*\"//' -e 's/\"$//' -e 's/\\\\\\n/\\n/g'" \
+ " | dmenu -i -l 10`\"" \
+ " && xprop -id $0 -f _SURF_NAV 8s -set _SURF_NAV \"$prop\"", \
+ winid, NULL \
+ } \
+}
+
/* DOWNLOAD(URI, referer) */
#define DOWNLOAD(d, r) { \
.v = (char *[]){ "/bin/sh", "-c", \
@@ -67,6 +77,7 @@ static Key keys[] = {
{ MODKEY, GDK_l, navigate, { .i = +1 } },
{ MODKEY, GDK_h, navigate, { .i = -1 } },
+ { MODKEY|GDK_SHIFT_MASK,GDK_h, selhist, SELNAV },
{ MODKEY, GDK_j, scroll_v, { .i = +1 } },
{ MODKEY, GDK_k, scroll_v, { .i = -1 } },
diff --git a/surf.c b/surf.c
index cebd469..8b6d751 100644
--- a/surf.c
+++ b/surf.c
@@ -32,7 +32,7 @@ char *argv0;
#define COOKIEJAR_TYPE (cookiejar_get_type ())
#define COOKIEJAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
-enum { AtomFind, AtomGo, AtomUri, AtomLast };
+enum { AtomFind, AtomGo, AtomUri, AtomHist, AtomNav, AtomLast };
typedef union Arg Arg;
union Arg {
@@ -137,6 +137,8 @@ static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
Client *c);
static void loaduri(Client *c, const Arg *arg);
static void navigate(Client *c, const Arg *arg);
+static void selhist(Client *c, const Arg *arg);
+static void navhist(Client *c, const Arg *arg);
static Client *newclient(void);
static void newwindow(Client *c, const Arg *arg, gboolean noembed);
static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
@@ -649,6 +651,59 @@ navigate(Client *c, const Arg *arg) {
webkit_web_view_go_back_or_forward(c->view, steps);
}
+static void
+selhist(Client *c, const Arg *arg) {
+ WebKitWebBackForwardList *lst;
+ WebKitWebHistoryItem *cur;
+ gint i;
+ gchar *out;
+ gchar *tmp;
+ gchar *line;
+
+ out = g_strdup("");
+
+ if(!(lst = webkit_web_view_get_back_forward_list(c->view)))
+ return;
+
+ for(i = webkit_web_back_forward_list_get_back_length(lst); i > 0; i--) {
+ if(!(cur = webkit_web_back_forward_list_get_nth_item(lst, -i)))
+ break;
+ line = g_strdup_printf("%d: %s\n", -i,
+ webkit_web_history_item_get_original_uri(cur));
+ tmp = g_strconcat(out, line, NULL);
+ g_free(out);
+ out = tmp;
+ }
+
+ if((cur = webkit_web_back_forward_list_get_nth_item(lst, 0))) {
+ line = g_strdup_printf("%d: %s", 0,
+ webkit_web_history_item_get_original_uri(cur));
+ tmp = g_strconcat(out, line, NULL);
+ g_free(out);
+ out = tmp;
+ }
+
+ for(i = 1; i <= webkit_web_back_forward_list_get_forward_length(lst); i++) {
+ if(!(cur = webkit_web_back_forward_list_get_nth_item(lst, i)))
+ break;
+ line = g_strdup_printf("\n%d: %s", i,
+ webkit_web_history_item_get_original_uri(cur));
+ tmp = g_strconcat(out, line, NULL);
+ g_free(out);
+ out = tmp;
+ }
+
+ setatom(c, AtomHist, out);
+ g_free(out);
+ spawn(c, arg);
+}
+
+static void
+navhist(Client *c, const Arg *arg) {
+ Arg a = { .i = atoi(arg->v) };
+ navigate(c, &a);
+}
+
static Client *
newclient(void) {
Client *c;
@@ -805,6 +860,7 @@ newclient(void) {
setatom(c, AtomFind, "");
setatom(c, AtomUri, "about:blank");
+ setatom(c, AtomHist, "");
if(hidebackground)
webkit_web_view_set_transparent(c->view, TRUE);
@@ -923,6 +979,9 @@ processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
arg.v = getatom(c, AtomGo);
loaduri(c, &arg);
return GDK_FILTER_REMOVE;
+ } else if(ev->atom == atoms[AtomNav]) {
+ arg.v = getatom(c, AtomNav);
+ navhist(c, &arg);
}
}
}
@@ -1004,6 +1063,8 @@ setup(void) {
atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
+ atoms[AtomHist] = XInternAtom(dpy, "_SURF_HIST", False);
+ atoms[AtomNav] = XInternAtom(dpy, "_SURF_NAV", False);
/* dirs and files */
cookiefile = buildpath(cookiefile);

55
surf.c
View file

@ -70,6 +70,8 @@
#define LENGTH(x) (sizeof(x) / sizeof(x[0]))
#define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
regex_t *filter_expressions;
enum { AtomFind, AtomGo, AtomUri, AtomUTF8, AtomLast };
enum {
@ -287,6 +289,10 @@ static void toggleinspector(Client *c, const Arg *a);
static void find(Client *c, const Arg *a);
static void insert(Client *c, const Arg *a);
/* filter url */
static bool filter_init(void);
static bool filter_request(const gchar *uri);
/* Buttons */
static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
@ -744,6 +750,49 @@ die(const char *errstr, ...)
exit(1);
}
static bool
filter_init(void) {
bool errors = false;
char *errorbuf;
errorbuf = malloc(sizeof(char) * BUFSIZ);
filter_expressions = malloc(sizeof(regex_t) * LENGTH(filter_patterns));
for (off_t idx = 0; idx < LENGTH(filter_patterns); idx++) {
char *pat = filter_patterns[idx];
int err = regcomp(&filter_expressions[idx], pat,
REG_EXTENDED | REG_ICASE | REG_NOSUB);
if (err != 0) {
/* regerror always ends messages with 0x00 */
(void) regerror(err, &filter_expressions[idx], errorbuf, BUFSIZ);
fprintf(stderr, "Failed to compile \"%s\": %s\n", pat, errorbuf);
errors = true;
}
}
free(errorbuf);
return !errors;
}
static bool
filter_request(const gchar *uri) {
if (!strcmp(uri, "about:blank"))
return false;
for (off_t idx = 0; idx < LENGTH(filter_patterns); idx++) {
if (regexec(&filter_expressions[idx], uri, 0, NULL, 0) == REG_NOMATCH) {
continue;
}
#ifdef FILTER_VERBOSE
fprintf(stderr, "filtering \"%s\"\n", uri);
#endif
return true;
}
#ifdef FILTER_VERBOSE
fprintf(stderr, "not filtering \"%s\"\n", uri);
#endif
return false;
}
void
usage(void)
{
@ -849,6 +898,10 @@ setup(void)
uriparams[i].config[j] = defconfig[j];
}
}
if (!filter_init()) {
die("Failed to compile one or more filter expressions\n");
}
}
void
@ -2225,7 +2278,7 @@ decideresource(WebKitPolicyDecision *d, Client *c)
webkit_response_policy_decision_get_response(r);
const gchar *uri = webkit_uri_response_get_uri(res);
if (g_str_has_suffix(uri, "/favicon.ico")) {
if (filter_request(uri)) {
webkit_policy_decision_ignore(d);
return;
}