aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbuild.sh4
-rw-r--r--config.def.h8
-rw-r--r--plugins/libre_redirect/libre_redirect.c69
-rwxr-xr-xplugins/libre_redirect/libre_redirect.h9
-rwxr-xr-xplugins/libre_redirect/str_replace_start.c60
-rwxr-xr-xplugins/libre_redirect/str_replace_start.h6
-rw-r--r--plugins/libre_redirect/str_replace_test/build-example.sh11
-rwxr-xr-xplugins/libre_redirect/str_replace_test/examplebin0 -> 17040 bytes
-rw-r--r--plugins/libre_redirect/str_replace_test/example.c20
-rwxr-xr-xrose-mklink2
-rw-r--r--rose.c51
11 files changed, 227 insertions, 13 deletions
diff --git a/build.sh b/build.sh
index cffe9d5..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[@]}`
@@ -8,4 +9,5 @@ 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
+cp -f config.def.h config.h
+$CC $INCS $LIBS $REQS $SRC $WYEBAB -o rose
diff --git a/config.def.h b/config.def.h
index fc5f0e4..8a10f74 100644
--- a/config.def.h
+++ b/config.def.h
@@ -27,7 +27,7 @@
#define KEY(x) GDK_KEY_##x
#define ZOOM 1 /* Starting zoom level */
#define ZOOM_VAL .1 /* Zooming value in zoomin/zoomout functions */
-#define BG_COLOR "#1E1E2E" /* or e.g., "#FEFEFE" if not using dark theme */
+#define BG_COLOR "#1E1E2E" /* or e.g., "#FEFEFE" if not using dark theme */
#define WIDTH 500
#define HEIGHT 400
@@ -47,7 +47,8 @@ typedef enum {
show_searchbar,
show_finder,
finder_next,
- finder_prev
+ finder_prev,
+ newtab
} func;
#define SFT 1 << 0
@@ -74,7 +75,8 @@ static struct {
{ CTRL, KEY(e), show_searchbar },
{ CTRL, KEY(f), show_finder },
{ CTRL, KEY(n), finder_next },
- { CTRL | SFT, KEY(N), finder_prev }
+ { CTRL | SFT, KEY(N), finder_prev },
+ { CTRL, KEY(t), newtab }
};
/* For controls more akin to normal browsers, use:
{
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 <string.h>
+#include <stdio.h>
+#include <stdbool.h>
+#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<n; i++){
+ str[i] = ' ';
+ }
+ str[n] = '\0';
+} // could also use <https://manpages.ubuntu.com/manpages/impish/man3/strinit.3pub.html>
+
+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<n ; i++){
+ int replace_check = str_replace_start(tmp_uri, annoying_sites[i], alternatives[i], output);
+ 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 2;
+ }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);
+ }
+ if(DEBUG) printf("No match found\n\n");
+ return 0;
+
+}
diff --git a/plugins/libre_redirect/libre_redirect.h b/plugins/libre_redirect/libre_redirect.h
new file mode 100755
index 0000000..aee42f4
--- /dev/null
+++ b/plugins/libre_redirect/libre_redirect.h
@@ -0,0 +1,9 @@
+#ifndef LIBRE_REDIRECT
+#define LIBRE_REDIRECT
+
+#define LIBRE_N 19
+
+int libre_redirect(const char* uri, char* uri_filtered);
+void str_init(char* str, int n);
+
+#endif
diff --git a/plugins/libre_redirect/str_replace_start.c b/plugins/libre_redirect/str_replace_start.c
new file mode 100755
index 0000000..74bff65
--- /dev/null
+++ b/plugins/libre_redirect/str_replace_start.c
@@ -0,0 +1,60 @@
+#include <string.h>
+#include <stdbool.h>
+#include <stdio.h>
+#define DEBUG false
+
+/*
+See also:
+* <https://web.archive.org/web/20160201212501/coding.debuntu.org/c-implementing-str_replace-replace-all-occurrences-substring>
+* 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<l2; i++){
+ if(string[i] != target[i]){
+ match = false;
+ break;
+ }
+ }
+ if(match){
+ if(DEBUG) printf("Found match.\n");
+ for(int i=0; i<l3; i++){
+ output[i] = replacement[i];
+ }
+ int counter = l3;
+ for(int i=l2; i<l1; i++){
+ output[counter] = string[i];
+ counter++;
+ }
+ output[counter] = '\0';
+ return 2; // success
+ }
+ else {
+ if(DEBUG) printf("Did not find match.\n");
+ strcpy(output, string);
+ }
+ }
+
+ return 0;
+}
diff --git a/plugins/libre_redirect/str_replace_start.h b/plugins/libre_redirect/str_replace_start.h
new file mode 100755
index 0000000..d9f1235
--- /dev/null
+++ b/plugins/libre_redirect/str_replace_start.h
@@ -0,0 +1,6 @@
+#ifndef STR_REPLACE_H_
+#define STR_REPLACE_H_
+
+int str_replace_start(const char* string, const char* target, const char* replacement, char* output);
+
+#endif
diff --git a/plugins/libre_redirect/str_replace_test/build-example.sh b/plugins/libre_redirect/str_replace_test/build-example.sh
new file mode 100644
index 0000000..a9f8022
--- /dev/null
+++ b/plugins/libre_redirect/str_replace_test/build-example.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+CC=gcc
+
+FLAGS="-std=c99 -Wall -lm"
+
+SRC=example.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/str_replace_test/example b/plugins/libre_redirect/str_replace_test/example
new file mode 100755
index 0000000..4e80ab8
--- /dev/null
+++ b/plugins/libre_redirect/str_replace_test/example
Binary files differ
diff --git a/plugins/libre_redirect/str_replace_test/example.c b/plugins/libre_redirect/str_replace_test/example.c
new file mode 100644
index 0000000..32285bd
--- /dev/null
+++ b/plugins/libre_redirect/str_replace_test/example.c
@@ -0,0 +1,20 @@
+#include "../libre_redirect.h"
+#include <string.h>
+#include <stdio.h>
+
+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.
+ }
+}
+
+
diff --git a/rose-mklink b/rose-mklink
index a658930..2aaba72 100755
--- a/rose-mklink
+++ b/rose-mklink
@@ -7,7 +7,7 @@ test "$1" = "--help" || test -z "$1" && {
}
test -z "$2" || {
- [ -f "/usr/bin/$1" ] && {
+ test -f "/usr/bin/$1" && {
echo "/usr/bin/$1 already exists, remove it first"
exit 1
}
diff --git a/rose.c b/rose.c
index 15baf7d..c71a1b8 100644
--- a/rose.c
+++ b/rose.c
@@ -7,12 +7,14 @@
* sublicense copies of this sotware for their own use.
* This code does not come with any warranty.
*
- * Author: fenze <contact@fenze.dev>
+ * @author: fenze <contact@fenze.dev>
*/
+#include <string.h>
+#include <stdbool.h>
+#include <webkit2/webkit2.h>
#include "config.h"
-
-#include <webkit2/webkit2.h>
+#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 <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html> */
+ 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));
@@ -233,8 +263,13 @@ int handle_key(func id, GtkNotebook *notebook)
webkit_find_controller_search_previous(
webkit_web_view_get_find_controller(notebook_get_webview(notebook)));
break;
+
+ case newtab:
+ notebook_append(notebook, NULL);
+ gtk_notebook_set_show_tabs(notebook, true);
}
+
return 1;
}