Major styles update.

In config.h there is now some styles array to apply site-specific styles.
This can be toggled using the -mM flags. If a stylefile is manually specified,
then this will overwrite everything.
This commit is contained in:
Christoph Lohmann 2015-01-17 07:19:48 +01:00
parent c467e5a3b3
commit c2da2bae5d
3 changed files with 84 additions and 11 deletions

View File

@ -2,8 +2,8 @@
static char *useragent = "Mozilla/5.0 (X11; U; Unix; en-US) " static char *useragent = "Mozilla/5.0 (X11; U; Unix; en-US) "
"AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 " "AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 "
"Safari/537.15 Surf/"VERSION; "Safari/537.15 Surf/"VERSION;
static char *stylefile = "~/.surf/style.css";
static char *scriptfile = "~/.surf/script.js"; static char *scriptfile = "~/.surf/script.js";
static char *styledir = "~/.surf/styles/";
static Bool kioskmode = FALSE; /* Ignore shortcuts */ static Bool kioskmode = FALSE; /* Ignore shortcuts */
static Bool showindicators = TRUE; /* Show indicators in window title */ static Bool showindicators = TRUE; /* Show indicators in window title */
@ -27,6 +27,7 @@ static Bool enablespatialbrowsing = TRUE;
static Bool enableplugins = TRUE; static Bool enableplugins = TRUE;
static Bool enablescripts = TRUE; static Bool enablescripts = TRUE;
static Bool enableinspector = TRUE; static Bool enableinspector = TRUE;
static Bool enablestyles = TRUE;
static Bool loadimages = TRUE; static Bool loadimages = TRUE;
static Bool hidebackground = FALSE; static Bool hidebackground = FALSE;
static Bool allowgeolocation = TRUE; static Bool allowgeolocation = TRUE;
@ -49,6 +50,12 @@ static Bool allowgeolocation = TRUE;
} \ } \
} }
/* styles */
static SiteStyle styles[] = {
/* regexp file in $styledir */
{ ".*", "default.css" },
};
#define MODKEY GDK_CONTROL_MASK #define MODKEY GDK_CONTROL_MASK
/* hotkeys */ /* hotkeys */

9
surf.1
View File

@ -3,7 +3,7 @@
surf \- simple webkit-based browser surf \- simple webkit-based browser
.SH SYNOPSIS .SH SYNOPSIS
.B surf .B surf
.RB [-bBfFgGiIkKnNpPsSvx] .RB [-bBfFgGiIkKmMnNpPsSvx]
.RB [-a\ cookiepolicies] .RB [-a\ cookiepolicies]
.RB [-c\ cookiefile] .RB [-c\ cookiefile]
.RB [-e\ xid] .RB [-e\ xid]
@ -68,6 +68,12 @@ Disable kiosk mode (disable key strokes and right click)
.B \-K .B \-K
Enable kiosk mode (disable key strokes and right click) Enable kiosk mode (disable key strokes and right click)
.TP .TP
.B \-m
Disable the site-specific styles.
.TP
.B \-M
Enable the site-specific styles.
.TP
.B \-n .B \-n
Disable the Web Inspector (Developer Tools). Disable the Web Inspector (Developer Tools).
.TP .TP
@ -93,6 +99,7 @@ Enable Javascript
.B \-t stylefile .B \-t stylefile
Specify the user Specify the user
.I stylefile. .I stylefile.
This does disable the site-specific styles.
.TP .TP
.B \-u useragent .B \-u useragent
Specify the Specify the

77
surf.c
View File

