remove repeated code.

This commit is contained in:
NunoSempere 2024-02-11 18:04:22 +01:00
parent 59b174e4bb
commit e1fc467795
2 changed files with 27 additions and 34 deletions

BIN
rose

Binary file not shown.

49
rose.c
View File

@ -18,7 +18,14 @@ static struct {
} bar; // top bar. } bar; // top bar.
static int num_tabs = 0; static int num_tabs = 0;
/* */ /* Utils */
WebKitWebView* notebook_get_webview(GtkNotebook* notebook)
{
return WEBKIT_WEB_VIEW(gtk_notebook_get_nth_page(
notebook, gtk_notebook_get_current_page(notebook)));
}
/* Webkit initialization */
WebKitWebView* webview_new() WebKitWebView* webview_new()
{ {
char* style; char* style;
@ -79,16 +86,10 @@ void load_uri(WebKitWebView* view, const char* uri)
} }
} }
WebKitWebView* notebook_get_webview(GtkNotebook* notebook)
{
return WEBKIT_WEB_VIEW(gtk_notebook_get_nth_page(
notebook, gtk_notebook_get_current_page(notebook)));
}
/* Deal with new load or changed load */ /* Deal with new load or changed load */
void redirect_if_annoying(WebKitWebView* view, const char* uri) void redirect_if_annoying(WebKitWebView* view, const char* uri)
{ {
if (LIBRE_REDIRECT_ENABLED) {
int l = LIBRE_N + strlen(uri) + 1; int l = LIBRE_N + strlen(uri) + 1;
char uri_filtered[l]; char uri_filtered[l];
str_init(uri_filtered, l); str_init(uri_filtered, l);
@ -98,6 +99,16 @@ void redirect_if_annoying(WebKitWebView* view, const char* uri)
if (check == 2) { if (check == 2) {
webkit_web_view_load_uri(view, uri_filtered); webkit_web_view_load_uri(view, uri_filtered);
} }
}
}
void set_custom_style(WebKitWebView* view){
if (CUSTOM_STYLE_ENABLED) {
char* style_js = malloc(STYLE_N + 1);
read_style_js(style_js);
webkit_web_view_run_javascript(notebook_get_webview(notebook), style_js,
NULL, NULL, NULL);
free(style_js);
}
} }
void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event, void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
GtkNotebook* notebook) GtkNotebook* notebook)
@ -106,33 +117,15 @@ void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
/* see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html> /* see <https://webkitgtk.org/reference/webkit2gtk/2.5.1/WebKitWebView.html>
*/ */
case WEBKIT_LOAD_STARTED: case WEBKIT_LOAD_STARTED:
if (CUSTOM_STYLE_ENABLED) { set_custom_style(self);
char* style_js = malloc(STYLE_N + 1);
read_style_js(style_js);
webkit_web_view_run_javascript(notebook_get_webview(notebook), style_js,
NULL, NULL, NULL);
free(style_js);
}
if (LIBRE_REDIRECT_ENABLED) {
redirect_if_annoying(self, webkit_web_view_get_uri(self)); redirect_if_annoying(self, webkit_web_view_get_uri(self));
}
break; break;
case WEBKIT_LOAD_REDIRECTED: case WEBKIT_LOAD_REDIRECTED:
if (LIBRE_REDIRECT_ENABLED) {
redirect_if_annoying(self, webkit_web_view_get_uri(self)); redirect_if_annoying(self, webkit_web_view_get_uri(self));
}
break; break;
case WEBKIT_LOAD_COMMITTED: case WEBKIT_LOAD_COMMITTED:
if (LIBRE_REDIRECT_ENABLED) {
redirect_if_annoying(self, webkit_web_view_get_uri(self)); redirect_if_annoying(self, webkit_web_view_get_uri(self));
} set_custom_style(self);
if (CUSTOM_STYLE_ENABLED) {
char* style_js = malloc(STYLE_N + 1);
read_style_js(style_js);
webkit_web_view_run_javascript(notebook_get_webview(notebook), style_js,
NULL, NULL, NULL);
free(style_js);
}
break; break;
case WEBKIT_LOAD_FINISHED: { case WEBKIT_LOAD_FINISHED: {
/* Add gtk tab title */ /* Add gtk tab title */