From 6ff86dcc44f0033d9ce29c3cc58d6ba05c9878d5 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 17 Dec 2022 20:47:44 +0100 Subject: feat: redirect websites to open-source frontends 1. Adds code for redirecting annoying websites to their open-source frontends, when these exist. 2. Organize this in a "plugins" folder --- plugins/libre_redirect/libre_redirect.c | 69 +++++++++++++++++++++ plugins/libre_redirect/libre_redirect.h | 9 +++ plugins/libre_redirect/str_replace_start.c | 60 ++++++++++++++++++ plugins/libre_redirect/str_replace_start.h | 6 ++ .../str_replace_test/build-example.sh | 11 ++++ plugins/libre_redirect/str_replace_test/example | Bin 0 -> 17040 bytes plugins/libre_redirect/str_replace_test/example.c | 20 ++++++ 7 files changed, 175 insertions(+) create mode 100644 plugins/libre_redirect/libre_redirect.c create mode 100755 plugins/libre_redirect/libre_redirect.h create mode 100755 plugins/libre_redirect/str_replace_start.c create mode 100755 plugins/libre_redirect/str_replace_start.h create mode 100644 plugins/libre_redirect/str_replace_test/build-example.sh create mode 100755 plugins/libre_redirect/str_replace_test/example create mode 100644 plugins/libre_redirect/str_replace_test/example.c diff --git a/plugins/libre_redirect/libre_redirect.c b/plugins/libre_redirect/libre_redirect.c new file mode 100644 index 0000000..b9da2b1 --- /dev/null +++ b/plugins/libre_redirect/libre_redirect.c @@ -0,0 +1,69 @@ +#include "str_replace_start.h" +#include +#include +#include +#define LIBRE_N 19 +#define DEBUG false + +/* Inspired by https://libredirect.github.io/, but in C. */ + +void str_init(char* str, int n){ + for(int i=0; i + +int libre_redirect(const char* uri, char* output){ + int l1 = strlen(uri); + int l2 = strlen(output); + + if((l2 - l1) < LIBRE_N){ + if(DEBUG) printf("Not enough memory\n"); + return 1; // not enough memory. + }else{ + char tmp_uri[l2++]; + char tmp_output[l2++]; + strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0' + strcpy(tmp_output, output); + + char* annoying_sites[] = { + "https://www.youtube.com", + "https://www.reddit.com", + "https://medium.com", + "https://translate.google.com", + "https://forum.effectivealtruism.org", + "https://www.bloomberg.com", + "https://twitter.com" + }; + char* alternatives[] = { + "https://yt.artemislena.eu", + "https://teddit.nunosempere.com", + "https://scribe.rip", + "https://simplytranslate.org/", + "https://ea.greaterwrong.com", + "https://archive.is/https://www.bloomberg.com", + "https://nitter.net" + }; + int n = sizeof(annoying_sites)/sizeof(annoying_sites[0]); + for(int i=0; i +#include +#include +#define DEBUG false + +/* +See also: +* +* https://github.com/irl/la-cucina/blob/master/str_replace.c +*/ + +int str_replace_start(const char* string, const char* target, const char* replacement, char* output){ + + int l1 = strlen(string); + int l2 = strlen(target); + int l3 = strlen(replacement); + int l4 = strlen(output); + if(DEBUG) printf("%d,%d,%d,%d\n", l1, l2, l3, l4); + // if(DEBUG) printf("%s,%s,%s,%s\n", string, target, replacement, output); + + if((l4 < (l1 - l2 + l3)) || l4 < l1 ){ + // Not enough memory in output string. + if(DEBUG) printf("String not long enough.\n"); + return 1; + } + /* else if(l1 < l2){ + // Not even possible that there is a match. + if(DEBUG) printf("Target larger than string.\n"); + strcpy(output, string); + } */ + else { + if(DEBUG) printf("Looking for a match for %s in %s.\n", target, string); + int match = true; + for(int i=0; i +#include + +int main(){ + char uri[] = "https://reddit.com/r/blah"; + + int l = LIBRE_N + strlen(uri) + 1; + char uri_filtered[l]; + str_init(uri_filtered, l); + + if(!libre_redirect(uri, uri_filtered)){ + printf("Filtered uri: %s\n", uri_filtered); + }else{ + printf("Uri: %s\n", uri); + // failure; do something with the original uri. + } +} + + -- cgit v1.2.3 From 98abdb39c4c043fa5175f0dc6e532ec794a5dc18 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 17 Dec 2022 20:54:27 +0100 Subject: feat: integrate libre_redirect code into rose.c --- rose.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/rose.c b/rose.c index 78bdd3b..c71a1b8 100644 --- a/rose.c +++ b/rose.c @@ -9,10 +9,12 @@ * * @author: fenze */ +#include +#include +#include #include "config.h" - -#include +#include "plugins/libre_redirect/libre_redirect.h" #define CACHE \ "base-cache-directory", CACHE_DIR, \ @@ -78,15 +80,43 @@ void load_uri(WebKitWebView *view, const char *uri) } } +void redirect_if_annoying(WebKitWebView *view, const char *uri){ + int l = LIBRE_N + strlen(uri) + 1; + char uri_filtered[l]; + str_init(uri_filtered, l); + + int check = libre_redirect(uri, uri_filtered); + if(check == 2){ + webkit_web_view_load_uri(view, uri_filtered); + } + +} + void load_changed(WebKitWebView *self, WebKitLoadEvent load_event, GtkNotebook *notebook) { - if (load_event == WEBKIT_LOAD_FINISHED) { - gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self), - webkit_web_view_get_title(self)); - gtk_widget_hide(GTK_WIDGET(bar)); - } + switch (load_event) { + /* see */ + case WEBKIT_LOAD_STARTED: + redirect_if_annoying(self, webkit_web_view_get_uri(self)); + break; + case WEBKIT_LOAD_REDIRECTED: + redirect_if_annoying(self, webkit_web_view_get_uri(self)); + break; + case WEBKIT_LOAD_COMMITTED: + redirect_if_annoying(self, webkit_web_view_get_uri(self)); + break; + case WEBKIT_LOAD_FINISHED: + { + const char* title = webkit_web_view_get_title(self); + gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self), + title == NULL ? "—" : title ); + // gtk_widget_hide(GTK_WIDGET(bar)); + break; + } + } } + void notebook_append(GtkNotebook *notebook, const char *uri) { GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window)); -- cgit v1.2.3 From f5d1c0f93dc744c4d548cf99206ec9151c42e73c Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 17 Dec 2022 20:54:47 +0100 Subject: tweak: integrate "plugins" into build.sh --- build.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build.sh b/build.sh index fcb7bea..7805479 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,6 @@ CC=clang SRC=rose.c +REQS=./plugins/*/*.c DEPS=('webkit2gtk-4.0') INCS=`pkg-config --cflags ${DEPS[@]}` @@ -9,4 +10,4 @@ LIBS=`pkg-config --libs ${DEPS[@]}` WYEBAB='-L/usr/lib/wyebrowser/adblock.so' cp -f config.def.h config.h -$CC $INCS $LIBS $SRC $WYEBAB -o rose +$CC $INCS $LIBS $REQS $SRC $WYEBAB -o rose -- cgit v1.2.3