feat: working version of libredirect.

This commit is contained in:
NunoSempere 2022-12-13 21:59:04 +00:00
parent d25a6e7e79
commit 21e5800bbe
2 changed files with 32 additions and 17 deletions

View File

@ -49,7 +49,7 @@ int libre_redirect(const char* uri, char* output){
if(DEBUG) printf("output: %s\n", output); if(DEBUG) printf("output: %s\n", output);
// strcpy(output, tmp_uri); // strcpy(output, tmp_uri);
// break; // break;
return 0; return 2;
}else if(replace_check == 1){ }else if(replace_check == 1){
if(DEBUG) printf("replace_check failed\n"); if(DEBUG) printf("replace_check failed\n");
return 1; return 1;

43
rose.c
View File

@ -26,6 +26,7 @@
"local-storage-directory", CACHE_DIR, \ "local-storage-directory", CACHE_DIR, \
"offline-application-cache-directory", CACHE_DIR, \ "offline-application-cache-directory", CACHE_DIR, \
"service-worker-registrations-directory", CACHE_DIR "service-worker-registrations-directory", CACHE_DIR
#define DEBUG false
enum { _SEARCH, _FIND }; enum { _SEARCH, _FIND };
@ -71,43 +72,57 @@ void load_uri(WebKitWebView *view, const char *uri)
{ {
if (g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || 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:")) { g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:")) {
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); webkit_web_view_load_uri(view, uri);
}
printf("uri_filtered: %s\n", uri_filtered);
printf("check: %d\n", check);
} else { } else {
// webkit_web_view_load_uri(view, uri);
char tmp[strlen(uri) + strlen(SEARCH)]; char tmp[strlen(uri) + strlen(SEARCH)];
snprintf(tmp, sizeof(tmp), SEARCH, uri); snprintf(tmp, sizeof(tmp), SEARCH, uri);
webkit_web_view_load_uri(view, tmp); webkit_web_view_load_uri(view, tmp);
} }
} }
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);
printf("Uri: %s\n", uri);
int check = libre_redirect(uri, uri_filtered);
if(check == 2){
webkit_web_view_load_uri(view, uri_filtered);
}
printf("uri_filtered: %s\n", uri_filtered);
printf("check: %d\n", check);
}
void load_changed(WebKitWebView *self, WebKitLoadEvent load_event, GtkNotebook *notebook) void load_changed(WebKitWebView *self, WebKitLoadEvent load_event, GtkNotebook *notebook)
{ {
switch (load_event) { switch (load_event) {
/* see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html> */
case WEBKIT_LOAD_STARTED: case WEBKIT_LOAD_STARTED:
/* New load, we have now a provisional URI */ /* New load, we have now a provisional URI */
provisional_uri = webkit_web_view_get_uri (web_view); //const char* provisional_uri = webkit_web_view_get_uri(self);
// printf("%s", provisional_uri);
redirect_if_annoying(self, webkit_web_view_get_uri(self));
printf("Load started with uri: %s\n", webkit_web_view_get_uri (self));
//provisional_uri);
/* Here we could start a spinner or update the /* Here we could start a spinner or update the
* location bar with the provisional URI */ * location bar with the provisional URI */
break; break;
case WEBKIT_LOAD_REDIRECTED: case WEBKIT_LOAD_REDIRECTED:
redirected_uri = webkit_web_view_get_uri (web_view); redirect_if_annoying(self, webkit_web_view_get_uri(self));
printf("Load redirected to uri: %s\n", webkit_web_view_get_uri (self));
break; break;
case WEBKIT_LOAD_COMMITTED: case WEBKIT_LOAD_COMMITTED:
/* The load is being performed. Current URI is /* The load is being performed. Current URI is
* the final one and it won't change unless a new * the final one and it won't change unless a new
* load is requested or a navigation within the * load is requested or a navigation within the
* same page is performed */ * same page is performed */
uri = webkit_web_view_get_uri (web_view); redirect_if_annoying(self, webkit_web_view_get_uri(self));
printf("Load committed with: %s\n", webkit_web_view_get_uri (self));
//printf("%s", uri);
break; break;
case WEBKIT_LOAD_FINISHED: case WEBKIT_LOAD_FINISHED:
gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self), gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self),