surf-sitejs-20220214-94226b8.diff (3338B)
1 From 94226b8009dcb92a309148f73a39cbb6223ea34b Mon Sep 17 00:00:00 2001 2 From: avalonwilliams <avalonwilliams@protonmail.com> 3 Date: Mon, 14 Feb 2022 23:29:35 -0500 4 Subject: [PATCH] Per-site JS script patch 5 6 Allows configuration of different javascript files for different sites 7 (similar to stylesheets) 8 --- 9 config.def.h | 10 ++++++++++ 10 surf.c | 34 ++++++++++++++++++++++++++++++---- 11 2 files changed, 40 insertions(+), 4 deletions(-) 12 13 diff --git a/config.def.h b/config.def.h 14 index 1355ba3..8ba093b 100644 15 --- a/config.def.h 16 +++ b/config.def.h 17 @@ -6,6 +6,7 @@ static char *styledir = "~/.surf/styles/"; 18 static char *certdir = "~/.surf/certificates/"; 19 static char *cachedir = "~/.surf/cache/"; 20 static char *cookiefile = "~/.surf/cookies.txt"; 21 +static char *scriptdir = "~/.surf/scripts/"; 22 23 /* Webkit default features */ 24 /* Highest priority value will be used. 25 @@ -121,6 +122,15 @@ static SiteSpecific certs[] = { 26 { "://suckless\\.org/", "suckless.org.crt" }, 27 }; 28 29 +/* scripts */ 30 +/* 31 + * Run scripts on certain URLs, will inject more than one script 32 + */ 33 +static SiteSpecific scripts[] = { 34 + /* regexp script in $scriptdir */ 35 + { "://duckduckgo\\.com", "example.js" }, 36 +}; 37 + 38 #define MODKEY GDK_CONTROL_MASK 39 40 /* hotkeys */ 41 diff --git a/surf.c b/surf.c 42 index 03d8242..3aa84a3 100644 43 --- a/surf.c 44 +++ b/surf.c 45 @@ -168,7 +168,8 @@ static const char *getcert(const char *uri); 46 static void setcert(Client *c, const char *file); 47 static const char *getstyle(const char *uri); 48 static void setstyle(Client *c, const char *file); 49 -static void runscript(Client *c); 50 +static void runscript(Client *c, const char *file); 51 +static void runsitescripts(Client *c, const char *uri); 52 static void evalscript(Client *c, const char *jsstr, ...); 53 static void updatewinid(Client *c); 54 static void handleplumb(Client *c, const char *uri); 55 @@ -400,6 +401,17 @@ setup(void) 56 stylefile = buildfile(stylefile); 57 } 58 59 + scriptdir = buildpath(scriptdir); 60 + for (i = 0; i < LENGTH(scripts); ++i) { 61 + if (!regcomp(&(scripts[i].re), scripts[i].regex, REG_EXTENDED)) { 62 + scripts[i].file = g_strconcat(scriptdir, "/", 63 + scripts[i].file, NULL); 64 + } else { 65 + fprintf(stderr, "Could not compile regex: %s\n", scripts[i].regex); 66 + scripts[i].regex = NULL; 67 + } 68 + } 69 + 70 for (i = 0; i < LENGTH(uriparams); ++i) { 71 if (regcomp(&(uriparams[i].re), uriparams[i].uri, 72 REG_EXTENDED)) { 73 @@ -951,12 +963,25 @@ setstyle(Client *c, const char *file) 74 } 75 76 void 77 -runscript(Client *c) 78 +runsitescripts(Client *c, const char *uri) { 79 + gchar *script; 80 + gsize l; 81 + int i; 82 + 83 + for (i = 0; i < LENGTH(scripts); ++i) { 84 + if (scripts[i].regex && 85 + !regexec(&(scripts[i].re), uri, 0, NULL, 0)) 86 + runscript(c, scripts[i].file); 87 + } 88 +} 89 + 90 +void 91 +runscript(Client *c, const char *file) 92 { 93 gchar *script; 94 gsize l; 95 96 - if (g_file_get_contents(scriptfile, &script, &l, NULL) && l) 97 + if (g_file_get_contents(file, &script, &l, NULL) && l) 98 evalscript(c, "%s", script); 99 g_free(script); 100 } 101 @@ -1536,7 +1561,8 @@ loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c) 102 evalscript(c, "document.documentElement.style.overflow = '%s'", 103 enablescrollbars ? "auto" : "hidden"); 104 */ 105 - runscript(c); 106 + runsitescripts(c, uri); 107 + runscript(c, scriptfile); 108 break; 109 } 110 updatetitle(c); 111 -- 112 2.34.1 113