[wiki] [sites] Added sitejs patch to surf || avalonwilliams

From: <git_AT_suckless.org>
Date: Tue, 15 Feb 2022 05:48:29 +0100

commit 84d9fba8c6995b6d379738db1c9c4fe476273621
Author: avalonwilliams <avalonwilliams_AT_protonmail.com>
Date: Mon Feb 14 23:47:42 2022 -0500

    Added sitejs patch to surf
    
    Added a patch that allows site-specific scripts for surf

diff --git a/surf.suckless.org/patches/sitejs/index.md b/surf.suckless.org/patches/sitejs/index.md
new file mode 100644
index 00000000..3212c57e
--- /dev/null
+++ b/surf.suckless.org/patches/sitejs/index.md
_AT_@ -0,0 +1,31 @@
+Site Specific JS
+================
+
+Description
+-----------
+
+This patch allows scripts to be injected based on the url matching
+a regex, allowing scripts to be site-specfic.
+
+It also can serve as a more complex replacement for the multijs patch.
+
+Configuration
+-------------
+
+In your `config.h`:
+
+ static char *scriptdir = "~/.surf/scripts/";
+ static SiteSpecific scripts[] = {
+ /* regexp script in $scriptdir */
+ { "://duckduckgo\.com", "example.js" },
+ };
+
+Download
+--------
+
+* [surf-sitejs-20220214-94226b8.diff](surf-sitejs-20220214-94226b8.diff) (3.3k)
+
+Author
+------
+
+* Avalon Williams <avalonwilliams_AT_protonmail.com>
diff --git a/surf.suckless.org/patches/sitejs/surf-sitejs-20220214-94226b8.diff b/surf.suckless.org/patches/sitejs/surf-sitejs-20220214-94226b8.diff
new file mode 100644
index 00000000..f7413195
--- /dev/null
+++ b/surf.suckless.org/patches/sitejs/surf-sitejs-20220214-94226b8.diff
_AT_@ -0,0 +1,113 @@
+From 94226b8009dcb92a309148f73a39cbb6223ea34b Mon Sep 17 00:00:00 2001
+From: avalonwilliams <avalonwilliams_AT_protonmail.com>
+Date: Mon, 14 Feb 2022 23:29:35 -0500
+Subject: [PATCH] Per-site JS script patch
+
+Allows configuration of different javascript files for different sites
+(similar to stylesheets)
+---
+ config.def.h | 10 ++++++++++
+ surf.c | 34 ++++++++++++++++++++++++++++++----
+ 2 files changed, 40 insertions(+), 4 deletions(-)
+
+diff --git a/config.def.h b/config.def.h
+index 1355ba3..8ba093b 100644
+--- a/config.def.h
++++ b/config.def.h
+_AT_@ -6,6 +6,7 @@ static char *styledir = "~/.surf/styles/";
+ static char *certdir = "~/.surf/certificates/";
+ static char *cachedir = "~/.surf/cache/";
+ static char *cookiefile = "~/.surf/cookies.txt";
++static char *scriptdir = "~/.surf/scripts/";
+
+ /* Webkit default features */
+ /* Highest priority value will be used.
+_AT_@ -121,6 +122,15 @@ static SiteSpecific certs[] = {
+ { "://suckless\.org/", "suckless.org.crt" },
+ };
+
++/* scripts */
++/*
++ * Run scripts on certain URLs, will inject more than one script
++ */
++static SiteSpecific scripts[] = {
++ /* regexp script in $scriptdir */
++ { "://duckduckgo\.com", "example.js" },
++};
++
+ #define MODKEY GDK_CONTROL_MASK
+
+ /* hotkeys */
+diff --git a/surf.c b/surf.c
+index 03d8242..3aa84a3 100644
+--- a/surf.c
++++ b/surf.c
+_AT_@ -168,7 +168,8 @@ static const char *getcert(const char *uri);
+ static void setcert(Client *c, const char *file);
+ static const char *getstyle(const char *uri);
+ static void setstyle(Client *c, const char *file);
+-static void runscript(Client *c);
++static void runscript(Client *c, const char *file);
++static void runsitescripts(Client *c, const char *uri);
+ static void evalscript(Client *c, const char *jsstr, ...);
+ static void updatewinid(Client *c);
+ static void handleplumb(Client *c, const char *uri);
+_AT_@ -400,6 +401,17 @@ setup(void)
+ stylefile = buildfile(stylefile);
+ }
+
++ scriptdir = buildpath(scriptdir);
++ for (i = 0; i < LENGTH(scripts); ++i) {
++ if (!regcomp(&(scripts[i].re), scripts[i].regex, REG_EXTENDED)) {
++ scripts[i].file = g_strconcat(scriptdir, "/",
++ scripts[i].file, NULL);
++ } else {
++ fprintf(stderr, "Could not compile regex: %s
", scripts[i].regex);
++ scripts[i].regex = NULL;
++ }
++ }
++
+ for (i = 0; i < LENGTH(uriparams); ++i) {
+ if (regcomp(&(uriparams[i].re), uriparams[i].uri,
+ REG_EXTENDED)) {
+_AT_@ -951,12 +963,25 @@ setstyle(Client *c, const char *file)
+ }
+
+ void
+-runscript(Client *c)
++runsitescripts(Client *c, const char *uri) {
++ gchar *script;
++ gsize l;
++ int i;
++
++ for (i = 0; i < LENGTH(scripts); ++i) {
++ if (scripts[i].regex &&
++ !regexec(&(scripts[i].re), uri, 0, NULL, 0))
++ runscript(c, scripts[i].file);
++ }
++}
++
++void
++runscript(Client *c, const char *file)
+ {
+ gchar *script;
+ gsize l;
+
+- if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
++ if (g_file_get_contents(file, &script, &l, NULL) && l)
+ evalscript(c, "%s", script);
+ g_free(script);
+ }
+_AT_@ -1536,7 +1561,8 @@ loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
+ evalscript(c, "document.documentElement.style.overflow = '%s'",
+ enablescrollbars ? "auto" : "hidden");
+ */
+- runscript(c);
++ runsitescripts(c, uri);
++ runscript(c, scriptfile);
+ break;
+ }
+ updatetitle(c);
+--
+2.34.1
+
Received on Tue Feb 15 2022 - 05:48:29 CET

This archive was generated by hypermail 2.3.0 : Tue Feb 15 2022 - 05:48:51 CET