commit: 7f4b6c84006c4af9d34319f5f29d890935580370
parent 17fdbc9f19c12095a88e4d64c47f35e62ebb0f86
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 16 Mar 2023 17:37:23 +0100
Add UserScripts support
Diffstat:
9 files changed, 167 insertions(+), 22 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,4 +1,4 @@
-# SPDX-FileCopyrightText: 2019-2022 Badwolf Authors <https://hacktivis.me/projects/badwolf>
+# SPDX-FileCopyrightText: 2019-2023 Badwolf Authors <https://hacktivis.me/projects/badwolf>
 # SPDX-License-Identifier: BSD-3-Clause
 
 # POSIX-ish Makefile with extensions common to *BSD and GNU such as:
@@ -8,8 +8,8 @@
 
 include config.mk
 
-SRCS  = bookmarks.c fmt.c fmt_test.c uri.c uri_test.c keybindings.c downloads.c badwolf.c
-OBJS  = bookmarks.o fmt.o uri.o keybindings.o downloads.o badwolf.o
+SRCS  = bookmarks.c userscripts.c fmt.c fmt_test.c uri.c uri_test.c keybindings.c downloads.c badwolf.c
+OBJS  = bookmarks.o userscripts.o fmt.o uri.o keybindings.o downloads.o badwolf.o
 OBJS_test = fmt_test.o uri_test.o bookmarks_test.o
 EXE   = badwolf
 EXE_test = fmt_test uri_test bookmarks_test
diff --git a/badwolf.1 b/badwolf.1
@@ -1,5 +1,5 @@
 .\" BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
-.\" Copyright © 2019-2022 Badwolf Authors <https://hacktivis.me/projects/badwolf>
+.\" Copyright © 2019-2023 Badwolf Authors <https://hacktivis.me/projects/badwolf>
 .\" SPDX-License-Identifier: BSD-3-Clause
 .Dd 2022-07-13
 .Dt BADWOLF 1
@@ -147,6 +147,12 @@ For testing your styles I would recommend using the
 environment variable on launching
 .Nm
 and going to the CSS tab.
+.It Pa ${XDG_DATA_HOME:-$HOME/.local/share}/badwolf/scripts/
+Directory containing JS userscripts ran on page loads, useful to override
+website behaviors or add missing features to websites.
+.Pp
+The files need to end with
+.Ic .js
 .El
 .Sh AUTHORS
 .An Haelwenn (lanodan) Monnier Aq Mt contact+badwolf@hacktivis.me
diff --git a/badwolf.c b/badwolf.c
@@ -1,5 +1,5 @@
 // BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
-// SPDX-FileCopyrightText: 2019-2022 Badwolf Authors <https://hacktivis.me/projects/badwolf>
+// SPDX-FileCopyrightText: 2019-2023 Badwolf Authors <https://hacktivis.me/projects/badwolf>
 // SPDX-License-Identifier: BSD-3-Clause
 
 #include "badwolf.h"
@@ -10,6 +10,7 @@
 #include "fmt.h"
 #include "keybindings.h"
 #include "uri.h"
+#include "userscripts.h"
 
 #include <assert.h>
 #include <glib/gi18n.h>   /* _() and other internationalization/localization helpers */
