From: Armaan Bhojwani Date: Thu, 10 Jun 2021 03:57:27 +0000 (-0400) Subject: Use content manager to run scripts and user styles X-Git-Tag: v1.0.0~12 X-Git-Url: https://git.armaanb.net/?p=chorizo.git;a=commitdiff_plain;h=c2f41f3f90d7add0588156cbf2ab4cd713a2297d Use content manager to run scripts and user styles Not sure why this wasn't implemented upstream --- diff --git a/man/chorizo-usage.1.scd b/man/chorizo-usage.1.scd index 1f73629..c21ae1d 100644 --- a/man/chorizo-usage.1.scd +++ b/man/chorizo-usage.1.scd @@ -20,9 +20,9 @@ resume downloads. If a file already exists, it won't be touched. Instead, the new file name will have a suffix such as *.1*, *.2*, *.3*, and so on. # USER-SUPPLIED JAVASCRIPT FILES -After a page has been successfully loaded, the directory -_~/.local/share/chorizo/user-scripts_ will be scanned and each file in it ending -with *.js* will be run as a JavaScript file in the context of said page. +When a page is being loaded, the directory _~/.local/share/chorizo/user-scripts_ +will be scanned and each file in it ending with *.js* will be run as a +JavaScript file in the context of said page. *chorizo* comes with the following scripts: @@ -40,6 +40,11 @@ with *.js* will be run as a JavaScript file in the context of said page. Those bundled scripts are automatically installed on *make install*. To use them, though, make sure to link them to the directory mentioned above. +# USER-SUPPLIED CSS FILES +User supplied CSS files will be scanned for from +_~/.local/share/chorizo/user-styles_, and be applied every time a page +loads. The rules in these files override any rules provided by the website. + # WEB EXTENSIONS On startup, WebKit checks _~/.local/share/chorizo/web_extensions_ for any *.so* files. See diff --git a/man/chorizo.1.scd b/man/chorizo.1.scd index 45048ba..5ff49f0 100644 --- a/man/chorizo.1.scd +++ b/man/chorizo.1.scd @@ -52,18 +52,21 @@ XDG variables will be used to construct these paths. Database where cookies are stored. It is kept in the same format as Firefox, so the file can easily be copied over. -~/.local/share/chorizo/user-scripts +*~/.local/share/chorizo/user-scripts* Directory to store user-supplied JavaScript snippets. See *chorizo-usage*(1). -~/.local/share/chorizo/web_extensions +*~/.local/share/chorizo/user-styles* + Directory to store user-supplied CSS snippets. See *chorizo-usage*(1). + +*~/.local/share/chorizo/web_extensions* Sets the directory where WebKit will look for web extensions. See *chorizo-usage*(1). -~/.cache/chorizo - -~/.cache/webkitgtk +*~/.cache/chorizo* +*~/.cache/webkitgtk* + General caches. -~/.local/share/webkitgtk +*~/.local/share/webkitgtk* WebKitGTK will dump its caches and local storage here. It is probably wise to clean those directories regularly or to mount them as *tmpfs*(5). diff --git a/src/browser.c b/src/browser.c index 032bc23..e6d890b 100644 --- a/src/browser.c +++ b/src/browser.c @@ -152,13 +152,63 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, c->focus_new_tab = focus_tab; - if (related_wv == NULL) - c->web_view = webkit_web_view_new(); - else + if (related_wv == NULL) { + WebKitUserContentManager *ucm = webkit_user_content_manager_new(); + WebKitUserScript *wkscript; + WebKitUserStyleSheet *wkstyle; + gchar *path = NULL, *source, *base; + const gchar *entry = NULL; + GDir *dir = NULL; + + base = g_build_filename(g_get_user_data_dir(), __NAME__, "user-scripts", + NULL); + dir = g_dir_open(base, 0, NULL); + if (dir != NULL) { + while ((entry = g_dir_read_name(dir)) != NULL) { + path = g_build_filename(base, entry, NULL); + if (g_str_has_suffix(path, ".js")) { + g_file_get_contents(path, &source, NULL, NULL); + wkscript = webkit_user_script_new( + source, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, + WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START, NULL, + NULL); + webkit_user_content_manager_add_script(ucm, wkscript); + webkit_user_script_unref(wkscript); + } + g_free(path); + g_free(source); + } + g_dir_close(dir); + } + + base = g_build_filename(g_get_user_data_dir(), __NAME__, "user-styles", + NULL); + dir = g_dir_open(base, 0, NULL); + if (dir != NULL) { + while ((entry = g_dir_read_name(dir)) != NULL) { + path = g_build_filename(base, entry, NULL); + if (g_str_has_suffix(path, ".css")) { + g_file_get_contents(path, &source, NULL, NULL); + wkstyle = webkit_user_style_sheet_new( + source, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, + WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL); + webkit_user_content_manager_add_style_sheet(ucm, wkstyle); + webkit_user_style_sheet_unref(wkstyle); + } + g_free(path); + g_free(source); + } + g_dir_close(dir); + } + + g_free(base); + + c->web_view = webkit_web_view_new_with_user_content_manager(ucm); + } else { c->web_view = webkit_web_view_new_with_related_view(related_wv); + } c->settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)); - webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), cfg.global_zoom); webkit_settings_set_enable_javascript(c->settings, @@ -378,8 +428,6 @@ changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data) { * references. */ webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view), grab_feeds, NULL, grab_feeds_finished, c); - - run_user_scripts(WEBKIT_WEB_VIEW(c->web_view)); } gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p); } @@ -1134,38 +1182,6 @@ remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data) { return TRUE; } -void -run_user_scripts(WebKitWebView *web_view) { - gchar *base = NULL, *path = NULL, *contents = NULL; - const gchar *entry = NULL; - GDir *scriptdir = NULL; - - base = - g_build_filename(g_get_user_data_dir(), __NAME__, "user-scripts", NULL); - scriptdir = g_dir_open(base, 0, NULL); - if (scriptdir != NULL) { - while ((entry = g_dir_read_name(scriptdir)) != NULL) { - path = g_build_filename(base, entry, NULL); - char *jscmd = malloc(strlen(path) + 36); - sprintf(jscmd, "console.log(\"Running userscript %s\");", path); - if (g_str_has_suffix(path, ".js")) { - if (g_file_get_contents(path, &contents, NULL, NULL)) { - webkit_web_view_run_javascript(web_view, jscmd, NULL, NULL, - NULL); - webkit_web_view_run_javascript(web_view, contents, NULL, - NULL, NULL); - g_free(contents); - g_free(jscmd); - } - } - g_free(path); - } - g_dir_close(scriptdir); - } - - g_free(base); -} - void show_web_view(WebKitWebView *web_view, gpointer data) { struct Client *c = (struct Client *)data; @@ -1189,7 +1205,7 @@ trust_user_certs(WebKitWebContext *wc) { GTlsCertificate *cert; gchar *basedir, *absfile; const gchar *file; - GDir *dir; + GDir *dir = NULL; basedir = g_build_filename(g_get_user_data_dir(), __NAME__, "certs", NULL); dir = g_dir_open(basedir, 0, NULL);