@ -23,6 +23,7 @@
#include <sys/file.h> #include <sys/file.h>
#include <libgen.h> #include <libgen.h>
#include <stdarg.h> #include <stdarg.h>
#include <regex.h>
#include "arg.h" #include "arg.h"
@ -71,6 +72,12 @@ typedef struct {
G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT) G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
typedef struct {
char *regex;
char *style;
regex_t re;
} SiteStyle;
static Display *dpy; static Display *dpy;
static Atom atoms[AtomLast]; static Atom atoms[AtomLast];
static Client *clients = NULL; static Client *clients = NULL;
@ -82,6 +89,7 @@ static char togglestat[8];
static char pagestat[3]; static char pagestat[3];
static GTlsDatabase *tlsdb; static GTlsDatabase *tlsdb;
static int policysel = 0; static int policysel = 0;
static char *stylefile = NULL;
static void addaccelgroup(Client *c); static void addaccelgroup(Client *c);
static void beforerequest(WebKitWebView *w, WebKitWebFrame *f, static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
@ -127,6 +135,8 @@ static const char *getatom(Client *c, int a);
static void gettogglestat(Client *c); static void gettogglestat(Client *c);
static void getpagestat(Client *c); static void getpagestat(Client *c);
static char *geturi(Client *c); static char *geturi(Client *c);
static gchar *getstyle(const char *uri);
static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c); static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
static void inspector(Client *c, const Arg *arg); static void inspector(Client *c, const Arg *arg);
@ -533,6 +543,19 @@ geturi(Client *c) {
return uri; return uri;
} }
static gchar *
getstyle(const char *uri) {
int i;
for(i = 0; i < LENGTH(styles); i++) {
if(styles[i].regex && !regexec(&(styles[i].re), uri, 0,
NULL, 0)) {
return g_strconcat("file://", styles[i].style, NULL);
}
}
return g_strdup("");
}
static gboolean static gboolean
initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) { initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
Arg arg; Arg arg;
@ -629,6 +652,7 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
WebKitWebFrame *frame; WebKitWebFrame *frame;
WebKitWebDataSource *src; WebKitWebDataSource *src;
WebKitNetworkRequest *request; WebKitNetworkRequest *request;
WebKitWebSettings *set = webkit_web_view_get_settings(c->view);
SoupMessage *msg; SoupMessage *msg;
char *uri; char *uri;
@ -644,6 +668,11 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
& SOUP_MESSAGE_CERTIFICATE_TRUSTED); & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
} }
setatom(c, AtomUri, uri); setatom(c, AtomUri, uri);
if(stylefile == NULL && enablestyles) {
g_object_set(G_OBJECT(set), "user-stylesheet-uri",
getstyle(uri), NULL);
}
break; break;
case WEBKIT_LOAD_FINISHED: case WEBKIT_LOAD_FINISHED:
c->progress = 100; c->progress = 100;
@ -702,7 +731,7 @@ newclient(void) {
GdkGeometry hints = { 1, 1 }; GdkGeometry hints = { 1, 1 };
GdkScreen *screen; GdkScreen *screen;
gdouble dpi; gdouble dpi;
char *uri, *ua; char *uri = NULL, *ua;
if(!(c = calloc(1, sizeof(Client)))) if(!(c = calloc(1, sizeof(Client))))
die("Cannot malloc!\n"); die("Cannot malloc!\n");
@ -832,8 +861,10 @@ newclient(void) {
if(!(ua = getenv("SURF_USERAGENT"))) if(!(ua = getenv("SURF_USERAGENT")))
ua = useragent; ua = useragent;
g_object_set(G_OBJECT(settings), "user-agent", ua, NULL); g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
uri = g_strconcat("file://", stylefile, NULL); if (stylefile != NULL) {
g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL); uri = g_strconcat("file://", stylefile, NULL);
g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
}
g_object_set(G_OBJECT(settings), "auto-load-images", loadimages, g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
NULL); NULL);
g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins, g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
@ -888,7 +919,8 @@ newclient(void) {
fullscreen(c, NULL); fullscreen(c, NULL);
} }
g_free(uri); if(stylefile != NULL)
g_free(uri);
setatom(c, AtomFind, ""); setatom(c, AtomFind, "");
setatom(c, AtomUri, "about:blank"); setatom(c, AtomUri, "about:blank");
@ -1094,6 +1126,7 @@ setatom(Client *c, int a, const char *v) {
static void static void
setup(void) { setup(void) {
int i;
char *proxy; char *proxy;
char *new_proxy; char *new_proxy;
SoupURI *puri; SoupURI *puri;
@ -1114,7 +1147,23 @@ setup(void) {
/* dirs and files */ /* dirs and files */
cookiefile = buildpath(cookiefile); cookiefile = buildpath(cookiefile);
scriptfile = buildpath(scriptfile); scriptfile = buildpath(scriptfile);
stylefile = buildpath(stylefile); styledir = buildpath(styledir);
if(stylefile == NULL && enablestyles) {
for(i = 0; i < LENGTH(styles); i++) {
if(regcomp(&(styles[i].re), styles[i].regex,
REG_EXTENDED)) {
fprintf(stderr,
"Could not compile regex: %s\n",
styles[i].regex);
styles[i].regex = NULL;
}
styles[i].style = buildpath(
g_strconcat(styledir,
styles[i].style, NULL));
}
} else {
stylefile = buildpath(stylefile);
}
/* request handler */ /* request handler */
s = webkit_get_default_session(); s = webkit_get_default_session();
@ -1282,12 +1331,16 @@ togglescrollbars(Client *c, const Arg *arg) {
static void static void
togglestyle(Client *c, const Arg *arg) { togglestyle(Client *c, const Arg *arg) {
WebKitWebSettings *settings; WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
char *uri; char *uri;
settings = webkit_web_view_get_settings(c->view);
g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL); g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
uri = uri[0] ? g_strdup("") : g_strconcat("file://", stylefile, NULL); if(stylefile == NULL && enablestyles) {
uri = (uri && uri[0])? g_strdup("") : getstyle(geturi(c));
} else {
uri = uri[0]? g_strdup("") : g_strconcat("file://",
stylefile, NULL);
}
g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL); g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
updatetitle(c); updatetitle(c);
@ -1318,7 +1371,7 @@ gettogglestat(Client *c){
togglestat[p++] = value? 'V': 'v'; togglestat[p++] = value? 'V': 'v';
g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL); g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
togglestat[p++] = uri[0] ? 'M': 'm'; togglestat[p++] = (uri && uri[0]) ? 'M': 'm';
togglestat[p] = '\0'; togglestat[p] = '\0';
} }
@ -1451,6 +1504,12 @@ main(int argc, char *argv[]) {
case 'K': case 'K':
kioskmode = 1; kioskmode = 1;
break; break;
case 'm':
enablestyles = 0;
break;
case 'M':
enablestyles = 1;
break;
case 'n': case 'n':
enableinspector = 0; enableinspector = 0;
break; break;