@@ -333,7 +334,7 @@ WebViewCb_decide_policy(WebKitWebView *UNUSED(web_view),
 	switch(decision_type)
 	{
 	case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
-		n = WEBKIT_NAVIGATION_POLICY_DECISION(decision);
+		n                 = WEBKIT_NAVIGATION_POLICY_DECISION(decision);
 		navigation_action = webkit_navigation_policy_decision_get_navigation_action(n);
 
 		if(GDK_CONTROL_MASK == webkit_navigation_action_get_modifiers(navigation_action) ||
@@ -1076,6 +1077,8 @@ main(int argc, char *argv[])
 	window->downloads_tab   = badwolf_downloads_tab_new();
 	window->content_manager = webkit_user_content_manager_new();
 
+	load_userscripts(window->content_manager);
+
 	gchar *contentFilterPath =
 	    g_build_filename(g_get_user_config_dir(), g_get_prgname(), "content-filters.json", NULL);
 	GFile *contentFilterFile = g_file_new_for_path(contentFilterPath);
diff --git a/badwolf.fr.1 b/badwolf.fr.1
@@ -1,5 +1,5 @@
 .\" BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
-.\" Copyright © 2019-2022 Badwolf Authors <https://hacktivis.me/projects/badwolf>
+.\" Copyright © 2019-2023 Badwolf Authors <https://hacktivis.me/projects/badwolf>
 .\" SPDX-License-Identifier: BSD-3-Clause
 .\"*******************************************************************
 .\"
@@ -170,6 +170,12 @@ pour la liste des propriétés disponibles.
 Pour tester les styles je recommende d'utiliser la variable d'environemnt
 .Ev GTK_DEBUG=interactive
 et d'aller à l'onglet CSS.
+.It Pa ${XDG_DATA_HOME:-$HOME/.local/share}/badwolf/scripts/
+Dossier contenant les scripts-utilisateurs (userscripts) lancé à chaque
+chargement de page, utile pour changer le comportement des sites web.
+.Pp
+Les fichiers doivent se terminer en
+.Ic .js
 .El
 .Sh AUTHORS
 .An Haelwenn (lanodan) Monnier Aq Mt contact+badwolf@hacktivis.me
diff --git a/po/fr_man.po b/po/fr_man.po
@@ -1,4 +1,4 @@
-# SPDX-FileCopyrightText: 2019-2022 Badwolf Authors <https://hacktivis.me/projects/badwolf>
+# SPDX-FileCopyrightText: 2019-2023 Badwolf Authors <https://hacktivis.me/projects/badwolf>
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #, fuzzy
@@ -6,7 +6,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: contact+badwolf-msgid@hacktivis.me\n"
-"POT-Creation-Date: 2023-02-05 08:08+0100\n"
+"POT-Creation-Date: 2023-03-17 04:55+0100\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -552,29 +552,49 @@ msgstr ""
 "Pour tester les styles je recommende d'utiliser la variable d'environemnt E<."
 "Ev GTK_DEBUG=interactive> et d'aller à l'onglet CSS."
 
+#. type: It
+#: badwolf.1:150
+#, no-wrap
+msgid "Pa ${XDG_DATA_HOME:-$HOME/.local/share}/badwolf/scripts/"
+msgstr "Pa ${XDG_DATA_HOME:-$HOME/.local/share}/badwolf/scripts/"
+
+#. type: Plain text
+#: badwolf.1:153
+msgid ""
+"Directory containing JS userscripts ran on page loads, useful to override "
+"website behaviors or add missing features to websites."
+msgstr ""
+"Dossier contenant les scripts-utilisateurs (userscripts) lancé à chaque "
+"chargement de page, utile pour changer le comportement des sites web."
+
+#. type: Plain text
+#: badwolf.1:156
+msgid "The files need to end with E<.Ic .js>"
+msgstr "Les fichiers doivent se terminer en E<.Ic .js>"
+
 #. type: Sh
-#: badwolf.1:151
+#: badwolf.1:157
 #, no-wrap
 msgid "AUTHORS"
 msgstr "AUTHORS"
 
 #. type: Plain text
-#: badwolf.1:153
+#: badwolf.1:159
 msgid "E<.An Haelwenn (lanodan) Monnier Aq Mt contact+badwolf@hacktivis.me>"
 msgstr "E<.An Haelwenn (lanodan) Monnier Aq Mt contact+badwolf@hacktivis.me>"
 
 #. type: Sh
-#: badwolf.1:153
+#: badwolf.1:159
 #, no-wrap
 msgid "BUGS"
 msgstr "BUGS"
 
 #. type: Plain text
-#: badwolf.1:155
+#: badwolf.1:161
 msgid "You can submit contributions or tickets to"
 msgstr "Vous pouvez soumettre des contributions ou des tickets à"
 
 #. type: Plain text
-#: badwolf.1:158
+#: badwolf.1:164
 msgid "with E<.Xr git-send-email 1> for patches."
 msgstr "avec E<.Xr git-send-email 1> pour les modifications."
diff --git a/po/manpage.pot b/po/manpage.pot
@@ -6,7 +6,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: Badwolf 1.2.0+geb77866.develop\n"
 "Report-Msgid-Bugs-To: contact+badwolf-msgid@hacktivis.me\n"
-"POT-Creation-Date: 2023-02-05 08:08+0100\n"
+"POT-Creation-Date: 2023-03-17 04:55+0100\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -496,29 +496,47 @@ msgid ""
 "the CSS tab."
 msgstr ""
 
+#. type: It
+#: badwolf.1:150
+#, no-wrap
+msgid "Pa ${XDG_DATA_HOME:-$HOME/.local/share}/badwolf/scripts/"
+msgstr ""
+
+#. type: Plain text
+#: badwolf.1:153
+msgid ""
+"Directory containing JS userscripts ran on page loads, useful to override "
+"website behaviors or add missing features to websites."
+msgstr ""
+
+#. type: Plain text
+#: badwolf.1:156
+msgid "The files need to end with E<.Ic .js>"
+msgstr ""
+
 #. type: Sh
-#: badwolf.1:151
+#: badwolf.1:157
 #, no-wrap
 msgid "AUTHORS"
 msgstr ""
 
 #. type: Plain text
-#: badwolf.1:153
+#: badwolf.1:159
 msgid "E<.An Haelwenn (lanodan) Monnier Aq Mt contact+badwolf@hacktivis.me>"
 msgstr ""
 
 #. type: Sh
-#: badwolf.1:153
+#: badwolf.1:159
 #, no-wrap
 msgid "BUGS"
 msgstr ""
 
 #. type: Plain text
-#: badwolf.1:155
+#: badwolf.1:161
 msgid "You can submit contributions or tickets to"
 msgstr ""
 
 #. type: Plain text
-#: badwolf.1:158
+#: badwolf.1:164
 msgid "with E<.Xr git-send-email 1> for patches."
 msgstr ""
diff --git a/po/messages.pot b/po/messages.pot
@@ -4,9 +4,9 @@
 #, fuzzy
 msgid ""
 msgstr ""
-"Project-Id-Version: Badwolf 1.2.0+g5595652e.develop\n"
+"Project-Id-Version: Badwolf 1.2.0+geb77866.develop\n"
 "Report-Msgid-Bugs-To: contact+badwolf-msgid@hacktivis.me\n"
-"POT-Creation-Date: 2022-08-24 15:12+0200\n"
+"POT-Creation-Date: 2023-03-17 03:08+0100\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -145,6 +145,30 @@ msgid "_JS"
 msgstr ""
 
 #, c-format
+msgid "badwolf: Checking for userscripts matching %s\n"
+msgstr ""
+
+#, c-format
+msgid "badwolf: Failed to load userscripts: Out of Memory\n"
+msgstr ""
+
+#, c-format
+msgid "badwolf: Failed to load userscripts: Read Error\n"
+msgstr ""
+
+#, c-format
+msgid "badwolf: Notice: Found %zd userscripts\n"
+msgstr ""
+
+#, c-format
+msgid "badwolf: Notice: No userscripts found\n"
+msgstr ""
+
+#, c-format
+msgid "badwolf: Notice: Userscripts loaded\n"
+msgstr ""
+
+#, c-format
 msgid "badwolf: content-filter loaded, adding to content-manager…\n"
 msgstr ""
 
diff --git a/userscripts.c b/userscripts.c
@@ -0,0 +1,61 @@
+// BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
+// SPDX-FileCopyrightText: 2019-2023 Badwolf Authors <https://hacktivis.me/projects/badwolf>
+// SPDX-License-Identifier: BSD-3-Clause
+
+#include "userscripts.h"
+
+#include "badwolf.h"
+#include "config.h"
+
+#include <assert.h>
+#include <glib/gi18n.h> /* _() and other internationalization/localization helpers */
+#include <glob.h>
+
+void
+load_userscripts(WebKitUserContentManager *content_manager)
+{
+	glob_t scripts_path_glob;
+	gchar *scripts_path = g_build_filename(g_get_user_data_dir(), "badwolf", "scripts", "*.js", NULL);
+
+	fprintf(stderr, _("badwolf: Checking for userscripts matching %s\n"), scripts_path);
+
+	switch(glob(scripts_path, GLOB_NOSORT, NULL, &scripts_path_glob))
+	{
+	case 0:
+		fprintf(stderr, _("badwolf: Notice: Found %zd userscripts\n"), scripts_path_glob.gl_pathc);
+		break;
+	case GLOB_NOMATCH:
+		fprintf(stderr, _("badwolf: Notice: No userscripts found\n"));
+		goto clean;
+		break;
+	case GLOB_NOSPACE:
+		fprintf(stderr, _("badwolf: Failed to load userscripts: Out of Memory\n"));
+		goto clean;
+		break;
+	case GLOB_ABORTED:
+		fprintf(stderr, _("badwolf: Failed to load userscripts: Read Error\n"));
+		goto clean;
+		break;
+	}
+
+	for(size_t i = 0; i < scripts_path_glob.gl_pathc; i++)
+	{
+		gsize l = 0;
+		gchar *contents;
+		g_file_get_contents(scripts_path_glob.gl_pathv[i], &contents, &l, NULL);
+
+		WebKitUserScript *userscript =
+		    webkit_user_script_new(contents,
+		                           WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
+		                           WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START,
+		                           NULL,
+		                           NULL);
+		webkit_user_content_manager_add_script(content_manager, userscript);
+	}
+
+	fprintf(stderr, _("badwolf: Notice: Userscripts loaded\n"));
+
+clean:
+	g_free(scripts_path);
+	globfree(&scripts_path_glob);
+}
diff --git a/userscripts.h b/userscripts.h
@@ -0,0 +1,7 @@
+// BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
+// SPDX-FileCopyrightText: 2019-2023 Badwolf Authors <https://hacktivis.me/projects/badwolf>
+// SPDX-License-Identifier: BSD-3-Clause
+
+#include <webkit2/webkit2.h>
+
+void load_userscripts(WebKitUserContentManager *content_manager);