From d25a6e7e7962c9c028e4f8bf0fc0c93745540f09 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Tue, 13 Dec 2022 21:26:53 +0000 Subject: [PATCH] tweak: try to integrate plugin. --- build.sh | 3 +- .../{ => example}/build-example.sh | 2 +- plugins/libre_redirect/{ => example}/example | Bin .../libre_redirect/{ => example}/example.c | 2 +- plugins/libre_redirect/libre_redirect.c | 18 +++++++- plugins/libre_redirect/str_replace_start.c | 5 ++- rose.c | 42 +++++++++++++++--- 7 files changed, 60 insertions(+), 12 deletions(-) rename plugins/libre_redirect/{ => example}/build-example.sh (69%) rename plugins/libre_redirect/{ => example}/example (100%) rename plugins/libre_redirect/{ => example}/example.c (92%) diff --git a/build.sh b/build.sh index cffe9d5..47824ce 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,6 @@ CC=clang SRC=rose.c +REQS=./plugins/libre_redirect/*.c DEPS=('webkit2gtk-4.0') INCS=`pkg-config --cflags ${DEPS[@]}` @@ -8,4 +9,4 @@ LIBS=`pkg-config --libs ${DEPS[@]}` # Optional adblocking depends on https://github.com/jun7/wyebadblock WYEBAB='-L/usr/lib/wyebrowser/adblock.so' -$CC $INCS $LIBS $SRC $WYEBAB -o rose +$CC $INCS $LIBS $SRC $REQS $WYEBAB -o rose diff --git a/plugins/libre_redirect/build-example.sh b/plugins/libre_redirect/example/build-example.sh similarity index 69% rename from plugins/libre_redirect/build-example.sh rename to plugins/libre_redirect/example/build-example.sh index ac7cf4a..a9f8022 100644 --- a/plugins/libre_redirect/build-example.sh +++ b/plugins/libre_redirect/example/build-example.sh @@ -4,7 +4,7 @@ CC=gcc FLAGS="-std=c99 -Wall -lm" SRC=example.c -REQS="str_replace_start.c libre_redirect.c" +REQS="../str_replace_start.c ../libre_redirect.c" echo -e "\n\n\n" $CC $FLAGS $SRC $REQS -o example diff --git a/plugins/libre_redirect/example b/plugins/libre_redirect/example/example similarity index 100% rename from plugins/libre_redirect/example rename to plugins/libre_redirect/example/example diff --git a/plugins/libre_redirect/example.c b/plugins/libre_redirect/example/example.c similarity index 92% rename from plugins/libre_redirect/example.c rename to plugins/libre_redirect/example/example.c index 8ac9bdf..32285bd 100644 --- a/plugins/libre_redirect/example.c +++ b/plugins/libre_redirect/example/example.c @@ -1,4 +1,4 @@ -#include "libre_redirect.h" +#include "../libre_redirect.h" #include #include diff --git a/plugins/libre_redirect/libre_redirect.c b/plugins/libre_redirect/libre_redirect.c index 59ee2fb..d5a2094 100644 --- a/plugins/libre_redirect/libre_redirect.c +++ b/plugins/libre_redirect/libre_redirect.c @@ -1,6 +1,9 @@ #include "str_replace_start.h" #include +#include +#include #define LIBRE_N 12 +#define DEBUG true /* Inspired by https://libredirect.github.io/, but in C. */ @@ -16,30 +19,43 @@ int libre_redirect(const char* uri, char* output){ 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* sites[] = { "https://youtube.com", "https://reddit.com", + "https://www.reddit.com", "https://medium.com", "https://translate.google.com" }; char* alternatives[] = { "https://yt.artemislena.eu", "https://teddit.nunosempere.com", + "https://teddit.nunosempere.com", "https://scribe.rip", "https://simplytranslate.org/" }; for(int i=1; i<4; i++){ int replace_check = str_replace_start(tmp_uri, sites[i], alternatives[i], output); - if(replace_check){ + if(replace_check == 2){ + if(DEBUG) printf("tmp_uri: %s\n", tmp_uri); + if(DEBUG) printf("output: %s\n", output); + // strcpy(output, tmp_uri); + // break; + return 0; + }else if(replace_check == 1){ + if(DEBUG) printf("replace_check failed\n"); return 1; } strcpy(tmp_uri, output); + str_init(output, l2); } strcpy(output, tmp_uri); } diff --git a/plugins/libre_redirect/str_replace_start.c b/plugins/libre_redirect/str_replace_start.c index eaa61d3..0d96f91 100755 --- a/plugins/libre_redirect/str_replace_start.c +++ b/plugins/libre_redirect/str_replace_start.c @@ -1,7 +1,7 @@ #include #include #include -#define DEBUG false +#define DEBUG true /* See also: @@ -28,7 +28,7 @@ int str_replace_start(const char* string, const char* target, const char* replac strcpy(output, string); } else { - if(DEBUG) printf("Looking for a match.\n"); + if(DEBUG) printf("Looking for a match for %s in %s.\n", target, string); int match = true; for(int i=0; i +#include #define CACHE \ "base-cache-directory", CACHE_DIR, \ @@ -70,7 +71,18 @@ void load_uri(WebKitWebView *view, const char *uri) { if (g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:")) { - webkit_web_view_load_uri(view, uri); + int l = LIBRE_N + strlen(uri) + 1; + char uri_filtered[l]; + str_init(uri_filtered, l); + printf("Uri: %s\n", uri); + int check = libre_redirect(uri, uri_filtered); + if(!check){ + webkit_web_view_load_uri(view, uri_filtered); + }else{ + webkit_web_view_load_uri(view, uri); + } + printf("uri_filtered: %s\n", uri_filtered); + printf("check: %d\n", check); } else { char tmp[strlen(uri) + strlen(SEARCH)]; snprintf(tmp, sizeof(tmp), SEARCH, uri); @@ -80,11 +92,29 @@ void load_uri(WebKitWebView *view, const char *uri) 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), + switch (load_event) { + case WEBKIT_LOAD_STARTED: + /* New load, we have now a provisional URI */ + provisional_uri = webkit_web_view_get_uri (web_view); + /* Here we could start a spinner or update the + * location bar with the provisional URI */ + break; + case WEBKIT_LOAD_REDIRECTED: + redirected_uri = webkit_web_view_get_uri (web_view); + break; + case WEBKIT_LOAD_COMMITTED: + /* The load is being performed. Current URI is + * the final one and it won't change unless a new + * load is requested or a navigation within the + * same page is performed */ + uri = webkit_web_view_get_uri (web_view); + break; + case 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)); - } + gtk_widget_hide(GTK_WIDGET(bar)); + break; + } } void notebook_append(GtkNotebook *notebook, const char *uri)